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