View Javadoc
1   package org.codehaus.gmavenplus.mojo;
2   
3   import org.apache.maven.archiver.MavenArchiveConfiguration;
4   import org.apache.maven.archiver.MavenArchiver;
5   import org.apache.maven.artifact.DependencyResolutionRequiredException;
6   import org.apache.maven.plugin.MojoExecutionException;
7   import org.apache.maven.plugins.annotations.Component;
8   import org.apache.maven.plugins.annotations.LifecyclePhase;
9   import org.apache.maven.plugins.annotations.Mojo;
10  import org.apache.maven.plugins.annotations.Parameter;
11  import org.apache.maven.plugins.annotations.ResolutionScope;
12  import org.apache.maven.project.MavenProjectHelper;
13  import org.codehaus.plexus.archiver.Archiver;
14  import org.codehaus.plexus.archiver.ArchiverException;
15  import org.codehaus.plexus.archiver.jar.JarArchiver;
16  import org.codehaus.plexus.archiver.jar.ManifestException;
17  
18  import java.io.File;
19  import java.io.IOException;
20  
21  
22  /**
23   * Create a GroovyDoc jar for the main sources. Note by default this will also invoke the <i>groovydoc</i> goal
24   * (unless <code>invokeGroovyDoc</code> is <code>false</code>).
25   *
26   * @author Keegan Witt
27   * @since 1.7.1
28   */
29  @Mojo(name = "groovydoc-jar", defaultPhase = LifecyclePhase.PACKAGE, requiresDependencyResolution = ResolutionScope.COMPILE, threadSafe = true)
30  public class GroovyDocJarMojo extends GroovyDocMojo {
31  
32      /**
33       * Specifies the filename that will be used for the generated jar file. Please note that <code>-groovydoc</code>
34       * will be appended to the file name.
35       */
36      @Parameter(property = "project.build.finalName")
37      protected String finalName;
38  
39      /**
40       * Specifies the directory where the generated jar file will be put.
41       */
42      @Parameter(property = "project.build.directory")
43      protected String jarOutputDirectory;
44  
45      /**
46       * The Jar archiver.
47       */
48      @Component(role = Archiver.class, hint = "jar")
49      protected JarArchiver jarArchiver;
50  
51      /**
52       * The archive configuration to use.
53       * See <a href="http://maven.apache.org/shared/maven-archiver/index.html">Maven Archiver Reference</a>.
54       */
55      @Parameter
56      protected final MavenArchiveConfiguration archive = new MavenArchiveConfiguration();
57  
58      /**
59       * Specifies whether to attach the generated artifact to the project helper.
60       */
61      @Parameter(property = "attach", defaultValue = "true")
62      protected boolean attach;
63  
64      /**
65       * Used for attaching the artifact in the project.
66       */
67      @Component
68      private MavenProjectHelper projectHelper;
69  
70      /**
71       * Path to the default MANIFEST file to use. It will be used if
72       * <code>useDefaultManifestFile</code> is set to <code>true</code>.
73       */
74      @Parameter(defaultValue = "${project.build.outputDirectory}/META-INF/MANIFEST.MF", required = true, readonly = true)
75      protected File defaultManifestFile;
76  
77      /**
78       * Set this to <code>true</code> to enable the use of the <code>defaultManifestFile</code>.
79       */
80      @Parameter(defaultValue = "false")
81      private boolean useDefaultManifestFile;
82  
83      /**
84       * The classifier for the GroovyDoc jar.
85       */
86      @Parameter(defaultValue = "groovydoc")
87      private String classifier;
88  
89      /**
90       * The artifact type for the GroovyDoc jar.
91       *
92       * @since 1.10.0
93       */
94      @Parameter(defaultValue = "javadoc")
95      private String artifactType;
96  
97      /**
98       * Whether to invoke the <code>groovydoc</code> goal before creating jar.
99       */
100     @Parameter(defaultValue = "true")
101     protected boolean invokeGroovyDoc;
102 
103     /**
104      * Executes this mojo.
105      */
106     @Override
107     public void execute() throws MojoExecutionException {
108         if (invokeGroovyDoc) {
109             // invoke the GroovyDoc mojo
110             super.execute();
111         }
112 
113         try {
114             File outputFile = generateArchive(groovyDocOutputDirectory, finalName + "-" + classifier + ".jar");
115 
116             if (attach) {
117                 projectHelper.attachArtifact(project, artifactType, classifier, outputFile);
118             } else {
119                 getLog().info("Not adding GroovyDoc jar to attached artifacts list.");
120             }
121         } catch (ArchiverException e) {
122             throw new MojoExecutionException("ArchiverException: Error while creating archive", e);
123         } catch (IOException e) {
124             throw new MojoExecutionException("IOException: Error while creating archive", e);
125         } catch (RuntimeException e) {
126             throw new MojoExecutionException("RuntimeException: Error while creating archive", e);
127         }
128     }
129 
130     /**
131      * Method that creates the jar file
132      *
133      * @param groovydocFiles the directory where the generated jar file will be put
134      * @param jarFileName    the filename of the generated jar file
135      * @return a File object that contains the generated jar file
136      * @throws ArchiverException When an issue occurs preventing Maven Archiver from creating the jar file
137      * @throws IOException       When an IO issue occurs preventing Maven Archiver from creating the jar file
138      */
139     protected File generateArchive(File groovydocFiles, String jarFileName) throws ArchiverException, IOException {
140         File groovydocJar = new File(jarOutputDirectory, jarFileName);
141 
142         if (groovydocJar.exists()) {
143             if (!groovydocJar.delete()) {
144                 getLog().warn("Unable to delete " + groovydocJar.getAbsolutePath());
145             }
146         }
147 
148         MavenArchiver archiver = new MavenArchiver();
149         archiver.setArchiver(jarArchiver);
150         archiver.setOutputFile(groovydocJar);
151 
152         if (!groovydocFiles.exists()) {
153             getLog().warn("JAR will be empty - no content was marked for inclusion!");
154         } else {
155             archiver.getArchiver().addDirectory(groovydocFiles);
156         }
157 
158         if (useDefaultManifestFile && defaultManifestFile.exists() && archive.getManifestFile() == null) {
159             getLog().info("Adding existing MANIFEST to archive. Found under: " + defaultManifestFile.getPath());
160             archive.setManifestFile(defaultManifestFile);
161         }
162 
163         try {
164             archiver.createArchive(session, project, archive);
165         } catch (ManifestException e) {
166             throw new ArchiverException("ManifestException: " + e.getMessage(), e);
167         } catch (DependencyResolutionRequiredException e) {
168             throw new ArchiverException("DependencyResolutionRequiredException: " + e.getMessage(), e);
169         }
170 
171         return groovydocJar;
172     }
173 
174 }