diff --git a/rewrite-maven/src/main/java/org/openrewrite/maven/ChangeDependencyGroupIdAndArtifactId.java b/rewrite-maven/src/main/java/org/openrewrite/maven/ChangeDependencyGroupIdAndArtifactId.java index b8196b41787..ac0227ec140 100755 --- a/rewrite-maven/src/main/java/org/openrewrite/maven/ChangeDependencyGroupIdAndArtifactId.java +++ b/rewrite-maven/src/main/java/org/openrewrite/maven/ChangeDependencyGroupIdAndArtifactId.java @@ -182,9 +182,10 @@ public Xml visitTag(Xml.Tag tag, ExecutionContext ctx) { } else { artifactId = t.getChildValue("artifactId").orElseThrow(NoSuchElementException::new); } + String currentVersion = t.getChildValue("version").orElse(null); if (newVersion != null) { try { - String resolvedNewVersion = resolveSemverVersion(ctx, groupId, artifactId); + String resolvedNewVersion = resolveSemverVersion(ctx, groupId, artifactId, currentVersion); Optional scopeTag = t.getChild("scope"); Scope scope = scopeTag.map(xml -> Scope.fromName(xml.getValue().orElse("compile"))).orElse(Scope.Compile); Optional versionTag = t.getChild("version"); @@ -242,15 +243,16 @@ private boolean isDependencyManaged(Scope scope, String groupId, String artifact } @SuppressWarnings("ConstantConditions") - private String resolveSemverVersion(ExecutionContext ctx, String groupId, String artifactId) throws MavenDownloadingException { + private String resolveSemverVersion(ExecutionContext ctx, String groupId, String artifactId, @Nullable String currentVersion) throws MavenDownloadingException { if (versionComparator == null) { return newVersion; } + String finalCurrentVersion = currentVersion != null ? currentVersion : newVersion; if (availableVersions == null) { availableVersions = new ArrayList<>(); MavenMetadata mavenMetadata = metadataFailures.insertRows(ctx, () -> downloadMetadata(groupId, artifactId, ctx)); for (String v : mavenMetadata.getVersioning().getVersions()) { - if (versionComparator.isValid(newVersion, v)) { + if (versionComparator.isValid(finalCurrentVersion, v)) { availableVersions.add(v); } } diff --git a/rewrite-maven/src/main/java/org/openrewrite/maven/ChangeManagedDependencyGroupIdAndArtifactId.java b/rewrite-maven/src/main/java/org/openrewrite/maven/ChangeManagedDependencyGroupIdAndArtifactId.java index da0389461af..96f88519a30 100644 --- a/rewrite-maven/src/main/java/org/openrewrite/maven/ChangeManagedDependencyGroupIdAndArtifactId.java +++ b/rewrite-maven/src/main/java/org/openrewrite/maven/ChangeManagedDependencyGroupIdAndArtifactId.java @@ -153,11 +153,10 @@ public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) { } if (newVersion != null) { try { - String resolvedNewVersion = resolveSemverVersion(ctx, newGroupId, newArtifactId); - Optional versionTag = t.getChild("version"); if (versionTag.isPresent()) { + String resolvedNewVersion = resolveSemverVersion(ctx, newGroupId, newArtifactId, versionTag.get().getValue().orElse(null)); t = (Xml.Tag) new ChangeTagValueVisitor<>(versionTag.get(), resolvedNewVersion).visitNonNull(t, 0, getCursor().getParentOrThrow()); } changed = true; @@ -174,15 +173,16 @@ public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) { } @SuppressWarnings("ConstantConditions") - private String resolveSemverVersion(ExecutionContext ctx, String groupId, String artifactId) throws MavenDownloadingException { + private String resolveSemverVersion(ExecutionContext ctx, String groupId, String artifactId, @Nullable String currentVersion) throws MavenDownloadingException { if (versionComparator == null) { return newVersion; } + String finalCurrentVersion = currentVersion != null ? currentVersion : newVersion; if (availableVersions == null) { availableVersions = new ArrayList<>(); MavenMetadata mavenMetadata = metadataFailures.insertRows(ctx, () -> downloadMetadata(groupId, artifactId, ctx)); for (String v : mavenMetadata.getVersioning().getVersions()) { - if (versionComparator.isValid(newVersion, v)) { + if (versionComparator.isValid(finalCurrentVersion, v)) { availableVersions.add(v); } } diff --git a/rewrite-maven/src/test/java/org/openrewrite/maven/ChangeDependencyGroupIdAndArtifactIdTest.java b/rewrite-maven/src/test/java/org/openrewrite/maven/ChangeDependencyGroupIdAndArtifactIdTest.java index aca6d239d9d..ed923a7ced6 100644 --- a/rewrite-maven/src/test/java/org/openrewrite/maven/ChangeDependencyGroupIdAndArtifactIdTest.java +++ b/rewrite-maven/src/test/java/org/openrewrite/maven/ChangeDependencyGroupIdAndArtifactIdTest.java @@ -703,6 +703,52 @@ void unmanagedToManagedExternalizedDepMgmt() { ); } + @Test + void latestPatch() { + rewriteRun( + spec -> spec.recipe(new ChangeDependencyGroupIdAndArtifactId( + "javax.activation", + "javax.activation-api", + "jakarta.activation", + "jakarta.activation-api", + "latest.patch", + null + )), + pomXml( + """ + + 4.0.0 + com.mycompany.app + my-app + 1 + + + javax.activation + javax.activation-api + 1.2.0 + + + + """, + """ + + 4.0.0 + com.mycompany.app + my-app + 1 + + + jakarta.activation + jakarta.activation-api + 1.2.2 + + + + """ + ) + ); + } + @Test void changeOnlyArtifactId() { rewriteRun( diff --git a/rewrite-maven/src/test/java/org/openrewrite/maven/ChangeManagedDependencyGroupIdAndArtifactIdTest.java b/rewrite-maven/src/test/java/org/openrewrite/maven/ChangeManagedDependencyGroupIdAndArtifactIdTest.java index 79d18e92325..5006e8412c0 100644 --- a/rewrite-maven/src/test/java/org/openrewrite/maven/ChangeManagedDependencyGroupIdAndArtifactIdTest.java +++ b/rewrite-maven/src/test/java/org/openrewrite/maven/ChangeManagedDependencyGroupIdAndArtifactIdTest.java @@ -126,4 +126,54 @@ void changeManagedDependencyWithDynamicVersion() { ) ); } + + @Test + void latestPatch() { + rewriteRun( + spec -> spec.recipe(new ChangeManagedDependencyGroupIdAndArtifactId( + "javax.activation", + "javax.activation-api", + "jakarta.activation", + "jakarta.activation-api", + "latest.patch", + null + )), + pomXml( + """ + + 4.0.0 + com.mycompany.app + my-app + 1 + + + + javax.activation + javax.activation-api + 1.2.0 + + + + + """, + """ + + 4.0.0 + com.mycompany.app + my-app + 1 + + + + jakarta.activation + jakarta.activation-api + 1.2.2 + + + + + """ + ) + ); + } }