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

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

There is a newer version: 1.0.37
Show newest version
/**
 * OW2 Util
 * Copyright (C) 2007-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: AbstractResolverMojo.java 5455 2010-03-25 16:15:47Z jlegrand $
 * --------------------------------------------------------------------------
 */

package org.ow2.util.maven.jbuilding;

import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.factory.ArtifactFactory;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.resolver.ArtifactResolver;
import org.apache.maven.model.Repository;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;

/**
 * Resolve the artifacts.
 * @author Guillaume Sauthier
 */
public abstract class AbstractResolverMojo extends AbstractMojo {

    /**
     * @parameter alias="deploymentPlans"
     */
    private DeploymentPlan[] deploymentPlans = new DeploymentPlan[0];

    /**
     * List of deployment plans for current profile.
     * @parameter alias="profile"
     */
    private List profile;

    /**
     * @parameter alias="artifactItems"
     */
    private ArtifactItem[] artifactItems = new ArtifactItem[0];

    /**
     * List of Remote Repositories used by the resolver.
     * @parameter expression="${project.remoteArtifactRepositories}"
     * @readonly
     * @required
     */
    private List remoteRepositories;

    /**
     * @parameter expression="${project}"
     * @readonly
     * @required
     */
    private MavenProject project;

    /**
     * @component
     * @required
     */
    private ArtifactResolver resolver;

    /**
     * Location of the local repository.
     * @parameter expression="${localRepository}"
     * @readonly
     * @required
     */
    private ArtifactRepository local;

    /**
     * Deployment plan inclusion map.
     */
    protected Map deploymentPlanIncludes = null;

    /**
     * Artifact items map.
     */
    protected Map artifactItemMap = null;

    /**
     * Deployment plan map.
     */
    protected Map deploymentPlanMap = null;

    /**
     * @component
     * @required
     */
    private ArtifactFactory factory;

    public static final int CLIENT_SIDE = 0;
    public static final int SERVER_SIDE = 1;

    public static final int BOTH_MODE = 0;
    public static final int INSTALL_MODE = 1;
    public static final int START_MODE = 2;

    public static final String AUTO_DEPLOY_ARTIFACT_ITEMS = "AUTO_DEPLOY_ARTIFACT_ITEMS";
    public static final String DEPLOYMENT_PLANS_ARTIFACT_ITEMS = "DEPLOYMENT_PLANS_ARTIFACT_ITEMS";
    public static final String ALL_ARTIFACT_ITEMS = "ALL_ARTIFACT_ITEMS";

    public AbstractResolverMojo()  {
        deploymentPlanIncludes = new HashMap();
        artifactItemMap = new HashMap();
        deploymentPlanMap = new HashMap();
    }

    /**
     * Resolve/Download all declared artifacts.
     * @param side "client" or "server"
     * @param mode "install", "start" or "both"
     * @param artifactItemType artifact item type
     * @return a {@link Map} of {@link Collection}s of {@link Artifact}s.
     * @throws MojoExecutionException If resolution fails.
     */
    public Map> resolveArtifacts(final int side, final int mode, final String artifactItemType)
            throws MojoExecutionException {
        Map> map = new HashMap>();
        Collection artifacts = null;

        if (ALL_ARTIFACT_ITEMS.equals(artifactItemType) || AUTO_DEPLOY_ARTIFACT_ITEMS.equals(artifactItemType)) {
            artifacts = resolveArtifacts(side, mode, artifactItems);
            map.put(AUTO_DEPLOY_ARTIFACT_ITEMS, artifacts);
        }

        if (ALL_ARTIFACT_ITEMS.equals(artifactItemType) || DEPLOYMENT_PLANS_ARTIFACT_ITEMS.equals(artifactItemType)) {
            // Clear deployment plan inclusion map
            deploymentPlanIncludes.clear();

            for (DeploymentPlan deploymentPlan: deploymentPlans) {
                String deploymentPlanName = deploymentPlan.getName();

                // Only copy in repository deployment plan artifacts defined in the current profile
                if (ALL_ARTIFACT_ITEMS.equals(artifactItemType) && !profile.contains(deploymentPlanName)) {
                    continue;
                }

                // Generate deployment plans for application only if defined in the current profile
                if (DEPLOYMENT_PLANS_ARTIFACT_ITEMS.equals(artifactItemType) && deploymentPlan.getDirectory() != null
                        && !profile.contains(deploymentPlanName)) {
                    continue;
                }

                deploymentPlanMap.put(deploymentPlanName, deploymentPlan);

                ArtifactItem[] items = deploymentPlan.getArtifactItems();
                artifacts = resolveArtifacts(side, mode, items);
                map.put(deploymentPlanName, artifacts);

                String[] includes = deploymentPlan.getIncludes();
                deploymentPlanIncludes.put(deploymentPlanName, includes);
            }
        }

        return map;
    }


    /**
     * Resolve/Download all declared Bundles/Artifacts.
     * @param side "client" or "server"
     * @param mode "install", "start" or "both"
     * @param items a set of artifact items to resolve
     * @return a {@link Collection} of {@link Artifact}s.
     * @throws MojoExecutionException If resolution fails.
     */
    private Collection resolveArtifacts(final int side, final int mode, final ArtifactItem[] items)
            throws MojoExecutionException {
        // First create artifacts from the ArtifactItems
        Map artifacts = new LinkedHashMap();

        if (items != null) {
            for (int j = 0; j < items.length; j++) {
                ArtifactItem artifactItem = items[j];
                if (!(mode == INSTALL_MODE && !artifactItem.isStart()) && !(mode == START_MODE && artifactItem.isStart())
                        && !(mode == BOTH_MODE)) {
                    continue;
                }
                if ("client".equalsIgnoreCase(artifactItem.getSide()) && side == SERVER_SIDE) {
                    continue;
                }
                if ("server".equalsIgnoreCase(artifactItem.getSide()) && side == CLIENT_SIDE) {
                    continue;
                }
                Artifact artifact = createArtifact(artifactItem.getGroupId(),
                                                   artifactItem.getArtifactId(),
                                                   artifactItem.getVersion(),
                                                   artifactItem.getType(),
                                                   artifactItem.getClassifier());

                // Store the artifact with its conflitId in a Map
                artifacts.put(artifact.getDependencyConflictId(), artifact);
                artifactItemMap.put(artifact.getDependencyConflictId(), artifactItem);
            }

            printArtifacts("Created ...", artifacts);

            // Manually resolve artifacts using dependencyManagement
            Set> artifactsSet = artifacts.entrySet();

            for (Map.Entry artifactEntry : artifactsSet) {
                String key = artifactEntry.getKey();
                Artifact artifact = artifactEntry.getValue();

                Map managedVersions = project.getManagedVersionMap();
                Artifact resolved  = null;
                if (managedVersions != null) {
                    resolved = (Artifact) managedVersions.get(key);
                }

                if (resolved != null) {
                    // Got a managed version of the artifact
                    artifacts.put(key, resolved);
                } else if (Artifact.LATEST_VERSION.equals(artifact.getVersion())) {
                    // We don't have a managed version of this artifact
                    // If the version is still latest, assume this is a JOnAS
                    // artifact
                    // and use the JOnAS version
                    artifacts.get(key).setVersion(project.getVersion());
                } // else, let use the provided version
            }

            // Now we can do something from that
            // Resolve (download) the required artifacts
            for (Artifact artifact : artifacts.values()) {
                try {
                    resolveArtifact(artifact);
                } catch (Exception e) {
                    throw new MojoExecutionException("Cannot resolve/download the Bundle " + artifact, e);
                }
            }

            printArtifacts("Resolved ...", artifacts);
        }

        return artifacts.values();
    }

    /**
     * Construct a new "jar" artifact.
     * @param groupId Group ID
     * @param artifactId Artifact ID
     * @param version Artifact's version
     * @param classifier Artifact's classifier (may be null)
     * @return a new artifact
     */
    protected Artifact createArtifact(final String groupId,
                                    final String artifactId,
                                    final String version,
                                    final String type,
                                    final String classifier) {
        return factory.createArtifactWithClassifier(groupId, artifactId, version, type, classifier);
    }

    /**
     * Resolve the given artifact (triggering its download if needed).
     * @param artifact the given artifact to resolve
     * @throws Exception if there is failure when resolving
     */
    protected void resolveArtifact(final Artifact artifact) throws Exception {
        resolver.resolve(artifact, remoteRepositories, local);
    }

    /**
     * Debug artifact Map.
     * @param banner Header
     * @param artifacts Map of artifacts to print.
     */
    private void printArtifacts(final String banner, final Map artifacts) {
        getLog().debug(banner);
        for (Artifact artifact : artifacts.values()) {
            getLog().debug(artifact.toString());
        }
    }

    /**
     * @return the project.
     */
    public MavenProject getProject() {
        return project;
    }

    /**
     * @return the artifact factory.
     */
    protected ArtifactFactory getArtifactFactory() {
        return this.factory;
    }

    /**
     * @return remote repositories.
     */
    public List getRemoteRepositories() {
        return remoteRepositories;
    }

    /**
     * Sets the remote repositories.
     * @param remoteRepositories the given list of repositories
     */
    public void setRemoteRepositories(final List remoteRepositories) {
        this.remoteRepositories = remoteRepositories;
    }

    /**
     * @return the artifact resolver.
     */
    public ArtifactResolver getResolver() {
        return resolver;
    }

    /**
     * Sets the artifact resolver.
     * @param resolver the maven resolver
     */
    public void setResolver(final ArtifactResolver resolver) {
        this.resolver = resolver;
    }

    /**
     * @return the local artifact repository.
     */
    public ArtifactRepository getLocal() {
        return local;
    }

    /**
     * Sets the local artifact repository.
     * @param local the local repository
     */
    public void setLocal(final ArtifactRepository local) {
        this.local = local;
    }

    /**
     * @return the maven artifact factory.
     */
    public ArtifactFactory getFactory() {
        return factory;
    }

    /**
     * Sets the maven artifact factory.
     * @param factory the given factory
     */
    public void setFactory(final ArtifactFactory factory) {
        this.factory = factory;
    }

    /**
     * Sets the maven project of this plugin.
     * @param project the given project
     */
    public void setProject(final MavenProject project) {
        this.project = project;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy