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