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