1 package org.apache.maven.plugins.javadoc;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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.artifact.handler.ArtifactHandler;
26 import org.apache.maven.model.Resource;
27 import org.apache.maven.plugin.MojoExecutionException;
28 import org.apache.maven.plugins.annotations.Component;
29 import org.apache.maven.plugins.annotations.LifecyclePhase;
30 import org.apache.maven.plugins.annotations.Mojo;
31 import org.apache.maven.plugins.annotations.Parameter;
32 import org.apache.maven.plugins.annotations.ResolutionScope;
33 import org.apache.maven.project.MavenProjectHelper;
34 import org.apache.maven.reporting.MavenReportException;
35 import org.codehaus.plexus.archiver.Archiver;
36 import org.codehaus.plexus.archiver.ArchiverException;
37 import org.codehaus.plexus.archiver.jar.JarArchiver;
38 import org.codehaus.plexus.archiver.jar.ManifestException;
39
40 import java.io.File;
41 import java.io.IOException;
42 import java.util.List;
43 import java.util.Locale;
44
45
46
47
48
49
50
51 @Mojo( name = "jar", defaultPhase = LifecyclePhase.PACKAGE, requiresDependencyResolution = ResolutionScope.COMPILE,
52 threadSafe = true )
53 public class JavadocJar
54 extends AbstractJavadocMojo
55 {
56
57
58
59 private static final String[] DEFAULT_INCLUDES = new String[]{ "**/**" };
60
61
62
63
64
65
66
67
68
69
70 private static final String[] DEFAULT_EXCLUDES =
71 new String[]{ DEBUG_JAVADOC_SCRIPT_NAME, OPTIONS_FILE_NAME, PACKAGES_FILE_NAME, ARGFILE_FILE_NAME,
72 FILES_FILE_NAME };
73
74
75
76
77
78
79
80
81 @Component
82 private MavenProjectHelper projectHelper;
83
84
85
86
87
88
89 @Component( role = Archiver.class, hint = "jar" )
90 private JarArchiver jarArchiver;
91
92
93
94
95
96
97
98
99
100
101
102 @Deprecated
103 @Parameter( property = "destDir" )
104 private File destDir;
105
106
107
108
109 @Parameter( property = "project.build.directory" )
110 private String jarOutputDirectory;
111
112
113
114
115
116 @Parameter( property = "project.build.finalName" )
117 private String finalName;
118
119
120
121
122
123 @Parameter( property = "attach", defaultValue = "true" )
124 private boolean attach;
125
126
127
128
129
130
131
132 @Parameter
133 private MavenArchiveConfiguration archive = new JavadocArchiveConfiguration();
134
135
136
137
138
139
140
141 @Parameter( defaultValue = "${project.build.outputDirectory}/META-INF/MANIFEST.MF", required = true,
142 readonly = true )
143 private File defaultManifestFile;
144
145
146
147
148
149
150
151 @Parameter( defaultValue = "false" )
152 private boolean useDefaultManifestFile;
153
154
155
156
157 @Parameter( property = "maven.javadoc.classifier", defaultValue = "javadoc", required = true )
158 private String classifier;
159
160
161 @Override
162 public void doExecute()
163 throws MojoExecutionException
164 {
165 if ( skip )
166 {
167 getLog().info( "Skipping javadoc generation" );
168 return;
169 }
170
171 File innerDestDir = this.destDir;
172 if ( innerDestDir == null )
173 {
174 innerDestDir = new File( getOutputDirectory() );
175 }
176
177 if ( !isAggregator() || !"pom".equalsIgnoreCase( project.getPackaging() ) )
178 {
179 ArtifactHandler artifactHandler = project.getArtifact().getArtifactHandler();
180 if ( !"java".equals( artifactHandler.getLanguage() ) )
181 {
182 getLog().info( "Not executing Javadoc as the project is not a Java classpath-capable package" );
183 return;
184 }
185 }
186
187 try
188 {
189 executeReport( Locale.getDefault() );
190 }
191 catch ( MavenReportException e )
192 {
193 failOnError( "MavenReportException: Error while generating Javadoc", e );
194 }
195 catch ( RuntimeException e )
196 {
197 failOnError( "RuntimeException: Error while generating Javadoc", e );
198 }
199
200 if ( innerDestDir.exists() )
201 {
202 try
203 {
204 File outputFile = generateArchive( innerDestDir, finalName + "-" + getClassifier() + ".jar" );
205
206 if ( !attach )
207 {
208 getLog().info( "NOT adding javadoc to attached artifacts list." );
209 }
210 else
211 {
212
213
214 projectHelper.attachArtifact( project, "javadoc", getClassifier(), outputFile );
215 }
216 }
217 catch ( ArchiverException e )
218 {
219 failOnError( "ArchiverException: Error while creating archive", e );
220 }
221 catch ( IOException e )
222 {
223 failOnError( "IOException: Error while creating archive", e );
224 }
225 catch ( RuntimeException e )
226 {
227 failOnError( "RuntimeException: Error while creating archive", e );
228 }
229 }
230 }
231
232
233
234
235
236
237
238
239 protected String getClassifier()
240 {
241 return classifier;
242 }
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257 private File generateArchive( File javadocFiles, String jarFileName )
258 throws ArchiverException, IOException
259 {
260 File javadocJar = new File( jarOutputDirectory, jarFileName );
261
262 if ( javadocJar.exists() )
263 {
264 javadocJar.delete();
265 }
266
267 MavenArchiver archiver = new MavenArchiver();
268 archiver.setArchiver( jarArchiver );
269 archiver.setOutputFile( javadocJar );
270
271 if ( !javadocFiles.exists() )
272 {
273 getLog().warn( "JAR will be empty - no content was marked for inclusion!" );
274 }
275 else
276 {
277 archiver.getArchiver().addDirectory( javadocFiles, DEFAULT_INCLUDES, DEFAULT_EXCLUDES );
278 }
279
280 List<Resource> resources = project.getBuild().getResources();
281
282 for ( Resource r : resources )
283 {
284 if ( r.getDirectory().endsWith( "maven-shared-archive-resources" ) )
285 {
286 archiver.getArchiver().addDirectory( new File( r.getDirectory() ) );
287 }
288 }
289
290 if ( useDefaultManifestFile && defaultManifestFile.exists() && archive.getManifestFile() == null )
291 {
292 getLog().info( "Adding existing MANIFEST to archive. Found under: " + defaultManifestFile.getPath() );
293 archive.setManifestFile( defaultManifestFile );
294 }
295
296 try
297 {
298 archiver.createArchive( session, project, archive );
299 }
300 catch ( ManifestException e )
301 {
302 throw new ArchiverException( "ManifestException: " + e.getMessage(), e );
303 }
304 catch ( DependencyResolutionRequiredException e )
305 {
306 throw new ArchiverException( "DependencyResolutionRequiredException: " + e.getMessage(), e );
307 }
308
309 return javadocJar;
310 }
311 }