1 /*
2 * @(#)$Id: JavafxdocJar.java 63 2010-04-08 01:07:01Z nderwin $
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 * under the License.
16 */
17
18 package net.sf.jfxdplugin;
19
20 import java.io.File;
21 import java.io.IOException;
22 import org.apache.maven.archiver.MavenArchiveConfiguration;
23 import org.apache.maven.archiver.MavenArchiver;
24 import org.apache.maven.artifact.DependencyResolutionRequiredException;
25 import org.apache.maven.plugin.MojoExecutionException;
26 import org.apache.maven.plugin.MojoFailureException;
27 import org.apache.maven.reporting.MavenReportException;
28 import org.codehaus.plexus.archiver.ArchiverException;
29 import org.codehaus.plexus.archiver.jar.JarArchiver;
30 import org.codehaus.plexus.archiver.jar.ManifestException;
31 import org.codehaus.plexus.archiver.util.DefaultFileSet;
32
33 /**
34 * Bundles the Javafxdoc documentation for the <code>JavaFX code</code> in a
35 * <b>NON aggregator</b> project into a jar using the standard Javafxdoc tool.
36 * <p>
37 * Based on the Maven Javadoc Plugin version 2.6.
38 *
39 * @author Nathan Erwin
40 * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
41 * @version $Revision: 63 $ $Date: 2010-04-07 21:07:01 -0400 (Wed, 07 Apr 2010) $
42 * @see <a href="http://maven.apache.org/plugins/maven-javadoc-plugin">
43 * http://maven.apache.org/plugins/maven-javadoc-plugin</a>
44 * @since 1.0
45 * @goal jar
46 * @phase package
47 */
48 public class JavafxdocJar extends AbstractJavafxdocMojo {
49
50 /**
51 * Includes all generated Javafxdoc files
52 */
53 private static final String[] DEFAULT_INCLUDES = new String[]{"**/**"};
54 /**
55 * Excludes all processing files.
56 *
57 * @see AbstractJavadocMojo#DEBUG_JAVAFXDOC_SCRIPT_NAME
58 * @see AbstractJavadocMojo#OPTIONS_FILE_NAME
59 * @see AbstractJavadocMojo#FILES_FILE_NAME
60 */
61 private static final String[] DEFAULT_EXCLUDES = new String[]{
62 DEBUG_JAVAFXDOC_SCRIPT_NAME, OPTIONS_FILE_NAME, FILES_FILE_NAME};
63 // ----------------------------------------------------------------------
64 // Mojo components
65 // ----------------------------------------------------------------------
66 /**
67 * The Jar archiver.
68 *
69 * @component role="org.codehaus.plexus.archiver.Archiver" roleHint="jar"
70 */
71 private JarArchiver jarArchiver;
72 // ----------------------------------------------------------------------
73 // Mojo Parameters
74 // ----------------------------------------------------------------------
75 /**
76 * Specifies the output directory for the generated jar file.
77 *
78 * @parameter expression="${project.build.directory}"
79 */
80 private String jarOutputDirectory;
81 /**
82 * Specifies the filename that will be used for the generated jar file.
83 * Please note that <code>-javafxdoc</code> or <code>-test-javafxdoc</code>
84 * will be appended to the file name.
85 *
86 * @parameter expression="${project.build.finalName}"
87 */
88 private String finalName;
89 /**
90 * The archive configuration to use.
91 *
92 * @see <a href="http://maven.apache.org/shared/maven-archiver/index.html">
93 * Maven Archiver Reference</a>
94 * @parameter
95 */
96 private MavenArchiveConfiguration archive = new MavenArchiveConfiguration();
97 /**
98 * Path to the default MANIFEST file to use. It will be used if
99 * <code>useDefaultManifestFile</code> is set to <code>true</code>.
100 *
101 * @parameter expression="${project.build.outputDirectory}/META-INF/MANIFEST.MF"
102 * @required
103 * @readonly
104 */
105 private File defaultManifestFile;
106 /**
107 * Set this to <code>true</code> to enable the use of the
108 * <code>defaultManifestFile</code>.
109 *
110 * @parameter default-value="false"
111 */
112 private boolean useDefaultManifestFile = false;
113
114 /**
115 * Entry point for the jar goal.
116 *
117 * @throws MojoExecutionException thrown if and exception occurred during
118 * the execution of the plugin
119 * @throws MojoFailureException not thrown
120 */
121 public void execute() throws MojoExecutionException, MojoFailureException {
122 try {
123 executeReport();
124
125 if (getOutputDirectory().exists()) {
126 generateArchive(getOutputDirectory(),
127 getFinalName() + "-" + "javafxdoc.jar");
128 } else {
129 getLog().warn(formatMessage("jar.nothing.to.generate"));
130 }
131 } catch (MavenReportException ex) {
132 String message = formatMessage("jar.error", ex.getMessage());
133 if (isFailOnError()) {
134 throw new MojoExecutionException(message, ex);
135 }
136 getLog().error(message, ex);
137 } catch (ArchiverException ex) {
138 String message = formatMessage("jar.error", ex.getMessage());
139 if (isFailOnError()) {
140 throw new MojoExecutionException(message, ex);
141 }
142 getLog().error(message, ex);
143 } catch (ManifestException ex) {
144 String message = formatMessage("jar.error", ex.getMessage());
145 if (isFailOnError()) {
146 throw new MojoExecutionException(message, ex);
147 }
148 getLog().error(message, ex);
149 } catch (IOException ex) {
150 String message = formatMessage("jar.error", ex.getMessage());
151 if (isFailOnError()) {
152 throw new MojoExecutionException(message, ex);
153 }
154 getLog().error(message, ex);
155 } catch (DependencyResolutionRequiredException ex) {
156 String message = formatMessage("jar.error", ex.getMessage());
157 if (isFailOnError()) {
158 throw new MojoExecutionException(message, ex);
159 }
160 getLog().error(message, ex);
161 }
162 }
163
164 /**
165 * Returns the final name for the generated jar file.
166 *
167 * @return the jar file name
168 * @see #finalName
169 */
170 public String getFinalName() {
171 return finalName;
172 }
173
174 /**
175 * Sets the final name for the generated jar file.
176 *
177 * @param finalName the jar file name
178 * @see #finalName
179 */
180 public void setFinalName(String finalName) {
181 this.finalName = finalName;
182 }
183
184 /**
185 * Returns the jar archiver for the plugin.
186 *
187 * @return the jar archiver
188 * @see #jarArchiver
189 */
190 public JarArchiver getJarArchiver() {
191 return jarArchiver;
192 }
193
194 /**
195 * Sets the jar archiver for the plugin.
196 *
197 * @param jarArchiver the jar archiver
198 * @see #jarArchiver
199 */
200 public void setJarArchiver(JarArchiver jarArchiver) {
201 this.jarArchiver = jarArchiver;
202 }
203
204 /**
205 * Returns the output directory for the generated jar file.
206 *
207 * @return the jar output directory
208 * @see #jarOutputDirectory
209 */
210 public String getJarOutputDirectory() {
211 return jarOutputDirectory;
212 }
213
214 /**
215 * Sets the output directory for the generated jar file.
216 *
217 * @param jarOutputDirectory the jar output directory
218 * @see #jarOutputDirectory
219 */
220 public void setJarOutputDirectory(String jarOutputDirectory) {
221 this.jarOutputDirectory = jarOutputDirectory;
222 }
223
224 /**
225 * Returns the Maven archive configuration.
226 *
227 * @return the Maven archive configuration
228 * @see #archive
229 */
230 public MavenArchiveConfiguration getArchive() {
231 return archive;
232 }
233
234 /**
235 * Sets the Maven archive configuration.
236 *
237 * @param archive the Maven archive configuration
238 * @see #archive
239 */
240 public void setArchive(MavenArchiveConfiguration archive) {
241 this.archive = archive;
242 }
243
244 /**
245 * Returns the default jar manifest file.
246 *
247 * @return the default manifest file
248 * @see #defaultManifestFile
249 */
250 public File getDefaultManifestFile() {
251 return defaultManifestFile;
252 }
253
254 /**
255 * Sets the default jar manifest file.
256 *
257 * @param defaultManifestFile the default manifest file
258 * @see #defaultManifestFile
259 */
260 public void setDefaultManifestFile(File defaultManifestFile) {
261 this.defaultManifestFile = defaultManifestFile;
262 }
263
264 /**
265 * Returns whether the default manifest file is used.
266 *
267 * @return true if used, false if not
268 * @see #useDefaultManifestFile
269 */
270 public boolean isUseDefaultManifestFile() {
271 return useDefaultManifestFile;
272 }
273
274 /**
275 * Sets whether the default manifest file is used.
276 *
277 * @param useDefaultManifestFile true is used, false if not
278 * @see #useDefaultManifestFile
279 */
280 public void setUseDefaultManifestFile(boolean useDefaultManifestFile) {
281 this.useDefaultManifestFile = useDefaultManifestFile;
282 }
283
284 private File generateArchive(File outputDir, String jarFileName) throws
285 ArchiverException, ManifestException, IOException,
286 DependencyResolutionRequiredException {
287
288 File jarFile = new File(getJarOutputDirectory(), jarFileName);
289
290 if (jarFile.exists()) {
291 jarFile.delete();
292 }
293
294 MavenArchiver archiver = new MavenArchiver();
295 archiver.setArchiver(getJarArchiver());
296 archiver.setOutputFile(jarFile);
297
298 DefaultFileSet fileSet = new DefaultFileSet();
299 fileSet.setDirectory(outputDir);
300 fileSet.setIncludes(DEFAULT_INCLUDES);
301 fileSet.setExcludes(DEFAULT_EXCLUDES);
302 archiver.getArchiver().addFileSet(fileSet);
303
304 if ((isUseDefaultManifestFile())
305 && (getDefaultManifestFile().exists())
306 && (null == getArchive().getManifestFile())) {
307 getLog().info(formatMessage("jar.existing.manifest",
308 getDefaultManifestFile().getPath()));
309 getArchive().setManifestFile(getDefaultManifestFile());
310 }
311
312 getArchive().setAddMavenDescriptor(false);
313 archiver.createArchive(getProject(), getArchive());
314
315 return jarFile;
316 }
317 }