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

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

There is a newer version: 1.0.37
Show newest version
/**
 * OW2 Util
 * Copyright (C) 2007-2010 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: GenerateMaven2DeploymentPlansMojo.java 5520 2010-06-02 09:41:18Z loris $
 * --------------------------------------------------------------------------
 */

package org.ow2.util.maven.jbuilding;

import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.Date;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.model.Resource;
import org.apache.maven.plugin.MojoExecutionException;
import org.codehaus.plexus.util.FileUtils;

/**
 * Generate maven2 deployment plans describe in the plugin configuration.
 * @author Francois Fornaciari
 *
 * @goal generate-maven2-deployment-plans
 */
public class GenerateMaven2DeploymentPlansMojo extends AbstractResolverMojo {

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

    /**
     * Location of the generated maven2 deployment plans
     * @parameter expression="repositories/url-internal"
     * @required
     */
    private String directory;

    /**
     * @throws MojoExecutionException When I cannot copy the artifacts in the
     *         output directory.
     * @see org.ow2.util.maven.jbuilding.AbstractResolverMojo#execute()
     */
    public void execute() throws MojoExecutionException {
        Map> plans = resolveArtifacts(SERVER_SIDE, BOTH_MODE, DEPLOYMENT_PLANS_ARTIFACT_ITEMS);

        // Generate maven2 deployment plans describe in the plugin configuration.
        Set>> deploymentPlanEntries = plans.entrySet();
        for (Entry> deploymentPlanEntry : deploymentPlanEntries) {
            String deploymentPlanName = deploymentPlanEntry.getKey();
            File file = new File(getDeploymentPlanDirectory(deploymentPlanName), deploymentPlanName.concat(".xml"));
            String[] includes = deploymentPlanIncludes.get(deploymentPlanName);
            generateDeploymentPlan(file, deploymentPlanEntry.getValue(), includes, deploymentPlanName);
        }
    }

    /**
     * @param file
     * @param artifacts
     * @param includes
     * @param deploymentPlanName the deployment plan name
     * @throws MojoExecutionException
     */
    private void generateDeploymentPlan(final File file, final Collection artifacts,
            final String[] includes, final String deploymentPlanName)
            throws MojoExecutionException {
        StringBuilder sb = new StringBuilder();
        sb.append("\n");
        sb.append("\n");
        sb.append("\n");

        if (includes != null) {
            for (String include : includes) {
                sb.append("  \n");
                sb.append("    " + include + ".xml\n");
                sb.append("  \n");
            }
        }

        for (Artifact artifact : artifacts) {
            // Get the reloadable mode
            boolean reloadable = artifactItemMap.get(artifact.getDependencyConflictId()).isReloadable();

            // Get the bundle start mode
            boolean start = artifactItemMap.get(artifact.getDependencyConflictId()).isStart();

            // Get the bundle reference mode
            boolean reference = artifactItemMap.get(artifact.getDependencyConflictId()).isReference();

            // Get the bundle start level
            int startLevel = artifactItemMap.get(artifact.getDependencyConflictId()).getStartlevel();

            // Get the bundle start operation mode
            boolean startTransient = artifactItemMap.get(artifact.getDependencyConflictId()).isStartTransient();

            sb.append("  \n");
            sb.append("    " + artifact.getGroupId() + "\n");
            sb.append("    " + artifact.getArtifactId() + "\n");

            if (artifact.getClassifier() != null) {
                sb.append("    " + artifact.getClassifier() + "\n");
            }

            sb.append("    " + artifact.getBaseVersion() + "\n");
            sb.append("    " + artifact.getType() + "\n");

            sb.append("  \n");
        }

        sb.append("");

        // add a final EOL
        sb.append("\n");

        // Flush the content
        try {
            FileUtils.fileWrite(file.getPath(), sb.toString());
        } catch (IOException e) {
            throw new MojoExecutionException("Cannot create file " + file);
        }

        // Add the generated file in the resources of maven
        Resource resource = new Resource();
        resource.setDirectory(output.getPath());
        getProject().addResource(resource);
    }

    /**
     * @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;
    }

    private File getDeploymentPlanDirectory(final String deploymentPlanName) throws MojoExecutionException {
        String planDirectoryLocation = directory;

        DeploymentPlan deploymentPlan = deploymentPlanMap.get(deploymentPlanName);
        if (deploymentPlan != null && deploymentPlan.getDirectory() != null) {
            planDirectoryLocation = deploymentPlan.getDirectory();
        }


        File planDirectoryLocationFile = new File(output, planDirectoryLocation.replace('/', File.separatorChar));
        // Create the directory
        if (!planDirectoryLocationFile.exists()) {
            if (!planDirectoryLocationFile.mkdirs()) {
                throw new MojoExecutionException("Cannot create directory: " + planDirectoryLocationFile);
            }
        }

        return planDirectoryLocationFile;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy