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

org.ow2.util.maven.jbuilding.GenerateClientJarMojo Maven / Gradle / Ivy

There is a newer version: 1.0.37
Show newest version
/**
 * OW2 Util
 * Copyright (C) 2008 Bull S.A.S.
 * Contact: [email protected]
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 *
 * --------------------------------------------------------------------------
 * $Id: GenerateClientJarMojo.java 4955 2009-05-13 12:24:17Z fornacif $
 * --------------------------------------------------------------------------
 */

package org.ow2.util.maven.jbuilding;

import java.io.File;
import java.util.Collection;
import java.util.Map;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugin.MojoExecutionException;
import org.ow2.util.maven.jbuilding.jar.ClientJarProvider;

/**
 * Create a big jar archive from the listed bundles content.
 *
 * @goal client-jar
 *
 * @phase process-sources
 */
public class GenerateClientJarMojo extends AbstractResolverMojo {

    /**
     * iPOJO Classifier default name.
     */
    private static final String IPOJO_CLASSIFIER = "ipojo";

    /**
     * Location new resources directory.
     *
     * @parameter expression="${project.build.directory}/assemble-resources"
     * @required
     */
    private File output;

    /**
     * Where do I place my artifacts ?
     *
     * @parameter expression="lib"
     * @required
     */
    private String directory;

    /**
     * The main class of the Client jar ?
     * @parameter
     */
    private String clientJarMainClass;

    /**
     * Generated jar name.
     * @parameter expression="client.jar"
     * @required
     */
    private String jarName;

    /**
     * @throws MojoExecutionException When .
     * @see org.ow2.util.maven.jbuilding.AbstractResolverMojo#execute()
     */
    public void execute() throws MojoExecutionException {

        Map> map = resolveArtifacts(CLIENT_SIDE, BOTH_MODE, ALL_ARTIFACT_ITEMS);

        // OK, now we're sure that everything is in the local repository
        // We have to copy theses files in the output directory
        File libDirectory = new File(output, directory.replace('/',
                                                               File.separatorChar));

        // Create the directory
        if (!libDirectory.exists()) {
            if (!libDirectory.mkdirs()) {
                throw new MojoExecutionException("Cannot create directory: "
                                                 + libDirectory);
            }
        }

        ClientJarProvider provider = new ClientJarProvider();

        File target = new File(libDirectory, jarName);
        provider.setTargetJarFile(target);

        addDefaultExclusionPatterns(provider);

        // List all plans
        for (Collection artifacts: map.values()) {
            // List all artifacts/bundle
            for (Artifact artifact : artifacts) {

                Artifact updated = artifact;

                // If this is an ipojo classified object, switch to the "normal" one
                if (IPOJO_CLASSIFIER.equals(artifact.getClassifier())) {
                    // Change classifier to "normal" one: 'jar'
                    updated = getArtifactFactory().createArtifact(artifact.getGroupId(),
                                                                  artifact.getArtifactId(),
                                                                  artifact.getVersion(),
                                                                  artifact.getScope(),
                                                                  artifact.getType());
                    try {
                        // Resolve the artifact
                        resolveArtifact(updated);
                    } catch (Exception e) {
                        throw new MojoExecutionException("Cannot resolve artifact: " + updated, e);
                    }
                }

                getLog().debug("Artifact file path: " + updated.getFile());

                // Get the Artifact local File path
                // and add it to the provider sources list
                File source = updated.getFile();
                provider.addSourceFile(source);
            }
        }

        try {
            provider.createClientJarFile(getProject().getVersion(), clientJarMainClass);
        } catch (Exception e) {
            throw new MojoExecutionException("Cannot create target file: " + target, e);
        }

        getProject().addCompileSourceRoot(output.getPath());

    }

    /**
     * Assign all the default excluded pattern in the provider.
     * @param provider {@link ClientJarProvider} instance.
     */
    protected void addDefaultExclusionPatterns(final ClientJarProvider provider) {
        // Some excluded patterns (Tomcat, Bouncycastle, some Keys) ...
        provider.addExclusionPattern("org/apache/tomcat");
        provider.addExclusionPattern("org/apache/catalina");
        provider.addExclusionPattern("org/apache/coyote");
        provider.addExclusionPattern("org/bouncycastle");
        provider.addExclusionPattern("org/jgroups");
        provider.addExclusionPattern("org/mortbay");
        provider.addExclusionPattern("META-INF/maven");
        provider.addExclusionPattern("META-INF/BCKEY");
    }

    /**
     * @return the output file.
     */
    public File getOutput() {
        return output;
    }

    /**
     * @return the directory.
     */
    public String getDirectory() {
        return directory;
    }

    /**
     * Set the given output.
     * @param output the output file
     */
    public void setOutput(final File output) {
        this.output = output;
    }

    /**
     * Sets the given directory.
     * @param directory the directory path
     */
    public void setDirectory(final String directory) {
        this.directory = directory;
    }

    /**
     * Sets the main class to use for the Jar.
     * @param clientJarMainClass the name of the main-class
     */
    public void setClientJarMainClass(final String clientJarMainClass) {
        this.clientJarMainClass = clientJarMainClass;
    }

    /**
     * Sets the name of the output jar.
     * @param jarName the name of the jar
     */
    public void setJarName(final String jarName) {
        this.jarName = jarName;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy