public boolean updateExcludesInDeps()

in src/main/java/org/apache/maven/plugins/shade/mojo/ShadeMojo.java [1178:1243]


    public boolean updateExcludesInDeps(
            MavenProject project, List<Dependency> dependencies, List<Dependency> transitiveDeps)
            throws DependencyGraphBuilderException {
        MavenProject original = session.getProjectBuildingRequest().getProject();
        try {
            session.getProjectBuildingRequest().setProject(project);
            DependencyNode node =
                    dependencyGraphBuilder.buildDependencyGraph(session.getProjectBuildingRequest(), null);
            boolean modified = false;
            for (DependencyNode n2 : node.getChildren()) {
                String artifactId2 = getId(n2.getArtifact());

                for (DependencyNode n3 : n2.getChildren()) {
                    Artifact artifact3 = n3.getArtifact();
                    String artifactId3 = getId(artifact3);

                    // 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 (getId(dep).equals(artifactId3)) {
                            found = true;
                            break;
                        }
                    }

                    // MSHADE-311: do not add exclusion for provided transitive dep
                    //       note: MSHADE-31 introduced the exclusion logic for promoteTransitiveDependencies=true,
                    //             but as of 3.2.1 promoteTransitiveDependencies has no effect for provided deps,
                    //             which makes this fix even possible (see also MSHADE-181)
                    if (!found && !"provided".equals(artifact3.getScope())) {
                        getLog().debug(String.format(
                                "dependency %s (scope %s) not found in transitive dependencies",
                                artifactId3, artifact3.getScope()));
                        for (Dependency dep : dependencies) {
                            if (getId(dep).equals(artifactId2)) {
                                // MSHADE-413: First check whether the exclusion has already been added,
                                // because it's meaningless to add it more than once. Certain cases
                                // can end up adding the exclusion "forever" and cause an endless loop
                                // rewriting the whole dependency-reduced-pom.xml file.
                                if (!dependencyHasExclusion(dep, artifact3)) {
                                    getLog().debug(String.format(
                                            "Adding exclusion for dependency %s (scope %s) " + "to %s (scope %s)",
                                            artifactId3, artifact3.getScope(), getId(dep), dep.getScope()));
                                    Exclusion exclusion = new Exclusion();
                                    exclusion.setArtifactId(artifact3.getArtifactId());
                                    exclusion.setGroupId(artifact3.getGroupId());
                                    dep.addExclusion(exclusion);
                                    modified = true;
                                    break;
                                }
                            }
                        }
                    }
                }
            }
            return modified;
        } finally {
            // restore it
            session.getProjectBuildingRequest().setProject(original);
        }
    }