Skip to content

Commit

Permalink
Retain dependencyManagement entries with exclusions (#4387)
Browse files Browse the repository at this point in the history
* Retain dependencyManagement entries with exclusions

* Update rewrite-maven/src/test/java/org/openrewrite/maven/RemoveRedundantDependencyVersionsTest.java
  • Loading branch information
timtebeek authored Aug 7, 2024
1 parent 3804e7e commit 4527f59
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -85,21 +85,21 @@ public class RemoveRedundantDependencyVersions extends Recipe {

@Deprecated
public RemoveRedundantDependencyVersions(@Nullable String groupPattern, @Nullable String artifactPattern,
@Nullable Boolean onlyIfVersionsMatch, @Nullable List<String> except) {
@Nullable Boolean onlyIfVersionsMatch, @Nullable List<String> except) {
this(groupPattern, artifactPattern, onlyIfVersionsMatch, null, except);
}

public RemoveRedundantDependencyVersions(@Nullable String groupPattern, @Nullable String artifactPattern,
@Nullable Comparator onlyIfManagedVersionIs, @Nullable List<String> except) {
@Nullable Comparator onlyIfManagedVersionIs, @Nullable List<String> except) {
this(groupPattern, artifactPattern, null, onlyIfManagedVersionIs, except);
}

@JsonCreator
@Deprecated
@SuppressWarnings("DeprecatedIsStillUsed")
public RemoveRedundantDependencyVersions(@Nullable String groupPattern, @Nullable String artifactPattern,
@Nullable Boolean onlyIfVersionsMatch, @Nullable Comparator onlyIfManagedVersionIs,
@Nullable List<String> except) {
@Nullable Boolean onlyIfVersionsMatch, @Nullable Comparator onlyIfManagedVersionIs,
@Nullable List<String> except) {
this.groupPattern = groupPattern;
this.artifactPattern = artifactPattern;
this.onlyIfVersionsMatch = onlyIfVersionsMatch;
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ void whenNoParentOrOwnDmThenKeepIt() {
<artifactId>myApiApp</artifactId>
<version>1.0.0</version>
</parent>
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
Expand Down Expand Up @@ -550,7 +550,7 @@ void whenNoVersionDefinedThenMakeNoChanges() {
<artifactId>myApiApp</artifactId>
<version>1.0.0</version>
</parent>
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
Expand Down Expand Up @@ -1063,7 +1063,7 @@ void except() {
</dependency>
</dependencies>
</project>
"""
"""
)
);
}
Expand Down Expand Up @@ -1305,11 +1305,11 @@ void pluginPropertySubstitution() {
<version>2.3.0.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.example</groupId>
<artifactId>test</artifactId>
<version>1.0.0-SNAPSHOT</version>
<modelVersion>4.0.0</modelVersion>
<build>
<plugins>
Expand All @@ -1329,11 +1329,11 @@ void pluginPropertySubstitution() {
<version>2.3.0.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.example</groupId>
<artifactId>test</artifactId>
<version>1.0.0-SNAPSHOT</version>
<modelVersion>4.0.0</modelVersion>
<build>
<plugins>
Expand Down Expand Up @@ -1362,13 +1362,13 @@ void dependencyPropertySubstitution() {
<version>2.5.4</version>
<relativePath/>
</parent>
<groupId>com.example</groupId>
<artifactId>test</artifactId>
<version>1.0.0-SNAPSHOT</version>
<modelVersion>4.0.0</modelVersion>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
Expand All @@ -1385,13 +1385,13 @@ void dependencyPropertySubstitution() {
<version>2.5.4</version>
<relativePath/>
</parent>
<groupId>com.example</groupId>
<artifactId>test</artifactId>
<version>1.0.0-SNAPSHOT</version>
<modelVersion>4.0.0</modelVersion>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
Expand Down Expand Up @@ -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(
"""
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.12.RELEASE</version>
</parent>
<groupId>com.example</groupId>
<artifactId>acme</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.2.13.RELEASE</version>
<!-- This exclusion should be kept -->
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
"""
)
);
}
}

0 comments on commit 4527f59

Please sign in to comment.