diff --git a/rewrite-maven/src/main/resources/META-INF/rewrite/maven.yml b/rewrite-maven/src/main/resources/META-INF/rewrite/maven.yml index 56c3ef6961..1820918238 100644 --- a/rewrite-maven/src/main/resources/META-INF/rewrite/maven.yml +++ b/rewrite-maven/src/main/resources/META-INF/rewrite/maven.yml @@ -26,6 +26,7 @@ recipeList: - org.openrewrite.maven.OrderPomElements - org.openrewrite.maven.RemoveDuplicateDependencies - org.openrewrite.maven.RemoveRedundantDependencyVersions + - org.openrewrite.maven.cleanup.RemoveUnnecessaryExclusions - org.openrewrite.maven.RemoveRedundantProperties: onlyIfValuesMatch: true - org.openrewrite.maven.plugin.DependencyPluginGoalResolveSources @@ -154,3 +155,15 @@ recipeList: filePattern: "**/mvnw.cmd" - org.openrewrite.DeleteSourceFiles: filePattern: "**/.mvn/wrapper/**" +--- +type: specs.openrewrite.org/v1beta/recipe +name: org.openrewrite.maven.cleanup.RemoveUnnecessaryExclusions +displayName: Remove unnecessary exclusions +description: Remove exclusions that are not necessary because the excluded dependency is not a transitive dependency of the project. +recipeList: + - org.openrewrite.maven.RemoveExclusion: + groupId: "*" + artifactId: "*" + exclusionGroupId: "*" + exclusionArtifactId: "*" + onlyIneffective: true diff --git a/rewrite-maven/src/test/java/org/openrewrite/maven/cleanup/RemoveUnnecessaryExclusionsTest.java b/rewrite-maven/src/test/java/org/openrewrite/maven/cleanup/RemoveUnnecessaryExclusionsTest.java new file mode 100644 index 0000000000..856dddc0bd --- /dev/null +++ b/rewrite-maven/src/test/java/org/openrewrite/maven/cleanup/RemoveUnnecessaryExclusionsTest.java @@ -0,0 +1,154 @@ +/* + * Copyright 2025 the original author or authors. + *

+ * 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 + *

+ * https://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.openrewrite.maven.cleanup; + +import org.junit.jupiter.api.Test; +import org.openrewrite.DocumentExample; +import org.openrewrite.test.RecipeSpec; +import org.openrewrite.test.RewriteTest; + +import static org.openrewrite.maven.Assertions.pomXml; + +class RemoveUnnecessaryExclusionsTest implements RewriteTest { + + @Override + public void defaults(RecipeSpec spec) { + spec.recipeFromResource("/META-INF/rewrite/maven.yml", "org.openrewrite.maven.cleanup.RemoveUnnecessaryExclusions"); + } + + @DocumentExample + @Test + void removeIneffectiveExclusion() { + rewriteRun( + pomXml( + """ + + com.mycompany.app + my-app + 1 + + + com.google.guava + guava + 29.0-jre + + + commons-lang + commons-lang + + + + + + """, + """ + + com.mycompany.app + my-app + 1 + + + com.google.guava + guava + 29.0-jre + + + + """ + ) + ); + } + + @Test + void retainEffectiveExclusion() { + rewriteRun( + pomXml( + """ + + com.mycompany.app + my-app + 1 + + + com.google.guava + guava + 29.0-jre + + + com.google.code.findbugs + jsr305 + + + + + + """ + ) + ); + } + + @Test + void removeOnlyIneffectiveExclusionsKeepEffective() { + rewriteRun( + pomXml( + """ + + com.mycompany.app + my-app + 1 + + + com.google.guava + guava + 29.0-jre + + + commons-lang + commons-lang + + + com.google.code.findbugs + jsr305 + + + + + + """, + """ + + com.mycompany.app + my-app + 1 + + + com.google.guava + guava + 29.0-jre + + + com.google.code.findbugs + jsr305 + + + + + + """ + ) + ); + } +}