View Javadoc

1   /**
2    * Pyx4me framework
3    * Copyright (C) 2006-2008 pyx4j.com.
4    * 
5    * Licensed under the Apache License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    * http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing,
12   * software distributed under the License is distributed on an
13   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14   * KIND, either express or implied.  See the License for the
15   * specific language governing permissions and limitations
16   * under the License.
17   * 
18   * @author vlads
19   * @version $Id: PreprocessMojo.java 2066 2008-06-21 23:16:18Z vlads $
20   */
21  package com.pyx4me.maven.j2me;
22  
23  import java.io.File;
24  import java.util.Iterator;
25  import java.util.List;
26  import java.util.Vector;
27  
28  import net.sf.jour.PreProcessor;
29  
30  import org.apache.maven.artifact.Artifact;
31  import org.apache.maven.plugin.MojoExecutionException;
32  import org.apache.maven.plugin.MojoFailureException;
33  import org.apache.tools.ant.Project;
34  import org.apache.tools.ant.Task;
35  
36  import com.pyx4j.log4j.MavenLogAppender;
37  
38  import de.pleumann.antenna.misc.Utility;
39  
40  /**
41   * The j2me:preprocess helper to optimize generated classes.
42   * 
43   * @author vlads
44   * 
45   * @goal preprocess
46   * 
47   * @description Preprocess
48   */
49  public abstract class PreprocessMojo extends AbstractJadWtkMojo {
50  
51  	/**
52  	 * Reads configuration options from the given file. File or resource name.
53  	 * 
54  	 * @parameter default-value="${basedir}/jour.xml"
55  	 */
56  	private String jourConfig;
57  
58  	/**
59  	 * Output directory name relative to outputDirectory parameter.
60  	 * 
61  	 * @parameter expression="iclasses"
62  	 * @required
63  	 */
64  	protected String output;
65  
66  	public void execute() throws MojoExecutionException, MojoFailureException {
67  		PreprocessMojo.executeClassPreprocessor(jourConfig, getJarFile(classifier), output, this);
68  	}
69  
70  	public static File executeClassPreprocessor(String jourConfig, File jarFileOrClassesDirectory, String output,
71  			AbstractJadWtkMojo mojo) throws MojoExecutionException, MojoFailureException {
72  		MavenLogAppender.startPluginLog(mojo);
73  		try {
74  
75  			mojo.getLog().info(jourConfig);
76  
77  			File out = new File(mojo.outputDirectory, output);
78  
79  			List classpath = new Vector();
80  
81  			if (mojo.useWtkLibs) {
82  				// Find MIDP ClassPath from WTK
83  				Project antProject = mojo.getAntProject();
84  				Task dummyTaks = new Task() {
85  				};
86  				dummyTaks.setTaskName("jour");
87  				dummyTaks.setProject(antProject);
88  
89  				Utility utility = Utility.getInstance(antProject, dummyTaks);
90  
91  				String mdipClassPath = utility.getMidpApi();
92  				classpath.add(mdipClassPath);
93  			} else {
94  				List dependancy = mojo.mavenProject.getCompileArtifacts();
95  				for (Iterator i = dependancy.iterator(); i.hasNext();) {
96  					Artifact artifact = (Artifact) i.next();
97  					File file = getClasspathElement(artifact, mojo.mavenProject);
98  					classpath.add(file.toString());
99  				}
100 				if (mojo.libs != null) {
101 					for (Iterator i = mojo.libs.iterator(); i.hasNext();) {
102 						Object lib = i.next();
103 						classpath.add(lib.toString());
104 					}
105 				}
106 			}
107 
108 			PreProcessor pp = new PreProcessor(jourConfig, jarFileOrClassesDirectory, out, classpath);
109 
110 			pp.setCopyClasses(true);
111 			pp.setCopyResources(true);
112 
113 			try {
114 				pp.process();
115 			} catch (Exception e) {
116 				mojo.getLog().error("PreProcessing error", e);
117 				throw new MojoExecutionException("PreProcessing error", e);
118 			}
119 
120 			return out;
121 
122 		} finally {
123 			MavenLogAppender.endPluginLog(mojo);
124 		}
125 	}
126 
127 }