|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.maven.plugins.dependency.exclusion; |
| 20 | + |
| 21 | +import java.util.List; |
| 22 | +import java.util.Map; |
| 23 | +import java.util.Objects; |
| 24 | +import java.util.Set; |
| 25 | +import java.util.function.Consumer; |
| 26 | + |
| 27 | +import org.apache.maven.artifact.Artifact; |
| 28 | +import org.apache.maven.execution.MavenSession; |
| 29 | +import org.apache.maven.model.Dependency; |
| 30 | +import org.apache.maven.plugin.AbstractMojo; |
| 31 | +import org.apache.maven.plugin.MojoExecutionException; |
| 32 | +import org.apache.maven.plugin.MojoFailureException; |
| 33 | +import org.apache.maven.plugins.annotations.Component; |
| 34 | +import org.apache.maven.plugins.annotations.Execute; |
| 35 | +import org.apache.maven.plugins.annotations.LifecyclePhase; |
| 36 | +import org.apache.maven.plugins.annotations.Mojo; |
| 37 | +import org.apache.maven.plugins.annotations.Parameter; |
| 38 | +import org.apache.maven.plugins.annotations.ResolutionScope; |
| 39 | +import org.apache.maven.project.DefaultProjectBuildingRequest; |
| 40 | +import org.apache.maven.project.MavenProject; |
| 41 | +import org.apache.maven.project.ProjectBuilder; |
| 42 | +import org.apache.maven.project.ProjectBuildingException; |
| 43 | +import org.apache.maven.project.ProjectBuildingRequest; |
| 44 | +import org.apache.maven.project.ProjectBuildingResult; |
| 45 | + |
| 46 | +import static java.lang.String.format; |
| 47 | +import static java.util.stream.Collectors.toList; |
| 48 | +import static java.util.stream.Collectors.toSet; |
| 49 | +import static org.apache.commons.lang3.StringUtils.stripToEmpty; |
| 50 | +import static org.apache.maven.plugins.dependency.exclusion.Coordinates.coordinates; |
| 51 | + |
| 52 | +/** |
| 53 | + * Analyzes the exclusions defined on dependencies in this project and reports if any of them are invalid. |
| 54 | + * <p> |
| 55 | + * Relevant use case is when an artifact in a later version has removed usage of a dependency, making the exclusion no |
| 56 | + * longer valid. |
| 57 | + * </p> |
| 58 | + * @since 3.6.2 |
| 59 | + */ |
| 60 | +@Mojo(name = "analyze-exclusions", requiresDependencyResolution = ResolutionScope.TEST, threadSafe = true) |
| 61 | +@Execute(phase = LifecyclePhase.TEST_COMPILE) |
| 62 | +public class AnalyzeExclusionsMojo extends AbstractMojo { |
| 63 | + |
| 64 | + @Component |
| 65 | + private MavenProject project; |
| 66 | + |
| 67 | + @Component |
| 68 | + private ProjectBuilder projectBuilder; |
| 69 | + |
| 70 | + @Component |
| 71 | + private MavenSession session; |
| 72 | + |
| 73 | + /** |
| 74 | + * Whether to fail the build if invalid exclusions is found. |
| 75 | + */ |
| 76 | + @Parameter(property = "failOnWarning", defaultValue = "false") |
| 77 | + private boolean failOnWarning; |
| 78 | + |
| 79 | + /** |
| 80 | + * Skip plugin execution completely. |
| 81 | + */ |
| 82 | + @Parameter(property = "mdep.skip", defaultValue = "false") |
| 83 | + private boolean skip; |
| 84 | + |
| 85 | + @Override |
| 86 | + public void execute() throws MojoExecutionException, MojoFailureException { |
| 87 | + if (skip) { |
| 88 | + getLog().debug("Skipping execution"); |
| 89 | + return; |
| 90 | + } |
| 91 | + List<Dependency> dependenciesWithExclusions = project.getDependencies().stream() |
| 92 | + .filter(dep -> !dep.getExclusions().isEmpty()) |
| 93 | + .collect(toList()); |
| 94 | + |
| 95 | + if (dependenciesWithExclusions.isEmpty()) { |
| 96 | + getLog().debug("No dependencies defined with exclusions - exiting"); |
| 97 | + return; |
| 98 | + } |
| 99 | + |
| 100 | + ExclusionChecker checker = new ExclusionChecker(); |
| 101 | + |
| 102 | + for (Dependency dependency : dependenciesWithExclusions) { |
| 103 | + Coordinates currentCoordinates = coordinates(dependency.getGroupId(), dependency.getArtifactId()); |
| 104 | + Artifact matchingArtifact = project.getArtifacts().stream() |
| 105 | + .filter(artifact -> matchesDependency(artifact, dependency)) |
| 106 | + .findFirst() |
| 107 | + .orElseThrow(() -> new MojoExecutionException("TODO Bug in code?")); |
| 108 | + |
| 109 | + ProjectBuildingResult result = buildProject(matchingArtifact); |
| 110 | + |
| 111 | + Set<Coordinates> actualDependencies = result.getProject().getArtifacts().stream() |
| 112 | + .map(a -> coordinates(a.getGroupId(), a.getArtifactId())) |
| 113 | + .collect(toSet()); |
| 114 | + |
| 115 | + Set<Coordinates> exclusions = dependency.getExclusions().stream() |
| 116 | + .map(e -> coordinates(e.getGroupId(), e.getArtifactId())) |
| 117 | + .collect(toSet()); |
| 118 | + |
| 119 | + checker.check(currentCoordinates, exclusions, actualDependencies); |
| 120 | + } |
| 121 | + |
| 122 | + if (!checker.getViolations().isEmpty()) { |
| 123 | + if (failOnWarning) { |
| 124 | + logWarnings(checker.getViolations(), (value) -> getLog().error(value)); |
| 125 | + throw new MojoExecutionException("Invalid exclusions found"); |
| 126 | + } else { |
| 127 | + logWarnings(checker.getViolations(), (value) -> getLog().warn(value)); |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + private boolean matchesDependency(Artifact artifact, Dependency dependency) { |
| 133 | + return Objects.equals(artifact.getGroupId(), dependency.getGroupId()) |
| 134 | + && Objects.equals(artifact.getArtifactId(), dependency.getArtifactId()) |
| 135 | + && Objects.equals(artifact.getType(), dependency.getType()) |
| 136 | + && Objects.equals(stripToEmpty(artifact.getClassifier()), stripToEmpty(dependency.getClassifier())); |
| 137 | + } |
| 138 | + |
| 139 | + private void logWarnings(Map<Coordinates, List<Coordinates>> violations, final Consumer<String> logger) { |
| 140 | + logger.accept("The following dependencies defines unnecessary excludes"); |
| 141 | + violations.forEach((dependency, invalidExclusions) -> { |
| 142 | + logger.accept(" " + dependency + ":"); |
| 143 | + invalidExclusions.forEach(invalidExclusion -> { |
| 144 | + logger.accept(" - " + invalidExclusion); |
| 145 | + }); |
| 146 | + }); |
| 147 | + } |
| 148 | + |
| 149 | + private ProjectBuildingResult buildProject(Artifact artifact) throws MojoExecutionException { |
| 150 | + try { |
| 151 | + ProjectBuildingRequest projectBuildingRequest = |
| 152 | + new DefaultProjectBuildingRequest(session.getProjectBuildingRequest()); |
| 153 | + projectBuildingRequest.setResolveDependencies(true); |
| 154 | + return projectBuilder.build(artifact, true, projectBuildingRequest); |
| 155 | + } catch (ProjectBuildingException e) { |
| 156 | + throw new MojoExecutionException( |
| 157 | + format("Failed to build project for %s:%s", artifact.getGroupId(), artifact.getArtifactId()), e); |
| 158 | + } |
| 159 | + } |
| 160 | +} |
0 commit comments