diff --git a/rewrite-maven/src/main/java/org/openrewrite/maven/RemoveRedundantDependencyVersions.java b/rewrite-maven/src/main/java/org/openrewrite/maven/RemoveRedundantDependencyVersions.java index 1331f27e8bc..917b85715b3 100644 --- a/rewrite-maven/src/main/java/org/openrewrite/maven/RemoveRedundantDependencyVersions.java +++ b/rewrite-maven/src/main/java/org/openrewrite/maven/RemoveRedundantDependencyVersions.java @@ -68,9 +68,9 @@ public class RemoveRedundantDependencyVersions extends Recipe { @Option(displayName = "Only if managed version is ...", description = "Only remove the explicit version if the managed version has the specified comparative relationship to the explicit version. " + - "For example, `gte` will only remove the explicit version if the managed version is the same or newer. " + - "Default `eq`.", - valid = { "any", "eq", "lt", "lte", "gt", "gte" }, + "For example, `gte` will only remove the explicit version if the managed version is the same or newer. " + + "Default `eq`.", + valid = {"any", "eq", "lt", "lte", "gt", "gte"}, required = false) @Nullable Comparator onlyIfManagedVersionIs; @@ -85,12 +85,12 @@ public class RemoveRedundantDependencyVersions extends Recipe { @Deprecated public RemoveRedundantDependencyVersions(@Nullable String groupPattern, @Nullable String artifactPattern, - @Nullable Boolean onlyIfVersionsMatch, @Nullable List except) { + @Nullable Boolean onlyIfVersionsMatch, @Nullable List except) { this(groupPattern, artifactPattern, onlyIfVersionsMatch, null, except); } public RemoveRedundantDependencyVersions(@Nullable String groupPattern, @Nullable String artifactPattern, - @Nullable Comparator onlyIfManagedVersionIs, @Nullable List except) { + @Nullable Comparator onlyIfManagedVersionIs, @Nullable List except) { this(groupPattern, artifactPattern, null, onlyIfManagedVersionIs, except); } @@ -98,8 +98,8 @@ public RemoveRedundantDependencyVersions(@Nullable String groupPattern, @Nullabl @Deprecated @SuppressWarnings("DeprecatedIsStillUsed") public RemoveRedundantDependencyVersions(@Nullable String groupPattern, @Nullable String artifactPattern, - @Nullable Boolean onlyIfVersionsMatch, @Nullable Comparator onlyIfManagedVersionIs, - @Nullable List except) { + @Nullable Boolean onlyIfVersionsMatch, @Nullable Comparator onlyIfManagedVersionIs, + @Nullable List except) { this.groupPattern = groupPattern; this.artifactPattern = artifactPattern; this.onlyIfVersionsMatch = onlyIfVersionsMatch; @@ -194,6 +194,9 @@ public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) { } else if (isManagedDependencyTag()) { ResolvedManagedDependency managed = findManagedDependency(tag); if (managed != null && matchesGroup(managed) && matchesArtifact(managed) && matchesVersion(managed, ctx)) { + if (tag.getChild("exclusions").isPresent()) { + return tag; + } //noinspection DataFlowIssue return null; } diff --git a/rewrite-maven/src/test/java/org/openrewrite/maven/RemoveRedundantDependencyVersionsTest.java b/rewrite-maven/src/test/java/org/openrewrite/maven/RemoveRedundantDependencyVersionsTest.java index 58d31368676..3fa8e1c2328 100644 --- a/rewrite-maven/src/test/java/org/openrewrite/maven/RemoveRedundantDependencyVersionsTest.java +++ b/rewrite-maven/src/test/java/org/openrewrite/maven/RemoveRedundantDependencyVersionsTest.java @@ -501,7 +501,7 @@ void whenNoParentOrOwnDmThenKeepIt() { myApiApp 1.0.0 - + com.google.guava @@ -550,7 +550,7 @@ void whenNoVersionDefinedThenMakeNoChanges() { myApiApp 1.0.0 - + com.google.guava @@ -1063,7 +1063,7 @@ void except() { - """ + """ ) ); } @@ -1305,11 +1305,11 @@ void pluginPropertySubstitution() { 2.3.0.RELEASE - + com.example test 1.0.0-SNAPSHOT - + 4.0.0 @@ -1329,11 +1329,11 @@ void pluginPropertySubstitution() { 2.3.0.RELEASE - + com.example test 1.0.0-SNAPSHOT - + 4.0.0 @@ -1362,13 +1362,13 @@ void dependencyPropertySubstitution() { 2.5.4 - + com.example test 1.0.0-SNAPSHOT - + 4.0.0 - + org.springframework @@ -1385,13 +1385,13 @@ void dependencyPropertySubstitution() { 2.5.4 - + com.example test 1.0.0-SNAPSHOT - + 4.0.0 - + org.springframework @@ -1530,4 +1530,49 @@ void keepVersionIfManagedIsNonExact() { ) ); } + + @Test + @Issue("https://github.com/openrewrite/rewrite/discussions/4386") + void dontOverrideDependencyConfigurations() { + rewriteRun( + spec -> spec.recipe(new RemoveRedundantDependencyVersions(null, null, RemoveRedundantDependencyVersions.Comparator.GTE, null)), + pomXml( + """ + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.3.12.RELEASE + + com.example + acme + 0.0.1-SNAPSHOT + + + + org.springframework.boot + spring-boot-starter-web + 2.2.13.RELEASE + + + + org.slf4j + slf4j-api + + + + + + + + org.springframework.boot + spring-boot-starter-web + + + + """ + ) + ); + } }