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