Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,17 @@
<artifactId>test-module2</artifactId>

<dependencies>
<dependency>
<groupId>org.apache.maven.its.dependency</groupId>
<artifactId>test-module1</artifactId>
<version>${project.version}</version>
<exclusions>
<exclusion>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,15 @@
</dependencies>
</dependencyManagement>

<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<version>@project.version@</version>
</plugin>
</plugins>
</pluginManagement>
</build>

</project>
9 changes: 5 additions & 4 deletions src/it/projects/analyze-invalid-exclude/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<version>3.9.6</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
Expand Down Expand Up @@ -75,6 +74,10 @@
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</exclusion>
<exclusion>
<groupId>org.codehaus.plexus</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
Expand All @@ -85,9 +88,7 @@
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<configuration>
<failOnWarning>false</failOnWarning>
</configuration>
<version>@project.version@</version>
</plugin>
</plugins>
</pluginManagement>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,28 @@
*/
package org.apache.maven.plugins.dependency.exclusion;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Consumer;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.RepositoryUtils;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Dependency;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.DefaultProjectBuildingRequest;
import org.apache.maven.plugins.dependency.utils.ResolverUtil;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.ProjectBuilder;
import org.apache.maven.project.ProjectBuildingException;
import org.apache.maven.project.ProjectBuildingRequest;
import org.apache.maven.project.ProjectBuildingResult;
import org.eclipse.aether.artifact.ArtifactTypeRegistry;
import org.eclipse.aether.collection.DependencyCollectionException;

import static java.lang.String.format;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toSet;
import static org.apache.commons.lang3.StringUtils.stripToEmpty;
import static org.apache.maven.plugins.dependency.exclusion.Coordinates.coordinates;

/**
Expand All @@ -56,14 +51,14 @@
*
* @since 3.7.0
*/
@Mojo(name = "analyze-exclusions", requiresDependencyResolution = ResolutionScope.TEST, threadSafe = true)
@Mojo(name = "analyze-exclusions", requiresDependencyCollection = ResolutionScope.TEST, threadSafe = true)
public class AnalyzeExclusionsMojo extends AbstractMojo {

@Component
private MavenProject project;

@Component
private ProjectBuilder projectBuilder;
private ResolverUtil resolverUtil;

@Component
private MavenSession session;
Expand All @@ -85,12 +80,12 @@ public class AnalyzeExclusionsMojo extends AbstractMojo {
private boolean skip;

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
public void execute() throws MojoExecutionException {
if (skip) {
getLog().debug("Skipping execution");
return;
}
List<Dependency> dependenciesWithExclusions = project.getDependencies().stream()
Collection<Dependency> dependenciesWithExclusions = project.getDependencies().stream()
.filter(dep -> !dep.getExclusions().isEmpty())
.collect(toList());

Expand All @@ -101,63 +96,48 @@ public void execute() throws MojoExecutionException, MojoFailureException {

ExclusionChecker checker = new ExclusionChecker();

ArtifactTypeRegistry artifactTypeRegistry =
session.getRepositorySession().getArtifactTypeRegistry();
for (final Dependency dependency : dependenciesWithExclusions) {

Coordinates currentCoordinates = coordinates(dependency.getGroupId(), dependency.getArtifactId());
Artifact matchingArtifact = project.getArtifacts().stream()
.filter(artifact -> matchesDependency(artifact, dependency))
.findFirst()
.orElseThrow(() -> new MojoExecutionException(
format("Error finding Artifact for given Dependency [%s]", dependency)));

ProjectBuildingResult result = buildProject(matchingArtifact);
Collection<org.eclipse.aether.graph.Dependency> actualDependencies = null;
try {
actualDependencies =
resolverUtil.collectDependencies(RepositoryUtils.toDependency(dependency, artifactTypeRegistry)
.setExclusions(null));
} catch (DependencyCollectionException e) {
throw new MojoExecutionException(e.getMessage(), e);
}

Set<Coordinates> actualDependencies = result.getProject().getArtifacts().stream()
Set<Coordinates> actualCoordinates = actualDependencies.stream()
.map(org.eclipse.aether.graph.Dependency::getArtifact)
.map(a -> coordinates(a.getGroupId(), a.getArtifactId()))
.collect(toSet());

Set<Coordinates> exclusions = dependency.getExclusions().stream()
.map(e -> coordinates(e.getGroupId(), e.getArtifactId()))
.collect(toSet());

checker.check(currentCoordinates, exclusions, actualDependencies);
checker.check(currentCoordinates, exclusions, actualCoordinates);
}

if (!checker.getViolations().isEmpty()) {
if (exclusionFail) {
logViolations(project.getName(), checker.getViolations(), (value) -> getLog().error(value));
logViolations(project.getName(), checker.getViolations(), value -> getLog().error(value));
throw new MojoExecutionException("Invalid exclusions found");
} else {
logViolations(project.getName(), checker.getViolations(), (value) -> getLog().warn(value));
logViolations(project.getName(), checker.getViolations(), value -> getLog().warn(value));
}
}
}

private boolean matchesDependency(Artifact artifact, Dependency dependency) {
return Objects.equals(artifact.getGroupId(), dependency.getGroupId())
&& Objects.equals(artifact.getArtifactId(), dependency.getArtifactId())
&& Objects.equals(artifact.getType(), dependency.getType())
&& Objects.equals(stripToEmpty(artifact.getClassifier()), stripToEmpty(dependency.getClassifier()));
}

private void logViolations(String name, Map<Coordinates, List<Coordinates>> violations, Consumer<String> logger) {
logger.accept(name + " defines following unnecessary excludes");
violations.forEach((dependency, invalidExclusions) -> {
logger.accept(" " + dependency + ":");
invalidExclusions.forEach(invalidExclusion -> {
logger.accept(" - " + invalidExclusion);
});
invalidExclusions.forEach(invalidExclusion -> logger.accept(" - " + invalidExclusion));
});
}

private ProjectBuildingResult buildProject(Artifact artifact) throws MojoExecutionException {
try {
ProjectBuildingRequest projectBuildingRequest =
new DefaultProjectBuildingRequest(session.getProjectBuildingRequest());
projectBuildingRequest.setResolveDependencies(true);
return projectBuilder.build(artifact, true, projectBuildingRequest);
} catch (ProjectBuildingException e) {
throw new MojoExecutionException(
format("Failed to build project for %s:%s", artifact.getGroupId(), artifact.getArtifactId()), e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.apache.maven.plugins.dependency.utils;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Provider;
import javax.inject.Singleton;

import java.util.Collection;

import org.apache.maven.execution.MavenSession;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.collection.CollectRequest;
import org.eclipse.aether.collection.CollectResult;
import org.eclipse.aether.collection.DependencyCollectionException;
import org.eclipse.aether.graph.Dependency;
import org.eclipse.aether.util.graph.visitor.PreorderNodeListGenerator;

/**
* Helper class for using Resolver API.
*/
@Named
@Singleton
public class ResolverUtil {

private final RepositorySystem repositorySystem;

private final Provider<MavenSession> mavenSessionProvider;

@Inject
private ResolverUtil(RepositorySystem repositorySystem, Provider<MavenSession> mavenSessionProvider) {
this.repositorySystem = repositorySystem;
this.mavenSessionProvider = mavenSessionProvider;
}

/**
* Collects the transitive dependencies.
*
* @param root a root dependency for collections
* @return a resolved dependencies collections
*/
public Collection<Dependency> collectDependencies(Dependency root) throws DependencyCollectionException {

MavenSession session = mavenSessionProvider.get();

CollectRequest request =
new CollectRequest(root, session.getCurrentProject().getRemoteProjectRepositories());
CollectResult result = repositorySystem.collectDependencies(session.getRepositorySession(), request);

PreorderNodeListGenerator nodeListGenerator = new PreorderNodeListGenerator();
result.getRoot().accept(nodeListGenerator);
return nodeListGenerator.getDependencies(true);
}
}
Loading