GroovyDocJarMojo.java

  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. package org.codehaus.gmavenplus.mojo;

  17. import org.apache.maven.archiver.MavenArchiveConfiguration;
  18. import org.apache.maven.archiver.MavenArchiver;
  19. import org.apache.maven.artifact.DependencyResolutionRequiredException;
  20. import org.apache.maven.plugin.MojoExecutionException;
  21. import org.apache.maven.plugins.annotations.Component;
  22. import org.apache.maven.plugins.annotations.LifecyclePhase;
  23. import org.apache.maven.plugins.annotations.Mojo;
  24. import org.apache.maven.plugins.annotations.Parameter;
  25. import org.apache.maven.plugins.annotations.ResolutionScope;
  26. import org.apache.maven.project.MavenProjectHelper;
  27. import org.codehaus.plexus.archiver.Archiver;
  28. import org.codehaus.plexus.archiver.ArchiverException;
  29. import org.codehaus.plexus.archiver.jar.JarArchiver;
  30. import org.codehaus.plexus.archiver.jar.ManifestException;

  31. import java.io.File;
  32. import java.io.IOException;


  33. /**
  34.  * Create a GroovyDoc jar for the main sources. Note by default this will also invoke the <i>groovydoc</i> goal
  35.  * (unless <code>invokeGroovyDoc</code> is <code>false</code>).
  36.  *
  37.  * @author Keegan Witt
  38.  * @since 1.7.1
  39.  */
  40. @Mojo(name = "groovydoc-jar", defaultPhase = LifecyclePhase.PACKAGE, requiresDependencyResolution = ResolutionScope.COMPILE, threadSafe = true)
  41. public class GroovyDocJarMojo extends GroovyDocMojo {

  42.     /**
  43.      * Specifies the filename that will be used for the generated jar file. Please note that <code>-groovydoc</code>
  44.      * will be appended to the file name.
  45.      */
  46.     @Parameter(property = "project.build.finalName")
  47.     protected String finalName;

  48.     /**
  49.      * Specifies the directory where the generated jar file will be put.
  50.      */
  51.     @Parameter(property = "project.build.directory")
  52.     protected String jarOutputDirectory;

  53.     /**
  54.      * The Jar archiver.
  55.      */
  56.     @Component(role = Archiver.class, hint = "jar")
  57.     protected JarArchiver jarArchiver;

  58.     /**
  59.      * The archive configuration to use.
  60.      * See <a href="http://maven.apache.org/shared/maven-archiver/index.html">Maven Archiver Reference</a>.
  61.      */
  62.     @Parameter
  63.     protected final MavenArchiveConfiguration archive = new MavenArchiveConfiguration();

  64.     /**
  65.      * Specifies whether to attach the generated artifact to the project helper.
  66.      */
  67.     @Parameter(property = "attach", defaultValue = "true")
  68.     protected boolean attach;

  69.     /**
  70.      * Used for attaching the artifact in the project.
  71.      */
  72.     @Component
  73.     private MavenProjectHelper projectHelper;

  74.     /**
  75.      * Path to the default MANIFEST file to use. It will be used if
  76.      * <code>useDefaultManifestFile</code> is set to <code>true</code>.
  77.      */
  78.     @Parameter(defaultValue = "${project.build.outputDirectory}/META-INF/MANIFEST.MF", required = true, readonly = true)
  79.     protected File defaultManifestFile;

  80.     /**
  81.      * Set this to <code>true</code> to enable the use of the <code>defaultManifestFile</code>.
  82.      */
  83.     @Parameter(defaultValue = "false")
  84.     private boolean useDefaultManifestFile;

  85.     /**
  86.      * The classifier for the GroovyDoc jar.
  87.      */
  88.     @Parameter(defaultValue = "groovydoc")
  89.     private String classifier;

  90.     /**
  91.      * The artifact type for the GroovyDoc jar.
  92.      *
  93.      * @since 1.10.0
  94.      */
  95.     @Parameter(defaultValue = "javadoc")
  96.     private String artifactType;

  97.     /**
  98.      * Whether to invoke the <code>groovydoc</code> goal before creating jar.
  99.      */
  100.     @Parameter(defaultValue = "true")
  101.     protected boolean invokeGroovyDoc;

  102.     /**
  103.      * Executes this mojo.
  104.      */
  105.     @Override
  106.     public void execute() throws MojoExecutionException {
  107.         if (invokeGroovyDoc) {
  108.             // invoke the GroovyDoc mojo
  109.             super.execute();
  110.         }

  111.         try {
  112.             File outputFile = generateArchive(groovyDocOutputDirectory, finalName + "-" + classifier + ".jar");

  113.             if (attach) {
  114.                 projectHelper.attachArtifact(project, artifactType, classifier, outputFile);
  115.             } else {
  116.                 getLog().info("Not adding GroovyDoc jar to attached artifacts list.");
  117.             }
  118.         } catch (ArchiverException e) {
  119.             throw new MojoExecutionException("ArchiverException: Error while creating archive", e);
  120.         } catch (IOException e) {
  121.             throw new MojoExecutionException("IOException: Error while creating archive", e);
  122.         } catch (RuntimeException e) {
  123.             throw new MojoExecutionException("RuntimeException: Error while creating archive", e);
  124.         }
  125.     }

  126.     /**
  127.      * Method that creates the jar file
  128.      *
  129.      * @param groovydocFiles the directory where the generated jar file will be put
  130.      * @param jarFileName    the filename of the generated jar file
  131.      * @return a File object that contains the generated jar file
  132.      * @throws ArchiverException When an issue occurs preventing Maven Archiver from creating the jar file
  133.      * @throws IOException       When an IO issue occurs preventing Maven Archiver from creating the jar file
  134.      */
  135.     protected File generateArchive(File groovydocFiles, String jarFileName) throws ArchiverException, IOException {
  136.         File groovydocJar = new File(jarOutputDirectory, jarFileName);

  137.         if (groovydocJar.exists()) {
  138.             if (!groovydocJar.delete()) {
  139.                 getLog().warn("Unable to delete " + groovydocJar.getAbsolutePath());
  140.             }
  141.         }

  142.         MavenArchiver archiver = new MavenArchiver();
  143.         archiver.setArchiver(jarArchiver);
  144.         archiver.setOutputFile(groovydocJar);

  145.         if (!groovydocFiles.exists()) {
  146.             getLog().warn("JAR will be empty - no content was marked for inclusion!");
  147.         } else {
  148.             archiver.getArchiver().addDirectory(groovydocFiles);
  149.         }

  150.         if (useDefaultManifestFile && defaultManifestFile.exists() && archive.getManifestFile() == null) {
  151.             getLog().info("Adding existing MANIFEST to archive. Found under: " + defaultManifestFile.getPath());
  152.             archive.setManifestFile(defaultManifestFile);
  153.         }

  154.         try {
  155.             archiver.createArchive(session, project, archive);
  156.         } catch (ManifestException e) {
  157.             throw new ArchiverException("ManifestException: " + e.getMessage(), e);
  158.         } catch (DependencyResolutionRequiredException e) {
  159.             throw new ArchiverException("DependencyResolutionRequiredException: " + e.getMessage(), e);
  160.         }

  161.         return groovydocJar;
  162.     }

  163. }