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 public class JavadocJar
50 extends AbstractJavadocMojo
51 {
52
53 private static final String[] DEFAULT_INCLUDES = new String[] { "**/**" };
54
55
56
57
58
59
60
61
62
63
64 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 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 if ( skip )
157 {
158 getLog().info( "Skipping javadoc generation" );
159 return;
160 }
161
162 File innerDestDir = this.destDir;
163 if ( innerDestDir == null )
164 {
165 innerDestDir = new File( getOutputDirectory() );
166 }
167
168 if ( !( "pom".equals( project.getPackaging().toLowerCase( Locale.ENGLISH ) ) && isAggregator() ) )
169 {
170 ArtifactHandler artifactHandler = project.getArtifact().getArtifactHandler();
171 if ( !"java".equals( artifactHandler.getLanguage() ) )
172 {
173 if ( getLog().isInfoEnabled() )
174 {
175 getLog().info( "Not executing Javadoc as the project is not a Java classpath-capable package" );
176 }
177
178 return;
179 }
180 }
181
182 try
183 {
184 executeReport( Locale.getDefault() );
185
186 if ( innerDestDir.exists() )
187 {
188 File outputFile = generateArchive( innerDestDir, finalName + "-" + getClassifier() + ".jar" );
189
190 if ( !attach )
191 {
192 if ( getLog().isInfoEnabled() )
193 {
194 getLog().info( "NOT adding javadoc to attached artifacts list." );
195 }
196 }
197 else
198 {
199
200
201 projectHelper.attachArtifact( project, "javadoc", getClassifier(), outputFile );
202 }
203 }
204 }
205 catch ( ArchiverException e )
206 {
207 if ( failOnError )
208 {
209 throw new MojoExecutionException( "ArchiverException: Error while creating archive:"
210 + e.getMessage(), e );
211 }
212
213 getLog().error( "ArchiverException: Error while creating archive:" + e.getMessage(), e );
214 }
215 catch ( IOException e )
216 {
217 if ( failOnError )
218 {
219 throw new MojoExecutionException( "IOException: Error while creating archive:" + e.getMessage(), e );
220 }
221
222 getLog().error( "IOException: Error while creating archive:" + e.getMessage(), e );
223 }
224 catch ( MavenReportException e )
225 {
226 if ( failOnError )
227 {
228 throw new MojoExecutionException( "MavenReportException: Error while creating archive:"
229 + e.getMessage(), e );
230 }
231
232 getLog().error( "MavenReportException: Error while creating archive:" + e.getMessage(), e );
233 }
234 catch ( RuntimeException e )
235 {
236 if ( failOnError )
237 {
238 throw e;
239 }
240
241 getLog().error( e.getMessage(), e );
242 }
243 }
244
245
246
247
248
249
250
251
252 protected String getClassifier()
253 {
254 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 File javadocJar = new File( jarOutputDirectory, jarFileName );
274
275 if ( javadocJar.exists() )
276 {
277 javadocJar.delete();
278 }
279
280 MavenArchiver archiver = new MavenArchiver();
281 archiver.setArchiver( jarArchiver );
282 archiver.setOutputFile( javadocJar );
283
284 File contentDirectory = javadocFiles;
285 if ( !contentDirectory.exists() )
286 {
287 getLog().warn( "JAR will be empty - no content was marked for inclusion!" );
288 }
289 else
290 {
291 archiver.getArchiver().addDirectory( contentDirectory, DEFAULT_INCLUDES, DEFAULT_EXCLUDES );
292 }
293
294 List resources = project.getBuild().getResources();
295
296 for ( Iterator i = resources.iterator(); i.hasNext(); )
297 {
298 Resource r = (Resource) i.next();
299 if ( r.getDirectory().endsWith( "maven-shared-archive-resources" ) )
300 {
301 archiver.getArchiver().addDirectory( new File( r.getDirectory() ) );
302 }
303 }
304
305 if ( useDefaultManifestFile && defaultManifestFile.exists() && archive.getManifestFile() == null )
306 {
307 getLog().info( "Adding existing MANIFEST to archive. Found under: " + defaultManifestFile.getPath() );
308 archive.setManifestFile( defaultManifestFile );
309 }
310
311 try
312 {
313
314 archive.setAddMavenDescriptor( false );
315 archiver.createArchive( project, archive );
316 }
317 catch ( ManifestException e )
318 {
319 throw new ArchiverException( "ManifestException: " + e.getMessage(), e );
320 }
321 catch ( DependencyResolutionRequiredException e )
322 {
323 throw new ArchiverException( "DependencyResolutionRequiredException: " + e.getMessage(), e );
324 }
325
326 return javadocJar;
327 }
328 }