View Javadoc

1   /*
2    * Jour - bytecode instrumentation library
3    *
4    * Copyright (C) 2007-2008 Vlad Skarzhevskyy
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Library General Public
8    * License as published by the Free Software Foundation; either
9    * version 2 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Library General Public License for more details.
15   *
16   * You should have received a copy of the GNU Library General Public
17   * License along with this library; if not, write to the
18   * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19   * Boston, MA  02111-1307, USA.
20   */
21  package net.sf.jour.maven;
22  
23  import java.io.File;
24  import java.util.Iterator;
25  import java.util.List;
26  
27  import net.sf.jour.signature.APIFilter;
28  import net.sf.jour.signature.ExportClasses;
29  import net.sf.jour.signature.SignatureImport;
30  
31  import org.apache.maven.artifact.Artifact;
32  import org.apache.maven.plugin.AbstractMojo;
33  import org.apache.maven.plugin.MojoExecutionException;
34  import org.apache.maven.plugin.MojoFailureException;
35  import org.apache.maven.plugin.logging.Log;
36  import org.apache.maven.project.MavenProject;
37  
38  import com.pyx4j.log4j.MavenLogAppender;
39  
40  /**
41   * The jour:generate will create API stub classes.
42   * 
43   * @author vlads
44   * 
45   * @goal generate
46   * 
47   * @phase compile
48   * 
49   * @description Export API descriptor XML to classes
50   */
51  public class GenerateMojo extends AbstractMojo {
52  
53  	/**
54  	 * The directory containing project classes.
55  	 * 
56  	 * @parameter expression="${project.build.outputDirectory}"
57  	 * @required
58  	 */
59  	private File output;
60  
61  	/**
62  	 * The API descriptor XML.
63  	 * 
64  	 * @parameter
65  	 * @required
66  	 */
67  	private File signature;
68  
69  	/**
70  	 * Appends the system search path to the end of the search path. The system
71  	 * search path usually includes the platform library, extension libraries,
72  	 * and the search path specified by the <code>-classpath</code> option or
73  	 * the <code>CLASSPATH</code> environment variable.
74  	 * 
75  	 * @parameter expression="false"
76  	 */
77  	private boolean useSystemClassPath;
78  
79  	/**
80  	 * Java platform version for created classes. e.g. 1.1, 1.3, 1.4, 1.5 or 1.6
81  	 * 
82  	 * @parameter
83  	 */
84  	private String classVersion;
85  
86  	/**
87  	 * Export API level public|[protected]|package|private
88  	 * 
89  	 * @parameter expression="protected"
90  	 */
91  	private String level;
92  
93  	/**
94  	 * Export Only selected packages
95  	 * 
96  	 * @parameter
97  	 */
98  	private String packages;
99  
100 	/**
101 	 * API stub empty method/constructor body code may just throw Exception
102 	 * class name can be selected.
103 	 * 
104 	 * @parameter
105 	 */
106 	private String stubException;
107 
108 	/**
109 	 * Exception class constructor String argument
110 	 * 
111 	 * @parameter
112 	 */
113 	private String stubExceptionMessage;
114 
115 	/**
116 	 * The Maven project reference where the plugin is currently being executed.
117 	 * Used for dependency resolution during compilation. The default value is
118 	 * populated from maven.
119 	 * 
120 	 * @parameter expression="${project}"
121 	 * @readonly
122 	 * @required
123 	 */
124 	protected MavenProject mavenProject;
125 
126 	/*
127 	 * (non-Javadoc)
128 	 * 
129 	 * @see org.apache.maven.plugin.Mojo#execute()
130 	 */
131 	public void execute() throws MojoExecutionException, MojoFailureException {
132 		Log log = getLog();
133 		MavenLogAppender.startPluginLog(this);
134 		log.info("use signature: " + signature);
135 		log.debug("packages: " + packages + " level:" + level);
136 
137 		StringBuffer supportingJars = new StringBuffer();
138 
139 		List dependancy = this.mavenProject.getTestArtifacts();
140 		for (Iterator i = dependancy.iterator(); i.hasNext();) {
141 			Artifact artifact = (Artifact) i.next();
142 			File file = InstrumentationMojo.getClasspathElement(artifact, mavenProject);
143 			log.debug("dependancy:" + file.toString());
144 			if (supportingJars.length() < 0) {
145 				supportingJars.append(File.pathSeparatorChar);
146 			}
147 			supportingJars.append(file.toString());
148 		}
149 
150 		SignatureImport im = new SignatureImport(useSystemClassPath, (supportingJars.length() > 0) ? supportingJars
151 				.toString() : null);
152 
153 		im.setStubException(stubException);
154 		im.setStubExceptionMessage(stubExceptionMessage);
155 
156 		APIFilter apiFilter = new APIFilter(level, packages);
157 
158 		im.load(signature.getAbsolutePath(), apiFilter);
159 
160 		log.debug("loaded " + im.getClassNames().size() + " classe(s)");
161 
162 		log.debug("output: " + output.getAbsolutePath());
163 
164 		int count = ExportClasses.export(output.getAbsolutePath(), im.getClasses(), classVersion);
165 		log.info("Created " + count + " classe(s)");
166 	}
167 
168 }