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

net.dubboclub.maven.jar.AbstractJarMojo Maven / Gradle / Ivy

The newest version!
package net.dubboclub.maven.jar;

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import net.dubboclub.maven.compiler.CompilerMojo;
import org.apache.maven.archiver.MavenArchiveConfiguration;
import org.apache.maven.archiver.MavenArchiver;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.MavenProjectHelper;
import org.codehaus.plexus.archiver.Archiver;
import org.codehaus.plexus.archiver.jar.JarArchiver;

import java.io.File;

/**
 * Base class for creating a jar from project classes.
 *
 * @author Emmanuel Venisse
 * @version $Id$
 */
public abstract class AbstractJarMojo
    extends CompilerMojo
{

    private static final String[] DEFAULT_EXCLUDES = new String[] { "**/package.html" };

    private static final String[] DEFAULT_INCLUDES = new String[] { "**/**" };

    /**
     * List of files to include. Specified as fileset patterns which are relative to the input directory whose contents
     * is being packaged into the JAR.
     */
    @Parameter
    private String[] includes;

    /**
     * List of files to exclude. Specified as fileset patterns which are relative to the input directory whose contents
     * is being packaged into the JAR.
     */
    @Parameter
    private String[] excludes;


    /**
     * Name of the generated JAR.
* Starting with 3.0.0 the property has been renamed from jar.finalName to * maven.jar.finalName. */ @Parameter( property = "maven.jar.finalName", defaultValue = "${project.build.finalName}" ) private String finalName; /** * The Jar archiver. */ @Component( role = Archiver.class, hint = "jar" ) private JarArchiver jarArchiver; @Parameter(defaultValue = "${project.build.directory}", required = true, readonly = true) protected File targetOutputDirectory; /** * The archive configuration to use. See Maven * Archiver Reference. */ @Parameter private MavenArchiveConfiguration archive = new MavenArchiveConfiguration(); /** * Using this property will fail your build cause it has been removed from the plugin configuration. See the * Major Version Upgrade to version 3.0.0 for the * plugin. * * @deprecated For version 3.0.0 this parameter is only defined here to break the build if you use it! */ @Parameter( property = "jar.useDefaultManifestFile", defaultValue = "false" ) private boolean useDefaultManifestFile; /** * */ @Component private MavenProjectHelper projectHelper; /** * Require the jar plugin to build a new JAR even if none of the contents appear to have changed. By default, this * plugin looks to see if the output jar exists and inputs have not changed. If these conditions are true, the * plugin skips creation of the jar. This does not work when other plugins, like the maven-shade-plugin, are * configured to post-process the jar. This plugin can not detect the post-processing, and so leaves the * post-processed jar in place. This can lead to failures when those plugins do not expect to find their own output * as an input. Set this parameter to true to avoid these problems by forcing this plugin to recreate the * jar every time.
* Starting with 3.0.0 the property has been renamed from jar.forceCreation to * maven.jar.forceCreation. */ @Parameter( property = "maven.jar.forceCreation", defaultValue = "false" ) private boolean forceCreation; /** * Skip creating empty archives.
* Starting with 3.0.0 the property has been renamed from jar.skipIfEmpty to * maven.jar.skipIfEmpty. */ @Parameter( property = "maven.jar.skipIfEmpty", defaultValue = "false" ) private boolean skipIfEmpty; /** * Return the specific output directory to serve as the root for the archive. */ protected abstract File getClassesDirectory(); /** * Overload this to produce a jar with another classifier, for example a test-jar. */ protected abstract String getClassifier(); /** * Overload this to produce a test-jar, for example. */ protected abstract String getType(); /** * Returns the Jar file to generate, based on an optional classifier. * * @param basedir the output directory * @param finalName the name of the ear file * @param classifier an optional classifier * @return the file to generate */ protected File getJarFile( File basedir, String finalName, String classifier ) { if ( basedir == null ) { throw new IllegalArgumentException( "basedir is not allowed to be null" ); } if ( finalName == null ) { throw new IllegalArgumentException( "finalName is not allowed to be null" ); } StringBuilder fileName = new StringBuilder( finalName ); if ( hasClassifier() ) { fileName.append( "-" ).append( classifier ); } fileName.append( ".jar" ); return new File( basedir, fileName.toString() ); } /** * Generates the JAR. */ public File createArchive() throws MojoExecutionException { File jarFile = getJarFile( targetOutputDirectory, finalName, getClassifier() ); MavenArchiver archiver = new MavenArchiver(); archiver.setArchiver( jarArchiver ); archiver.setOutputFile( jarFile ); archive.setForced( forceCreation ); try { File contentDirectory = getClassesDirectory(); if ( !contentDirectory.exists() ) { getLog().warn( "JAR will be empty - no content was marked for inclusion!" ); } else { archiver.getArchiver().addDirectory( contentDirectory, getIncludes(), getExcludes() ); } archiver.createArchive( session, project, archive ); return jarFile; } catch ( Exception e ) { // TODO: improve error handling throw new MojoExecutionException( "Error assembling JAR", e ); } } /** * Generates the JAR. */ public void jar() throws MojoExecutionException { if ( useDefaultManifestFile ) { throw new MojoExecutionException( "You are using 'useDefaultManifestFile' which has been removed" + " from the maven-jar-plugin. " + "Please see the >>Major Version Upgrade to version 3.0.0<< on the plugin site." ); } if ( skipIfEmpty && ( !getClassesDirectory().exists() || getClassesDirectory().list().length < 1 ) ) { getLog().info( "Skipping packaging of the " + getType() ); } else { File jarFile = createArchive(); if ( hasClassifier() ) { projectHelper.attachArtifact( project, getType(), getClassifier(), jarFile ); } else { if ( projectHasAlreadySetAnArtifact() ) { throw new MojoExecutionException( "You have to use a classifier " + "to attach supplemental artifacts to the project instead of replacing them." ); } project.getArtifact().setFile( jarFile ); } } } private boolean projectHasAlreadySetAnArtifact() { if ( project.getArtifact().getFile() != null ) { return project.getArtifact().getFile().isFile(); } else { return false; } } /** * @return true in case where the classifier is not {@code null} and contains something else than white spaces. */ protected boolean hasClassifier() { boolean result = false; if ( getClassifier() != null && getClassifier().trim().length() > 0 ) { result = true; } return result; } private String[] getIncludes() { if ( includes != null && includes.length > 0 ) { return includes; } return DEFAULT_INCLUDES; } private String[] getExcludes() { if ( excludes != null && excludes.length > 0 ) { return excludes; } return DEFAULT_EXCLUDES; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy