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

com.github.jlgrock.javascriptframework.jsar.JsarMojo Maven / Gradle / Ivy

The newest version!
package com.github.jlgrock.javascriptframework.jsar;

import java.io.File;

import org.apache.log4j.Logger;
import org.apache.maven.archiver.MavenArchiveConfiguration;
import org.apache.maven.archiver.MavenArchiver;
import org.apache.maven.artifact.DependencyResolutionRequiredException;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.*;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.MavenProjectHelper;
import org.codehaus.plexus.archiver.util.DefaultFileSet;

import com.github.jlgrock.javascriptframework.mavenutils.mavenobjects.JsarRelativeLocations;

/**
 * Build a JSAR package from the current project.
 * 
 * This is essentially a modified JAR plugin, since Plexus offers no way to
 * extend classes from other packages from maven dependencies, this is copied
 * over in its entirety and edited.
 */
@Mojo( name = "jsar",
        defaultPhase = LifecyclePhase.PACKAGE,
        requiresProject = true,
        threadSafe = true,
        requiresDependencyResolution = ResolutionScope.RUNTIME)
public class JsarMojo extends AbstractMojo {
	/**
	 * The Logger.
	 */
	private static final Logger LOGGER = Logger.getLogger(JsarMojo.class);

	/**
	 * The directory to place compiled files into.
	 */
    @Parameter( defaultValue = "${project.build.directory}${file.separator}javascriptFramework" )
    private File frameworkTargetDirectory;

	/**
	 * Classifier to add to the artifact generated. If given, the artifact will
	 * be an attachment instead.
	 */
    @Parameter
	private String classifier;

	/**
	 * @return the classifier
	 */
	protected final String getClassifier() {
		return classifier;
	}

	/**
	 * @return type of the generated artifact
	 */
	protected final String getType() {
		return "jsar";
	}

	/**
	 * The default file excludes array.
	 */
	private static final String[] DEFAULT_EXCLUDES = new String[] { "**/package.html" };

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

	/**
	 * Directory containing the generated JSAR.
	 */
    @Parameter( defaultValue = "${project.build.directory}", required = true )
	private File outputDirectory;

	/**
	 * Name of the generated JSAR.
	 */
    @Parameter( defaultValue = "${project.build.finalName}", required = true )
	private String finalName;

	/**
	 * The JSAR archiver.
	 */
    @Component(role = org.codehaus.plexus.archiver.Archiver.class, hint="javascript")
	private JavascriptArchiver jsarArchiver;

	/**
	 * The Maven project.
	 */
    @Parameter( defaultValue = "${project}", readonly = true )
	private MavenProject project;

    /**
     * The Maven Session Object.
     */
    @Parameter( defaultValue = "${session}", required = true, readonly = true )
    protected MavenSession session;

    /**
	 * 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.
     *
	 * @since 2.2
	 */
    @Parameter(
            defaultValue = "${project.build.outputDirectory}/META-INF/MANIFEST.MF",
            required = true,
            readonly = true)
	private File defaultManifestFile;

	/**
	 * Set this to true to enable the use of the
	 * defaultManifestFile.
	 * 
	 * @since 2.2
	 */
    @Parameter( defaultValue = "false" )
	private boolean useDefaultManifestFile;

	/**
	 * The project helper.
	 */
    @Component
	private MavenProjectHelper projectHelper;

	/**
	 * Whether creating the archive should be forced.
	 */
    @Parameter( defaultValue = "false" )
	private boolean forceCreation;

	/**
	 * This will default to compiledFilename + "-debug" if not overridden.
	 */
    @Parameter( defaultValue = "${project.build.finalName}-debug.js" )
	private String debugFilename;

	/**
	 * @return project
	 */
	protected final MavenProject getProject() {
		return project;
	}

	/**
	 * Get the archive file.
	 * 
	 * @param basedir
	 *            the base directory
	 * @param finalName
	 *            the name to give the created archive
	 * @param classifier
	 *            the classifier to match against
	 * @return the archive file
	 */
	protected static File getJsarFile(final File basedir,
			final String finalName, final String classifier) {
		String classifierOut = classifier;
		if (classifier == null) {
			classifierOut = "";
		} else if (classifier.trim().length() > 0
				&& !classifier.startsWith("-")) {
			classifierOut = "-" + classifier;
		}

		return new File(basedir, finalName + classifierOut + ".jsar");
	}

	/**
	 * Default Manifest location. Can point to a non existing file. Cannot
	 * return null.
	 * 
	 * @return the default mainifest file
	 */
	protected final File getDefaultManifestFile() {
		return defaultManifestFile;
	}

	/**
	 * Generates the JSAR.
	 * 
	 * @return the archive File
	 * @throws MojoExecutionException
	 *             if there is an exception of any kind
	 */
	public final File createArchive() throws MojoExecutionException {
		File jsarFile = getJsarFile(outputDirectory, finalName, getClassifier());

		MavenArchiver archiver = new MavenArchiver();
		archiver.setArchiver(jsarArchiver);
		archiver.setOutputFile(jsarFile);
		archive.setForced(forceCreation);

		try {
            // Add all output code
            File compiledDirectory = JsarRelativeLocations
                    .getOutputLocation(getFrameworkTargetDirectory());

            //Add files, minus exclusions (such as externs)
            DefaultFileSet fs = new DefaultFileSet();
            fs.setDirectory(compiledDirectory);
            fs.setExcludes(getExcludes());
            archiver.getArchiver().addFileSet(fs);

            // add manifest
            File existingManifest = getDefaultManifestFile();

            if (useDefaultManifestFile && existingManifest.exists()
                    && archive.getManifestFile() == null) {
                LOGGER.info("Adding existing MANIFEST to archive. Found under: "
                        + existingManifest.getPath());
                archive.setManifestFile(existingManifest);
            }
            archiver.createArchive(session, project, archive);

            return jsarFile;
        } catch ( DependencyResolutionRequiredException e ) {
            throw new MojoExecutionException("Could not create classes archive", e);
        } catch (Exception e) {
			throw new MojoExecutionException("Error assembling JSAR", e);
		}
	}

	@Override
	public final void execute() throws MojoExecutionException,
			MojoFailureException {
		File jsarFile = createArchive();

		if (classifier != null) {
			projectHelper.attachArtifact(getProject(), getType(), classifier,
					jsarFile);
		} else {
			getProject().getArtifact().setFile(jsarFile);
		}
	}

	/**
	 * @return the array of excludes, or the default excludes if they have not
	 *         been set.
	 */
	private String[] getExcludes() {
		if (excludes != null && excludes.length > 0) {
			return excludes;
		}
		return DEFAULT_EXCLUDES;
	}

	/**
	 * @return the debugFilename
	 */
	public final String getDebugFilename() {
		return debugFilename;
	}

	/**
	 * @param debugFilenameIn
	 *            the debugFilename to set
	 */
	public final void setDebugFilename(final String debugFilenameIn) {
		this.debugFilename = debugFilenameIn;
	}

	/**
	 * @return the frameworkTargetDirectory
	 */
	public final File getFrameworkTargetDirectory() {
		return frameworkTargetDirectory;
	}

	/**
	 * @param frameworkTargetDirectoryIn
	 *            the frameworkTargetDirectory to set
	 */
	public final void setFrameworkTargetDirectory(
			final File frameworkTargetDirectoryIn) {
		this.frameworkTargetDirectory = frameworkTargetDirectoryIn;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy