1 | |
package org.apache.maven.plugin.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 | |
|
52 | |
@Mojo( name = "jar", defaultPhase = LifecyclePhase.PACKAGE, requiresDependencyResolution = ResolutionScope.COMPILE, threadSafe = true ) |
53 | 4 | public class JavadocJar |
54 | |
extends AbstractJavadocMojo |
55 | |
{ |
56 | |
|
57 | |
|
58 | |
|
59 | 2 | private static final String[] DEFAULT_INCLUDES = new String[]{ "**/**" }; |
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | 2 | 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 | |
@Parameter( property = "destDir" ) |
103 | |
private File destDir; |
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
@Parameter( property = "project.build.directory" ) |
109 | |
private String jarOutputDirectory; |
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
@Parameter( property = "project.build.finalName" ) |
116 | |
private String finalName; |
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
@Parameter( property = "attach", defaultValue = "true" ) |
123 | |
private boolean attach; |
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | 4 | @Parameter |
132 | |
private MavenArchiveConfiguration archive = new MavenArchiveConfiguration(); |
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
@Parameter( defaultValue = "${project.build.outputDirectory}/META-INF/MANIFEST.MF", required = true, |
141 | |
readonly = true ) |
142 | |
private File defaultManifestFile; |
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
@Parameter( defaultValue = "false" ) |
151 | |
private boolean useDefaultManifestFile; |
152 | |
|
153 | |
|
154 | |
public void execute() |
155 | |
throws MojoExecutionException |
156 | |
{ |
157 | 4 | if ( skip ) |
158 | |
{ |
159 | 0 | getLog().info( "Skipping javadoc generation" ); |
160 | 0 | return; |
161 | |
} |
162 | |
|
163 | 4 | File innerDestDir = this.destDir; |
164 | 4 | if ( innerDestDir == null ) |
165 | |
{ |
166 | 2 | innerDestDir = new File( getOutputDirectory() ); |
167 | |
} |
168 | |
|
169 | 4 | if ( !( "pom".equalsIgnoreCase( project.getPackaging() ) && isAggregator() ) ) |
170 | |
{ |
171 | 4 | ArtifactHandler artifactHandler = project.getArtifact().getArtifactHandler(); |
172 | 4 | if ( !"java".equals( artifactHandler.getLanguage() ) ) |
173 | |
{ |
174 | 0 | getLog().info( "Not executing Javadoc as the project is not a Java classpath-capable package" ); |
175 | 0 | return; |
176 | |
} |
177 | |
} |
178 | |
|
179 | |
try |
180 | |
{ |
181 | 4 | executeReport( Locale.getDefault() ); |
182 | |
|
183 | 4 | if ( innerDestDir.exists() ) |
184 | |
{ |
185 | 2 | File outputFile = generateArchive( innerDestDir, finalName + "-" + getClassifier() + ".jar" ); |
186 | |
|
187 | 2 | if ( !attach ) |
188 | |
{ |
189 | 0 | getLog().info( "NOT adding javadoc to attached artifacts list." ); |
190 | |
} |
191 | |
else |
192 | |
{ |
193 | |
|
194 | |
|
195 | 2 | projectHelper.attachArtifact( project, "javadoc", getClassifier(), outputFile ); |
196 | |
} |
197 | |
} |
198 | |
} |
199 | 0 | catch ( ArchiverException e ) |
200 | |
{ |
201 | 0 | failOnError( "ArchiverException: Error while creating archive", e ); |
202 | |
} |
203 | 0 | catch ( IOException e ) |
204 | |
{ |
205 | 0 | failOnError( "IOException: Error while creating archive", e ); |
206 | |
} |
207 | 0 | catch ( MavenReportException e ) |
208 | |
{ |
209 | 0 | failOnError( "MavenReportException: Error while creating archive", e ); |
210 | |
} |
211 | 0 | catch ( RuntimeException e ) |
212 | |
{ |
213 | 0 | failOnError( "RuntimeException: Error while creating archive", e ); |
214 | 4 | } |
215 | 4 | } |
216 | |
|
217 | |
|
218 | |
|
219 | |
|
220 | |
|
221 | |
|
222 | |
|
223 | |
|
224 | |
protected String getClassifier() |
225 | |
{ |
226 | 4 | return "javadoc"; |
227 | |
} |
228 | |
|
229 | |
|
230 | |
|
231 | |
|
232 | |
|
233 | |
|
234 | |
|
235 | |
|
236 | |
|
237 | |
|
238 | |
|
239 | |
|
240 | |
|
241 | |
|
242 | |
private File generateArchive( File javadocFiles, String jarFileName ) |
243 | |
throws ArchiverException, IOException |
244 | |
{ |
245 | 2 | File javadocJar = new File( jarOutputDirectory, jarFileName ); |
246 | |
|
247 | 2 | if ( javadocJar.exists() ) |
248 | |
{ |
249 | 2 | javadocJar.delete(); |
250 | |
} |
251 | |
|
252 | 2 | MavenArchiver archiver = new MavenArchiver(); |
253 | 2 | archiver.setArchiver( jarArchiver ); |
254 | 2 | archiver.setOutputFile( javadocJar ); |
255 | |
|
256 | 2 | File contentDirectory = javadocFiles; |
257 | 2 | if ( !contentDirectory.exists() ) |
258 | |
{ |
259 | 0 | getLog().warn( "JAR will be empty - no content was marked for inclusion!" ); |
260 | |
} |
261 | |
else |
262 | |
{ |
263 | 2 | archiver.getArchiver().addDirectory( contentDirectory, DEFAULT_INCLUDES, DEFAULT_EXCLUDES ); |
264 | |
} |
265 | |
|
266 | 2 | List<Resource> resources = project.getBuild().getResources(); |
267 | |
|
268 | 2 | for ( Resource r : resources ) |
269 | |
{ |
270 | 0 | if ( r.getDirectory().endsWith( "maven-shared-archive-resources" ) ) |
271 | |
{ |
272 | 0 | archiver.getArchiver().addDirectory( new File( r.getDirectory() ) ); |
273 | |
} |
274 | |
} |
275 | |
|
276 | 2 | if ( useDefaultManifestFile && defaultManifestFile.exists() && archive.getManifestFile() == null ) |
277 | |
{ |
278 | 0 | getLog().info( "Adding existing MANIFEST to archive. Found under: " + defaultManifestFile.getPath() ); |
279 | 0 | archive.setManifestFile( defaultManifestFile ); |
280 | |
} |
281 | |
|
282 | |
try |
283 | |
{ |
284 | |
|
285 | 2 | archive.setAddMavenDescriptor( false ); |
286 | 2 | archiver.createArchive( project, archive ); |
287 | |
} |
288 | 0 | catch ( ManifestException e ) |
289 | |
{ |
290 | 0 | throw new ArchiverException( "ManifestException: " + e.getMessage(), e ); |
291 | |
} |
292 | 0 | catch ( DependencyResolutionRequiredException e ) |
293 | |
{ |
294 | 0 | throw new ArchiverException( "DependencyResolutionRequiredException: " + e.getMessage(), e ); |
295 | 2 | } |
296 | |
|
297 | 2 | return javadocJar; |
298 | |
} |
299 | |
} |