Search in sources :

Example 16 with Exclusion

use of org.apache.maven.model.Exclusion in project pom-manipulation-ext by release-engineering.

the class DependencyManipulator method applyExplicitOverrides.

/**
 * Apply explicit overrides to a set of dependencies from a project. The explicit overrides come from
 * dependencyExclusion. However they have to be separated out from standard overrides so we can easily
 * ignore any property references (and overwrite them).
 *
 * @param project the current Project
 * @param dependencies dependencies to check
 * @param explicitOverrides a custom map to handle wildcard overrides
 * @param state the CommonState, to retrieve Common Properties
 * @param versionPropertyUpdateMap properties to update
 * @throws ManipulationException if an error occurs
 */
private void applyExplicitOverrides(final Project project, final HashMap<ArtifactRef, Dependency> dependencies, final WildcardMap<String> explicitOverrides, final CommonState state, final Map<Project, Map<String, String>> versionPropertyUpdateMap) throws ManipulationException {
    // Apply matching overrides to dependencies
    for (final ProjectVersionRef dependency : dependencies.keySet()) {
        final ProjectRef groupIdArtifactId = new SimpleProjectRef(dependency.getGroupId(), dependency.getArtifactId());
        if (explicitOverrides.containsKey(groupIdArtifactId)) {
            final String overrideVersion = explicitOverrides.get(groupIdArtifactId);
            final String oldVersion = dependencies.get(dependency).getVersion();
            if (isEmpty(overrideVersion) || isEmpty(oldVersion)) {
                if (isEmpty(oldVersion)) {
                    logger.debug("Unable to force align as no existing version field to update for " + groupIdArtifactId + "; ignoring");
                } else {
                    logger.warn("Unable to force align as override version is empty for " + groupIdArtifactId + "; ignoring");
                }
            } else {
                for (String target : overrideVersion.split(",")) {
                    if (target.startsWith("+")) {
                        logger.info("Adding dependency exclusion {} to dependency {} ", target.substring(1), dependency);
                        Exclusion e = new Exclusion();
                        e.setGroupId(target.substring(1).split(":")[0]);
                        e.setArtifactId(target.split(":")[1]);
                        dependencies.get(dependency).addExclusion(e);
                    } else {
                        logger.info("Explicit overrides : force aligning {} to {}.", groupIdArtifactId, target);
                        if (!PropertiesUtils.cacheProperty(project, state, versionPropertyUpdateMap, oldVersion, target, dependency, true)) {
                            if (oldVersion.contains("${")) {
                                logger.warn("Overriding version with {} when old version contained a property {} ", target, oldVersion);
                            // TODO: Should this throw an exception?
                            }
                            // Not checking strict version alignment here as explicit overrides take priority.
                            dependencies.get(dependency).setVersion(target);
                        }
                    }
                }
            }
        }
    }
}
Also used : SimpleProjectRef(org.commonjava.maven.atlas.ident.ref.SimpleProjectRef) ProjectVersionRef(org.commonjava.maven.atlas.ident.ref.ProjectVersionRef) Exclusion(org.apache.maven.model.Exclusion) SimpleProjectRef(org.commonjava.maven.atlas.ident.ref.SimpleProjectRef) ProjectRef(org.commonjava.maven.atlas.ident.ref.ProjectRef)

Example 17 with Exclusion

use of org.apache.maven.model.Exclusion in project maven-plugins by apache.

the class ShadeMojo method updateExcludesInDeps.

public boolean updateExcludesInDeps(MavenProject project, List<Dependency> dependencies, List<Dependency> transitiveDeps) throws DependencyGraphBuilderException {
    DependencyNode node = dependencyGraphBuilder.buildDependencyGraph(project, null);
    boolean modified = false;
    for (DependencyNode n2 : node.getChildren()) {
        for (DependencyNode n3 : n2.getChildren()) {
            // check if it really isn't in the list of original dependencies. Maven
            // prior to 2.0.8 may grab versions from transients instead of
            // from the direct deps in which case they would be marked included
            // instead of OMITTED_FOR_DUPLICATE
            // also, if not promoting the transitives, level 2's would be included
            boolean found = false;
            for (Dependency dep : transitiveDeps) {
                if (dep.getArtifactId().equals(n3.getArtifact().getArtifactId()) && dep.getGroupId().equals(n3.getArtifact().getGroupId()) && (dep.getType() == null || dep.getType().equals(n3.getArtifact().getType()))) {
                    found = true;
                    break;
                }
            }
            if (!found) {
                for (Dependency dep : dependencies) {
                    if (dep.getArtifactId().equals(n2.getArtifact().getArtifactId()) && dep.getGroupId().equals(n2.getArtifact().getGroupId()) && (dep.getType() == null || dep.getType().equals(n2.getArtifact().getType()))) {
                        Exclusion exclusion = new Exclusion();
                        exclusion.setArtifactId(n3.getArtifact().getArtifactId());
                        exclusion.setGroupId(n3.getArtifact().getGroupId());
                        dep.addExclusion(exclusion);
                        modified = true;
                        break;
                    }
                }
            }
        }
    }
    return modified;
}
Also used : DependencyNode(org.apache.maven.shared.dependency.graph.DependencyNode) Exclusion(org.apache.maven.model.Exclusion) Dependency(org.apache.maven.model.Dependency)

Example 18 with Exclusion

use of org.apache.maven.model.Exclusion in project maven-plugins by apache.

the class AnalyzeDepMgt method checkDependencyManagement.

/**
 * Does the work of checking the DependencyManagement Section.
 *
 * @return true if errors are found.
 * @throws MojoExecutionException
 */
private boolean checkDependencyManagement() throws MojoExecutionException {
    boolean foundError = false;
    getLog().info("Found Resolved Dependency/DependencyManagement mismatches:");
    List<Dependency> depMgtDependencies = null;
    DependencyManagement depMgt = project.getDependencyManagement();
    if (depMgt != null) {
        depMgtDependencies = depMgt.getDependencies();
    }
    if (depMgtDependencies != null && !depMgtDependencies.isEmpty()) {
        // put all the dependencies from depMgt into a map for quick lookup
        Map<String, Dependency> depMgtMap = new HashMap<String, Dependency>();
        Map<String, Exclusion> exclusions = new HashMap<String, Exclusion>();
        for (Dependency depMgtDependency : depMgtDependencies) {
            depMgtMap.put(depMgtDependency.getManagementKey(), depMgtDependency);
            // now put all the exclusions into a map for quick lookup
            exclusions.putAll(addExclusions(depMgtDependency.getExclusions()));
        }
        // get dependencies for the project (including transitive)
        Set<Artifact> allDependencyArtifacts = new LinkedHashSet<Artifact>(project.getArtifacts());
        // depMgt. That's ok.
        if (this.ignoreDirect) {
            getLog().info("\tIgnoring Direct Dependencies.");
            Set<Artifact> directDependencies = project.getDependencyArtifacts();
            allDependencyArtifacts.removeAll(directDependencies);
        }
        // log exclusion errors
        List<Artifact> exclusionErrors = getExclusionErrors(exclusions, allDependencyArtifacts);
        for (Artifact exclusion : exclusionErrors) {
            getLog().info(StringUtils.stripEnd(getArtifactManagementKey(exclusion), ":") + " was excluded in DepMgt, but version " + exclusion.getVersion() + " has been found in the dependency tree.");
            foundError = true;
        }
        // find and log version mismatches
        Map<Artifact, Dependency> mismatch = getMismatch(depMgtMap, allDependencyArtifacts);
        for (Map.Entry<Artifact, Dependency> entry : mismatch.entrySet()) {
            logMismatch(entry.getKey(), entry.getValue());
            foundError = true;
        }
        if (!foundError) {
            getLog().info("\tNone");
        }
    } else {
        getLog().info("\tNothing in DepMgt.");
    }
    return foundError;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) HashMap(java.util.HashMap) Dependency(org.apache.maven.model.Dependency) Artifact(org.apache.maven.artifact.Artifact) Exclusion(org.apache.maven.model.Exclusion) HashMap(java.util.HashMap) Map(java.util.Map) DependencyManagement(org.apache.maven.model.DependencyManagement)

Example 19 with Exclusion

use of org.apache.maven.model.Exclusion in project maven-plugins by apache.

the class TestAnalyzeDepMgt method setUp.

protected void setUp() throws Exception {
    mojo = new AnalyzeDepMgt();
    MavenProject project = new DependencyProjectStub();
    stubFactory = new DependencyArtifactStubFactory(new File(""), false);
    Set<Artifact> allArtifacts = stubFactory.getMixedArtifacts();
    Set<Artifact> directArtifacts = stubFactory.getClassifiedArtifacts();
    exclusionArtifact = stubFactory.getReleaseArtifact();
    directArtifacts.add(exclusionArtifact);
    ex = new Exclusion();
    ex.setArtifactId(exclusionArtifact.getArtifactId());
    ex.setGroupId(exclusionArtifact.getGroupId());
    exclusion = new Dependency();
    exclusion.setArtifactId(exclusionArtifact.getArtifactId());
    exclusion.setGroupId(exclusionArtifact.getGroupId());
    exclusion.setType(exclusionArtifact.getType());
    exclusion.setClassifier("");
    exclusion.setVersion("3.0");
    exclusion.addExclusion(ex);
    List<Dependency> list = new ArrayList<Dependency>();
    list.add(exclusion);
    depMgt = new DependencyManagement();
    depMgt.setDependencies(list);
    project.setArtifacts(allArtifacts);
    project.setDependencyArtifacts(directArtifacts);
    mojo.setProject(project);
}
Also used : AnalyzeDepMgt(org.apache.maven.plugins.dependency.analyze.AnalyzeDepMgt) MavenProject(org.apache.maven.project.MavenProject) Exclusion(org.apache.maven.model.Exclusion) ArrayList(java.util.ArrayList) Dependency(org.apache.maven.model.Dependency) DependencyProjectStub(org.apache.maven.plugins.dependency.testUtils.stubs.DependencyProjectStub) File(java.io.File) Artifact(org.apache.maven.artifact.Artifact) DependencyManagement(org.apache.maven.model.DependencyManagement) DependencyArtifactStubFactory(org.apache.maven.plugins.dependency.testUtils.DependencyArtifactStubFactory)

Aggregations

Exclusion (org.apache.maven.model.Exclusion)19 Dependency (org.apache.maven.model.Dependency)11 Artifact (org.apache.maven.artifact.Artifact)6 File (java.io.File)4 ArrayList (java.util.ArrayList)4 Iterator (java.util.Iterator)4 DependencyManagement (org.apache.maven.model.DependencyManagement)4 Element (org.jdom.Element)4 HashMap (java.util.HashMap)2 LinkedHashSet (java.util.LinkedHashSet)2 Map (java.util.Map)2 ArtifactFilter (org.apache.maven.artifact.resolver.filter.ArtifactFilter)2 ExcludesArtifactFilter (org.apache.maven.artifact.resolver.filter.ExcludesArtifactFilter)2 InvalidVersionSpecificationException (org.apache.maven.artifact.versioning.InvalidVersionSpecificationException)2 VersionRange (org.apache.maven.artifact.versioning.VersionRange)2 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)2 AnalyzeDepMgt (org.apache.maven.plugins.dependency.analyze.AnalyzeDepMgt)2 DependencyArtifactStubFactory (org.apache.maven.plugins.dependency.testUtils.DependencyArtifactStubFactory)2 DependencyProjectStub (org.apache.maven.plugins.dependency.testUtils.stubs.DependencyProjectStub)2 MavenProject (org.apache.maven.project.MavenProject)2