All Downloads are FREE. Search and download functionalities are using the official Maven repository.

scala_maven.ScalaDocJarMojo Maven / Gradle / Ivy

Go to download

The scala-maven-plugin (previously maven-scala-plugin) is used for compiling/testing/running/documenting scala code of any maven project.

There is a newer version: 4.9.2
Show newest version
package scala_maven;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Locale;

import org.apache.maven.archiver.MavenArchiveConfiguration;
import org.apache.maven.archiver.MavenArchiver;
import org.apache.maven.artifact.DependencyResolutionRequiredException;
import org.apache.maven.model.Resource;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProjectHelper;
import org.apache.maven.reporting.MavenReportException;
import org.codehaus.plexus.archiver.ArchiverException;
import org.codehaus.plexus.archiver.jar.JarArchiver;
import org.codehaus.plexus.archiver.jar.ManifestException;

/**
 * Creates a jar of the non-aggregated scaladoc and attaches it
 * to the project for distribution.
 * @goal doc-jar
 * @phase package
 *
 */
public class ScalaDocJarMojo extends ScalaDocMojo {

  private static final String[] DEFAULT_INCLUDES = new String[] { "**/**" };
  private static final String[] DEFAULT_EXCLUDES = new String[] {};
  /**
   * The Jar archiver.
   *
   * @component role="org.codehaus.plexus.archiver.Archiver" roleHint="jar"
   */
  private JarArchiver jarArchiver;

  /**
  * Used for attaching the artifact in the project.
  *
  * @component
  */
  private MavenProjectHelper projectHelper;
  /**
   * Specifies the filename that will be used for the generated jar file. Please note that -javadoc
   * or -test-javadoc will be appended to the file name.
   *
   * @parameter property="project.build.finalName"
   */
  private String finalName;
  /**
   * Specifies whether to attach the generated artifact to the project helper.
   * 
* * @parameter property="attach" default-value="true" */ private boolean attach; /** * Specifies the classifier of the generated artifact. * * @parameter property="classifier" default-value="javadoc" */ private String classifier; /** * Specifies whether to skip generating scaladoc. *
* * @parameter property="skip" default-value="false" */ private boolean skip; /** * Specifies the directory where the generated jar file will be put. * * @parameter property="project.build.directory" */ private String jarOutputDirectory; /** * The archive configuration to use. * See Maven Archiver Reference. * * @parameter */ private MavenArchiveConfiguration archive = new MavenArchiveConfiguration(); /** * Path to the default MANIFEST file to use. It will be used if * useDefaultManifestFile is set to true. * * @parameter default-value="${project.build.outputDirectory}/META-INF/MANIFEST.MF" * @required * @readonly */ private File defaultManifestFile; /** * Set this to true to enable the use of the defaultManifestFile. *
* * @parameter default-value="false" */ private boolean useDefaultManifestFile; /** * Specifies if the build will fail if there are errors during javadoc execution or not. * * @parameter property="maven.javadoc.failOnError" default-value="true" * @since 2.5 */ protected boolean failOnError; @Override public void doExecute() throws Exception { if(skip) { getLog().info( "Skipping javadoc generation" ); return; } try { generate(null, Locale.getDefault()); if(reportOutputDirectory.exists()) { File outputFile = generateArchive( reportOutputDirectory, finalName + "-" + getClassifier() + ".jar" ); if(!attach ) { getLog().info( "NOT adding javadoc to attached artifacts list." ); } else { // TODO: these introduced dependencies on the project are going to become problematic - can we export it // through metadata instead? projectHelper.attachArtifact( project, "javadoc", getClassifier(), outputFile ); } } } catch (ArchiverException e) { failOnError( "ArchiverException: Error while creating archive", e ); } catch (IOException e) { failOnError( "IOException: Error while creating archive", e ); } catch ( MavenReportException e ) { failOnError( "MavenReportException: Error while creating archive", e ); } catch ( RuntimeException e ) { failOnError( "RuntimeException: Error while creating archive", e ); } } /** * Method that creates the jar file * * @param javadocFiles the directory where the generated jar file will be put * @param jarFileName the filename of the generated jar file * @return a File object that contains the generated jar file * @throws ArchiverException * @throws IOException */ private File generateArchive( File javadocFiles, String jarFileName ) throws ArchiverException, IOException { final File javadocJar = new File( jarOutputDirectory, jarFileName ); if(javadocJar.exists()) { javadocJar.delete(); } MavenArchiver archiver = new MavenArchiver(); archiver.setArchiver( jarArchiver ); archiver.setOutputFile( javadocJar ); File contentDirectory = javadocFiles; if(!contentDirectory.exists()) { getLog().warn( "JAR will be empty - no content was marked for inclusion!" ); } else { archiver.getArchiver().addDirectory( contentDirectory, DEFAULT_INCLUDES, DEFAULT_EXCLUDES ); } List resources = project.getBuild().getResources(); for ( Resource r : resources ) { if ( r.getDirectory().endsWith( "maven-shared-archive-resources" ) ) { archiver.getArchiver().addDirectory( new File( r.getDirectory() ) ); } } if ( useDefaultManifestFile && defaultManifestFile.exists() && archive.getManifestFile() == null ) { getLog().info("Adding existing MANIFEST to archive. Found under: " + defaultManifestFile.getPath()); archive.setManifestFile(defaultManifestFile); } try { // we don't want Maven stuff archive.setAddMavenDescriptor( false ); archiver.createArchive( project, archive ); } catch ( ManifestException e ) { throw new ArchiverException( "ManifestException: " + e.getMessage(), e ); } catch ( DependencyResolutionRequiredException e ) { throw new ArchiverException( "DependencyResolutionRequiredException: " + e.getMessage(), e ); } return javadocJar; } protected String getClassifier() { return classifier; } protected void failOnError(String prefix, Exception e) throws MojoExecutionException { if (failOnError) { if (e instanceof RuntimeException) { throw (RuntimeException) e; } throw new MojoExecutionException(prefix + ": " + e.getMessage(), e); } getLog().error(prefix + ": " + e.getMessage(), e); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy