Skip to content

Commit a08f694

Browse files
committed
Add RemoveUnnecessaryExclusions recipe
Declarative YAML recipe that removes Maven dependency exclusions which are not necessary because the excluded dependency is not a transitive dependency of the project. Added to BestPractices recipe list.
1 parent d4392fc commit a08f694

2 files changed

Lines changed: 167 additions & 0 deletions

File tree

rewrite-maven/src/main/resources/META-INF/rewrite/maven.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ recipeList:
2626
- org.openrewrite.maven.OrderPomElements
2727
- org.openrewrite.maven.RemoveDuplicateDependencies
2828
- org.openrewrite.maven.RemoveRedundantDependencyVersions
29+
- org.openrewrite.maven.cleanup.RemoveUnnecessaryExclusions
2930
- org.openrewrite.maven.RemoveRedundantProperties:
3031
onlyIfValuesMatch: true
3132
- org.openrewrite.maven.plugin.DependencyPluginGoalResolveSources
@@ -154,3 +155,15 @@ recipeList:
154155
filePattern: "**/mvnw.cmd"
155156
- org.openrewrite.DeleteSourceFiles:
156157
filePattern: "**/.mvn/wrapper/**"
158+
---
159+
type: specs.openrewrite.org/v1beta/recipe
160+
name: org.openrewrite.maven.cleanup.RemoveUnnecessaryExclusions
161+
displayName: Remove unnecessary exclusions
162+
description: Remove exclusions that are not necessary because the excluded dependency is not a transitive dependency of the project.
163+
recipeList:
164+
- org.openrewrite.maven.RemoveExclusion:
165+
groupId: "*"
166+
artifactId: "*"
167+
exclusionGroupId: "*"
168+
exclusionArtifactId: "*"
169+
onlyIneffective: true
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
/*
2+
* Copyright 2025 the original author or authors.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.openrewrite.maven.cleanup;
17+
18+
import org.junit.jupiter.api.Test;
19+
import org.openrewrite.DocumentExample;
20+
import org.openrewrite.test.RecipeSpec;
21+
import org.openrewrite.test.RewriteTest;
22+
23+
import static org.openrewrite.maven.Assertions.pomXml;
24+
25+
class RemoveUnnecessaryExclusionsTest implements RewriteTest {
26+
27+
@Override
28+
public void defaults(RecipeSpec spec) {
29+
spec.recipeFromResource("/META-INF/rewrite/maven.yml", "org.openrewrite.maven.cleanup.RemoveUnnecessaryExclusions");
30+
}
31+
32+
@DocumentExample
33+
@Test
34+
void removeIneffectiveExclusion() {
35+
rewriteRun(
36+
pomXml(
37+
"""
38+
<project>
39+
<groupId>com.mycompany.app</groupId>
40+
<artifactId>my-app</artifactId>
41+
<version>1</version>
42+
<dependencies>
43+
<dependency>
44+
<groupId>com.google.guava</groupId>
45+
<artifactId>guava</artifactId>
46+
<version>29.0-jre</version>
47+
<exclusions>
48+
<exclusion>
49+
<groupId>commons-lang</groupId>
50+
<artifactId>commons-lang</artifactId>
51+
</exclusion>
52+
</exclusions>
53+
</dependency>
54+
</dependencies>
55+
</project>
56+
""",
57+
"""
58+
<project>
59+
<groupId>com.mycompany.app</groupId>
60+
<artifactId>my-app</artifactId>
61+
<version>1</version>
62+
<dependencies>
63+
<dependency>
64+
<groupId>com.google.guava</groupId>
65+
<artifactId>guava</artifactId>
66+
<version>29.0-jre</version>
67+
</dependency>
68+
</dependencies>
69+
</project>
70+
"""
71+
)
72+
);
73+
}
74+
75+
@Test
76+
void retainEffectiveExclusion() {
77+
rewriteRun(
78+
pomXml(
79+
"""
80+
<project>
81+
<groupId>com.mycompany.app</groupId>
82+
<artifactId>my-app</artifactId>
83+
<version>1</version>
84+
<dependencies>
85+
<dependency>
86+
<groupId>com.google.guava</groupId>
87+
<artifactId>guava</artifactId>
88+
<version>29.0-jre</version>
89+
<exclusions>
90+
<exclusion>
91+
<groupId>com.google.code.findbugs</groupId>
92+
<artifactId>jsr305</artifactId>
93+
</exclusion>
94+
</exclusions>
95+
</dependency>
96+
</dependencies>
97+
</project>
98+
"""
99+
)
100+
);
101+
}
102+
103+
@Test
104+
void removeOnlyIneffectiveExclusionsKeepEffective() {
105+
rewriteRun(
106+
pomXml(
107+
"""
108+
<project>
109+
<groupId>com.mycompany.app</groupId>
110+
<artifactId>my-app</artifactId>
111+
<version>1</version>
112+
<dependencies>
113+
<dependency>
114+
<groupId>com.google.guava</groupId>
115+
<artifactId>guava</artifactId>
116+
<version>29.0-jre</version>
117+
<exclusions>
118+
<exclusion>
119+
<groupId>commons-lang</groupId>
120+
<artifactId>commons-lang</artifactId>
121+
</exclusion>
122+
<exclusion>
123+
<groupId>com.google.code.findbugs</groupId>
124+
<artifactId>jsr305</artifactId>
125+
</exclusion>
126+
</exclusions>
127+
</dependency>
128+
</dependencies>
129+
</project>
130+
""",
131+
"""
132+
<project>
133+
<groupId>com.mycompany.app</groupId>
134+
<artifactId>my-app</artifactId>
135+
<version>1</version>
136+
<dependencies>
137+
<dependency>
138+
<groupId>com.google.guava</groupId>
139+
<artifactId>guava</artifactId>
140+
<version>29.0-jre</version>
141+
<exclusions>
142+
<exclusion>
143+
<groupId>com.google.code.findbugs</groupId>
144+
<artifactId>jsr305</artifactId>
145+
</exclusion>
146+
</exclusions>
147+
</dependency>
148+
</dependencies>
149+
</project>
150+
"""
151+
)
152+
);
153+
}
154+
}

0 commit comments

Comments
 (0)