org.jfrog.jade.plugins.ide.AbstractIdeMojo.java Source code

Java tutorial

Introduction

Here is the source code for org.jfrog.jade.plugins.ide.AbstractIdeMojo.java

Source

/*
 * Copyright 2005-2006 The Apache Software Foundation.
 *
 * Licensed 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.
 */
package org.jfrog.jade.plugins.ide;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
import org.apache.maven.artifact.resolver.filter.ExcludesArtifactFilter;
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
import org.apache.maven.artifact.versioning.VersionRange;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.DependencyManagement;
import org.apache.maven.model.Exclusion;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.ProjectBuildingException;
import org.jfrog.jade.plugins.common.injectable.MvnInjectable;
import org.jfrog.jade.plugins.common.injectable.MvnInjectableMojoSupport;
import org.jfrog.jade.plugins.common.naming.GroupDefinition;
import org.jfrog.jade.plugins.common.naming.ProjectNameProvider;
import org.jfrog.maven.annomojo.annotations.MojoComponent;
import org.jfrog.maven.annomojo.annotations.MojoParameter;

import java.io.File;
import java.util.*;

/**
 * Super abstract class for all IDE related functions.
 * User: freds
 * Date: May 20, 2007
 * Time: 2:46:37 AM
 */
public abstract class AbstractIdeMojo extends MvnInjectableMojoSupport {
    @MojoParameter(expression = "${executedProject}", required = true, readonly = true, description = "The Maven Project after generate-sources")
    protected MavenProject executedProject;
    @MojoComponent(roleHint = "maven")
    protected ArtifactMetadataSource artifactMetadataSource;
    @MojoParameter
    protected Set<GroupDefinition> groupDefinitions = new HashSet<GroupDefinition>();
    @MojoParameter(expression = "${reactorProjects}", required = true, readonly = true)
    protected List<MavenProject> reactorProjects;
    @MojoParameter(property = "templatesPath", description = "Absolute path for the workspace/module/project templates")
    protected String templatesPath;

    public void updateFromMvnInjectable(MvnInjectable injectable) {
        super.updateFromMvnInjectable(injectable);
        AbstractIdeMojo ideaMojo = ((AbstractIdeMojo) injectable);
        this.artifactMetadataSource = ideaMojo.artifactMetadataSource;
        this.executedProject = ideaMojo.getExecutedProject();
        this.groupDefinitions = ideaMojo.getGroupDefinitions();
        this.reactorProjects = ideaMojo.getReactorProjects();
        this.templatesPath = ideaMojo.templatesPath;
    }

    public MavenProject getExecutedProject() {
        return executedProject;
    }

    public Set<GroupDefinition> getGroupDefinitions() {
        return groupDefinitions;
    }

    public void setGroupDefinitions(Set<GroupDefinition> groupDefinitions) {
        this.groupDefinitions = groupDefinitions;
    }

    public List<MavenProject> getReactorProjects() {
        return reactorProjects;
    }

    public ProjectNameProvider getNameProvider() {
        ProjectNameProvider np = super.getNameProvider();
        if (np != null && groupDefinitions != null && !groupDefinitions.isEmpty())
            np.setGroupDefinitions(groupDefinitions);
        return np;
    }

    public String getIdeProjectName() {
        return getNameProvider().getProjectName(getExecutedProject());
    }

    protected Set getProjectArtifacts() throws InvalidVersionSpecificationException {
        Set<Artifact> artifacts = new HashSet<Artifact>();

        for (Iterator dependencies = executedProject.getDependencies().iterator(); dependencies.hasNext();) {
            Dependency dep = (Dependency) dependencies.next();

            String groupId = dep.getGroupId();
            String artifactId = dep.getArtifactId();
            VersionRange versionRange = VersionRange.createFromVersionSpec(dep.getVersion());
            String type = dep.getType();
            if (type == null) {
                type = "jar";
            }
            String classifier = dep.getClassifier();
            boolean optional = dep.isOptional();
            String scope = dep.getScope();
            if (scope == null) {
                scope = Artifact.SCOPE_COMPILE;
            }

            Artifact artifact = getArtifactFactory().createDependencyArtifact(groupId, artifactId, versionRange,
                    type, classifier, scope, optional);

            if (scope.equalsIgnoreCase(Artifact.SCOPE_SYSTEM)) {
                artifact.setFile(new File(dep.getSystemPath()));
            }

            List<String> exclusions = new ArrayList<String>();
            for (Iterator j = dep.getExclusions().iterator(); j.hasNext();) {
                Exclusion e = (Exclusion) j.next();
                exclusions.add(e.getGroupId() + ":" + e.getArtifactId());
            }

            ArtifactFilter newFilter = new ExcludesArtifactFilter(exclusions);

            artifact.setDependencyFilter(newFilter);

            artifacts.add(artifact);
        }

        return artifacts;
    }

    protected Map<String, Artifact> createManagedVersionMap() throws ProjectBuildingException {
        Map<String, Artifact> map;
        DependencyManagement dependencyManagement = getExecutedProject().getDependencyManagement();
        String projectId = getExecutedProject().getId();

        if (dependencyManagement != null && dependencyManagement.getDependencies() != null) {
            map = new HashMap<String, Artifact>();
            for (Iterator i = dependencyManagement.getDependencies().iterator(); i.hasNext();) {
                Dependency d = (Dependency) i.next();

                try {
                    VersionRange versionRange = VersionRange.createFromVersionSpec(d.getVersion());
                    Artifact artifact = getArtifactFactory().createDependencyArtifact(d.getGroupId(),
                            d.getArtifactId(), versionRange, d.getType(), d.getClassifier(), d.getScope(),
                            d.isOptional());
                    map.put(d.getManagementKey(), artifact);
                } catch (InvalidVersionSpecificationException e) {
                    throw new ProjectBuildingException(projectId, "Unable to parse version '" + d.getVersion()
                            + "' for dependency '" + d.getManagementKey() + "': " + e.getMessage(), e);
                }
            }
        } else {
            map = Collections.emptyMap();
        }
        return map;
    }
}