From 47cb38a1d1faa8b71c98e4f6d4add97da68a919f Mon Sep 17 00:00:00 2001 From: marcel-gepardec Date: Thu, 11 Jul 2024 17:23:41 +0200 Subject: [PATCH 1/6] Added version migration for properties section in ChangeDependencyGroupIdAndArtifactId and ChangeManagedDependencyGroupIdAndArtifactId --- .../ChangeDependencyGroupIdAndArtifactId.java | 66 +++- ...ManagedDependencyGroupIdAndArtifactId.java | 53 ++- ...ngeDependencyGroupIdAndArtifactIdTest.java | 338 ++++++++++++++++++ ...gedDependencyGroupIdAndArtifactIdTest.java | 187 ++++++++++ 4 files changed, 623 insertions(+), 21 deletions(-) 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 30a0467d16e..1dbd466fac1 100755 --- a/rewrite-maven/src/main/java/org/openrewrite/maven/ChangeDependencyGroupIdAndArtifactId.java +++ b/rewrite-maven/src/main/java/org/openrewrite/maven/ChangeDependencyGroupIdAndArtifactId.java @@ -94,12 +94,22 @@ public class ChangeDependencyGroupIdAndArtifactId extends Recipe { @Nullable Boolean changeManagedDependency; + @Option(displayName = "Update dependency management", + description = "Also update the dependency management section. The default for this flag is `true`.", + required = false) + @Nullable + Boolean changePropertyVersionNames; + public ChangeDependencyGroupIdAndArtifactId(String oldGroupId, String oldArtifactId, @Nullable String newGroupId, @Nullable String newArtifactId, @Nullable String newVersion, @Nullable String versionPattern) { - this(oldGroupId, oldArtifactId, newGroupId, newArtifactId, newVersion, versionPattern, false, true); + this(oldGroupId, oldArtifactId, newGroupId, newArtifactId, newVersion, versionPattern, false, true, true); } - @JsonCreator public ChangeDependencyGroupIdAndArtifactId(String oldGroupId, String oldArtifactId, @Nullable String newGroupId, @Nullable String newArtifactId, @Nullable String newVersion, @Nullable String versionPattern, @Nullable Boolean overrideManagedVersion, @Nullable Boolean changeManagedDependency) { + this(oldGroupId, oldArtifactId, newGroupId, newArtifactId, newVersion, versionPattern, overrideManagedVersion, changeManagedDependency, true); + } + + @JsonCreator + public ChangeDependencyGroupIdAndArtifactId(String oldGroupId, String oldArtifactId, @Nullable String newGroupId, @Nullable String newArtifactId, @Nullable String newVersion, @Nullable String versionPattern, @Nullable Boolean overrideManagedVersion, @Nullable Boolean changeManagedDependency, @Nullable Boolean changePropertyVersionNames) { this.oldGroupId = oldGroupId; this.oldArtifactId = oldArtifactId; this.newGroupId = newGroupId; @@ -108,6 +118,7 @@ public ChangeDependencyGroupIdAndArtifactId(String oldGroupId, String oldArtifac this.versionPattern = versionPattern; this.overrideManagedVersion = overrideManagedVersion; this.changeManagedDependency = changeManagedDependency; + this.changePropertyVersionNames = changePropertyVersionNames; } @Override @@ -156,13 +167,13 @@ public TreeVisitor getVisitor() { @Override public Xml visitDocument(Xml.Document document, ExecutionContext ctx) { - // Any managed dependency change is unlikely to use the same version, so only update selectively. - if ((changeManagedDependency == null || changeManagedDependency) && newVersion != null || versionPattern != null) { + // Any managed dependency change is unlikely to use the same version, so only update selectively. If the new dependency is already managed, don't change the old dependency. It will be removed at the end. + if ((changeManagedDependency == null || changeManagedDependency) && newVersion != null && !isDependencyManaged(newGroupId, newArtifactId, newVersion)) { doAfterVisit(new ChangeManagedDependencyGroupIdAndArtifactId( oldGroupId, oldArtifactId, Optional.ofNullable(newGroupId).orElse(oldGroupId), Optional.ofNullable(newArtifactId).orElse(oldArtifactId), - newVersion, versionPattern).getVisitor()); + newVersion, versionPattern, changePropertyVersionNames).getVisitor()); } return super.visitDocument(document, ctx); } @@ -203,13 +214,35 @@ public Xml visitTag(Xml.Tag tag, ExecutionContext ctx) { t = (Xml.Tag) new RemoveContentVisitor<>(versionTag.get(), false).visit(t, ctx); } else { // Otherwise, change the version to the new value. - t = changeChildTagValue(t, "version", resolvedNewVersion, ctx); + if (Objects.requireNonNull(currentVersion).contains("$")) { + // If the version is a property value, change version in property section and rename the property if changePropertyNames is true. + String propertyVariable = currentVersion.substring(2, currentVersion.length() - 1); + String newPropertyVariable = propertyVariable; + if (Boolean.TRUE.equals(changePropertyVersionNames)){ + newPropertyVariable = newArtifactId + ".version"; + doAfterVisit(new RenamePropertyKey(propertyVariable, newPropertyVariable).getVisitor()); + t = changeChildTagValue(t, "version", "${"+newPropertyVariable+"}", ctx); + } + doAfterVisit(new ChangePropertyValue(newPropertyVariable, resolvedNewVersion, false, false).getVisitor()); + } else { + //If the version is no property value, change it right here. + t = changeChildTagValue(t, "version", resolvedNewVersion, ctx); + } + } - } else if (configuredToOverrideManageVersion || !newDependencyManaged) { - //If the version is not present, add the version if we are explicitly overriding a managed version or if no managed version exists. - Xml.Tag newVersionTag = Xml.Tag.build("" + resolvedNewVersion + ""); - //noinspection ConstantConditions - t = (Xml.Tag) new AddToTagVisitor(t, newVersionTag, new MavenTagInsertionComparator(t.getChildren())).visitNonNull(t, ctx, getCursor().getParent()); + } else if (!(newDependencyManaged && configuredToChangeManagedDependency)) { + //If the Version not present and the new version is not managed and/or the changeManagedDependency Option is false. + List md = getResolutionResult().getPom().getDependencyManagement(); + if (configuredToOverrideManageVersion || ((!md.isEmpty() && md.get(0).getRequestedBom() != null) && !newDependencyManaged) || !configuredToChangeManagedDependency) { + //If the version is not present, add the version if we are explicitly overriding a managed version, if no managed version exists with no collection of dependencies or changeManageDependency is false. + Xml.Tag newVersionTag = Xml.Tag.build("" + resolvedNewVersion + ""); + //noinspection ConstantConditions + t = (Xml.Tag) new AddToTagVisitor(t, newVersionTag, new MavenTagInsertionComparator(t.getChildren())).visitNonNull(t, ctx, getCursor().getParent()); + } + + } else if (oldDependencyManaged){ + //If the version is not present and the new and old version are managed, remove the old dependency + doAfterVisit(new RemoveManagedDependency(oldGroupId,oldArtifactId, null).getVisitor()); } } catch (MavenDownloadingException e) { return e.warn(tag); @@ -243,6 +276,17 @@ private boolean isDependencyManaged(Scope scope, String groupId, String artifact return false; } + private boolean isDependencyManaged(String groupId, String artifactId, String version) { + + MavenResolutionResult result = getResolutionResult(); + for (ResolvedManagedDependency managedDependency : result.getPom().getDependencyManagement()) { + if (groupId.equals(managedDependency.getGroupId()) && artifactId.equals(managedDependency.getArtifactId()) && version.equals(managedDependency.getVersion())) { + return true; + } + } + return false; + } + @SuppressWarnings("ConstantConditions") private String resolveSemverVersion(ExecutionContext ctx, String groupId, String artifactId, @Nullable String currentVersion) throws MavenDownloadingException { if (versionComparator == null) { 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 19c6744d118..060e759e5cd 100644 --- a/rewrite-maven/src/main/java/org/openrewrite/maven/ChangeManagedDependencyGroupIdAndArtifactId.java +++ b/rewrite-maven/src/main/java/org/openrewrite/maven/ChangeManagedDependencyGroupIdAndArtifactId.java @@ -72,29 +72,36 @@ public class ChangeManagedDependencyGroupIdAndArtifactId extends Recipe { @Option(displayName = "Version pattern", description = "Allows version selection to be extended beyond the original Node Semver semantics. So for example," + - "Setting 'version' to \"25-29\" can be paired with a metadata pattern of \"-jre\" to select Guava 29.0-jre", + "Setting 'version' to \"25-29\" can be paired with a metadata pattern of \"-jre\" to select Guava 29.0-jre", example = "-jre", required = false) @Nullable String versionPattern; + @Option(displayName = "Change property version names", + description = "Allows property version names to be changed to best practice naming convention", + example = "-jre", + required = false) + @Nullable + Boolean changePropertyVersionNames; + public ChangeManagedDependencyGroupIdAndArtifactId(String oldGroupId, String oldArtifactId, String newGroupId, String newArtifactId, @Nullable String newVersion) { - this.oldGroupId = oldGroupId; - this.oldArtifactId = oldArtifactId; - this.newGroupId = newGroupId; - this.newArtifactId = newArtifactId; - this.newVersion = newVersion; - this.versionPattern = null; + this(oldGroupId, oldArtifactId, newGroupId, newArtifactId, newVersion, null, true); } - @JsonCreator public ChangeManagedDependencyGroupIdAndArtifactId(String oldGroupId, String oldArtifactId, String newGroupId, String newArtifactId, @Nullable String newVersion, @Nullable String versionPattern) { + this(oldGroupId, oldArtifactId, newGroupId, newArtifactId, newVersion, versionPattern, true); + } + + @JsonCreator + public ChangeManagedDependencyGroupIdAndArtifactId(String oldGroupId, String oldArtifactId, String newGroupId, String newArtifactId, @Nullable String newVersion, @Nullable String versionPattern, @Nullable Boolean changePropertyVersionNames) { this.oldGroupId = oldGroupId; this.oldArtifactId = oldArtifactId; this.newGroupId = newGroupId; this.newArtifactId = newArtifactId; this.newVersion = newVersion; this.versionPattern = versionPattern; + this.changePropertyVersionNames = changePropertyVersionNames; } @Override @@ -156,8 +163,21 @@ public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) { 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()); + String version = versionTag.get().getValue().orElse(null); + String resolvedNewVersion = resolveSemverVersion(ctx, newGroupId, newArtifactId, version); + if (Objects.requireNonNull(version).contains("$")) { + String propertyVariable = version.substring(2, version.length() - 1); + String newPropertyVariable = propertyVariable; + if (Boolean.TRUE.equals(changePropertyVersionNames)){ + newPropertyVariable = newArtifactId + ".version"; + doAfterVisit(new RenamePropertyKey(propertyVariable, newPropertyVariable).getVisitor()); + t = changeChildTagValue(t, "version", "${"+newPropertyVariable+"}", ctx); + } + doAfterVisit(new ChangePropertyValue(newPropertyVariable, resolvedNewVersion, false, false).getVisitor()); + } else { + t = changeChildTagValue(t, "version", resolvedNewVersion, ctx); + } + } changed = true; } catch(MavenDownloadingException e) { @@ -172,8 +192,21 @@ public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) { return t; } + private Xml.Tag changeChildTagValue(Xml.Tag tag, String childTagName, String newValue, ExecutionContext ctx) { + Optional childTag = tag.getChild(childTagName); + if (childTag.isPresent() && !newValue.equals(childTag.get().getValue().orElse(null))) { + tag = (Xml.Tag) new ChangeTagValueVisitor<>(childTag.get(), newValue).visitNonNull(tag, ctx); + } + return tag; + } + @SuppressWarnings("ConstantConditions") private String resolveSemverVersion(ExecutionContext ctx, String groupId, String artifactId, @Nullable String currentVersion) throws MavenDownloadingException { + if (currentVersion.contains("$")){ + String propertyVariable = currentVersion.substring(2, currentVersion.length() - 1); + currentVersion = getResolutionResult().getPom().getProperties().get(propertyVariable); + } + if (versionComparator == null) { return newVersion; } 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 c161d490a51..6d86db06f5d 100644 --- a/rewrite-maven/src/test/java/org/openrewrite/maven/ChangeDependencyGroupIdAndArtifactIdTest.java +++ b/rewrite-maven/src/test/java/org/openrewrite/maven/ChangeDependencyGroupIdAndArtifactIdTest.java @@ -1194,6 +1194,73 @@ void changeGroupIdOnWildcardArtifacts() { ); } + @Test + void removeUnusedDependencyInManagedDependency() { + rewriteRun( + spec -> spec.recipe(new ChangeDependencyGroupIdAndArtifactId( + "javax.activation", + "javax.activation-api", + "jakarta.activation", + "jakarta.activation-api", + "2.1.0", + null + )), + pomXml( + """ + + 4.0.0 + com.mycompany.app + my-app + 1 + + + + javax.activation + javax.activation-api + 1.2.0 + + + jakarta.activation + jakarta.activation-api + 2.1.0 + + + + + + javax.activation + javax.activation-api + + + + """, + """ + + 4.0.0 + com.mycompany.app + my-app + 1 + + + + jakarta.activation + jakarta.activation-api + 2.1.0 + + + + + + jakarta.activation + jakarta.activation-api + + + + """ + ) + ); + } + @Test void changeProfileDependencyGroupIdAndArtifactId() { rewriteRun( @@ -1247,4 +1314,275 @@ void changeProfileDependencyGroupIdAndArtifactId() { ) ); } + + @Test + void changePropertyVersionProfileManagedDependency() { + rewriteRun( + spec -> spec.recipe(new ChangeDependencyGroupIdAndArtifactId( + "javax.activation", + "javax.activation-api", + "jakarta.activation", + "jakarta.activation-api", + "2.1.0", + null + )), + pomXml( + """ + + 4.0.0 + com.mycompany.app + my-app + 1 + + 1.2.0 + + + + test + + + + javax.activation + javax.activation-api + ${javax.activation.version} + + + + + + + """, + """ + + 4.0.0 + com.mycompany.app + my-app + 1 + + 2.1.0 + + + + test + + + + jakarta.activation + jakarta.activation-api + ${jakarta.activation-api.version} + + + + + + + """ + ) + ); + } + + @Test + void changePropertyVersionManagedDependency() { + rewriteRun( + spec -> spec.recipe(new ChangeDependencyGroupIdAndArtifactId( + "javax.activation", + "javax.activation-api", + "jakarta.activation", + "jakarta.activation-api", + "2.1.0", + null + )), + pomXml( + """ + + 4.0.0 + com.mycompany.app + my-app + 1 + + 1.2.0 + + + + + javax.activation + javax.activation-api + ${javax.activation.version} + + + + + + test + + + javax.activation + javax.activation-api + + + + + + """, + """ + + 4.0.0 + com.mycompany.app + my-app + 1 + + 2.1.0 + + + + + jakarta.activation + jakarta.activation-api + ${jakarta.activation-api.version} + + + + + + test + + + jakarta.activation + jakarta.activation-api + + + + + + """ + ) + ); + } + + @Test + void changePropertyVersionProfileDependency() { + rewriteRun( + spec -> spec.recipe(new ChangeDependencyGroupIdAndArtifactId( + "javax.activation", + "javax.activation-api", + "jakarta.activation", + "jakarta.activation-api", + "2.1.0", + null + )), + pomXml( + """ + + 4.0.0 + com.mycompany.app + my-app + 1 + + 1.2.0 + + + + test + + + javax.activation + javax.activation-api + ${javax.activation.version} + + + + + + """, + """ + + 4.0.0 + com.mycompany.app + my-app + 1 + + 2.1.0 + + + + test + + + jakarta.activation + jakarta.activation-api + ${jakarta.activation-api.version} + + + + + + """ + ) + ); + } + + @Test + void changePropertyVersionProfileDependencyWithChangePropertyNamesFalse() { + rewriteRun( + spec -> spec.recipe(new ChangeDependencyGroupIdAndArtifactId( + "javax.activation", + "javax.activation-api", + "jakarta.activation", + "jakarta.activation-api", + "2.1.0", + null, + false, + true, + false + )), + pomXml( + """ + + 4.0.0 + com.mycompany.app + my-app + 1 + + 1.2.0 + + + + test + + + javax.activation + javax.activation-api + ${javax.activation.version} + + + + + + """, + """ + + 4.0.0 + com.mycompany.app + my-app + 1 + + 2.1.0 + + + + test + + + jakarta.activation + jakarta.activation-api + ${javax.activation.version} + + + + + + """ + ) + ); + } } 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 b1458dda26d..d6603523c74 100644 --- a/rewrite-maven/src/test/java/org/openrewrite/maven/ChangeManagedDependencyGroupIdAndArtifactIdTest.java +++ b/rewrite-maven/src/test/java/org/openrewrite/maven/ChangeManagedDependencyGroupIdAndArtifactIdTest.java @@ -177,6 +177,61 @@ void latestPatchMangedDependency() { ); } + @Test + void changePropertyVersionManagedDependency() { + rewriteRun( + spec -> spec.recipe(new ChangeManagedDependencyGroupIdAndArtifactId( + "javax.activation", + "javax.activation-api", + "jakarta.activation", + "jakarta.activation-api", + "2.1.0" + )), + pomXml( + """ + + 4.0.0 + com.mycompany.app + my-app + 1 + + 1.2.0 + + + + + javax.activation + javax.activation-api + ${javax.activation.version} + + + + + """, + """ + + 4.0.0 + com.mycompany.app + my-app + 1 + + 2.1.0 + + + + + jakarta.activation + jakarta.activation-api + ${jakarta.activation-api.version} + + + + + """ + ) + ); + } + @Test void changeProfileManagedDependencyGroupIdAndArtifactId() { rewriteRun( @@ -233,4 +288,136 @@ void changeProfileManagedDependencyGroupIdAndArtifactId() { ) ); } + + @Test + void changePropertyVersionProfileManagedDependency() { + rewriteRun( + spec -> spec.recipe(new ChangeManagedDependencyGroupIdAndArtifactId( + "javax.activation", + "javax.activation-api", + "jakarta.activation", + "jakarta.activation-api", + "2.1.0" + )), + pomXml( + """ + + 4.0.0 + com.mycompany.app + my-app + 1 + + 1.2.0 + + + + test + + + + javax.activation + javax.activation-api + ${javax.activation.version} + + + + + + + """, + """ + + 4.0.0 + com.mycompany.app + my-app + 1 + + 2.1.0 + + + + test + + + + jakarta.activation + jakarta.activation-api + ${jakarta.activation-api.version} + + + + + + + """ + ) + ); + } + + @Test + void changePropertyVersionProfileManagedDependencyWithChangePropertyVersionName() { + rewriteRun( + spec -> spec.recipe(new ChangeManagedDependencyGroupIdAndArtifactId( + "javax.activation", + "javax.activation-api", + "jakarta.activation", + "jakarta.activation-api", + "2.1.0", + null, + false + )), + pomXml( + """ + + 4.0.0 + com.mycompany.app + my-app + 1 + + 1.2.0 + + + + test + + + + javax.activation + javax.activation-api + ${javax.activation.version} + + + + + + + """, + """ + + 4.0.0 + com.mycompany.app + my-app + 1 + + 2.1.0 + + + + test + + + + jakarta.activation + jakarta.activation-api + ${javax.activation.version} + + + + + + + """ + ) + ); + } } From 91a66dac55de57dc56b4d4b7312de2fbf2249c7a Mon Sep 17 00:00:00 2001 From: marcel-gepardec Date: Fri, 12 Jul 2024 14:06:21 +0200 Subject: [PATCH 2/6] changed changePropertyVersionNames option per default on false --- .../maven/ChangeDependencyGroupIdAndArtifactId.java | 4 ++-- .../ChangeDependencyGroupIdAndArtifactIdTest.java | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) 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 1dbd466fac1..4da3c55ccf4 100755 --- a/rewrite-maven/src/main/java/org/openrewrite/maven/ChangeDependencyGroupIdAndArtifactId.java +++ b/rewrite-maven/src/main/java/org/openrewrite/maven/ChangeDependencyGroupIdAndArtifactId.java @@ -101,11 +101,11 @@ public class ChangeDependencyGroupIdAndArtifactId extends Recipe { Boolean changePropertyVersionNames; public ChangeDependencyGroupIdAndArtifactId(String oldGroupId, String oldArtifactId, @Nullable String newGroupId, @Nullable String newArtifactId, @Nullable String newVersion, @Nullable String versionPattern) { - this(oldGroupId, oldArtifactId, newGroupId, newArtifactId, newVersion, versionPattern, false, true, true); + this(oldGroupId, oldArtifactId, newGroupId, newArtifactId, newVersion, versionPattern, false, true, false); } public ChangeDependencyGroupIdAndArtifactId(String oldGroupId, String oldArtifactId, @Nullable String newGroupId, @Nullable String newArtifactId, @Nullable String newVersion, @Nullable String versionPattern, @Nullable Boolean overrideManagedVersion, @Nullable Boolean changeManagedDependency) { - this(oldGroupId, oldArtifactId, newGroupId, newArtifactId, newVersion, versionPattern, overrideManagedVersion, changeManagedDependency, true); + this(oldGroupId, oldArtifactId, newGroupId, newArtifactId, newVersion, versionPattern, overrideManagedVersion, changeManagedDependency, false); } @JsonCreator 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 6d86db06f5d..4980351bc11 100644 --- a/rewrite-maven/src/test/java/org/openrewrite/maven/ChangeDependencyGroupIdAndArtifactIdTest.java +++ b/rewrite-maven/src/test/java/org/openrewrite/maven/ChangeDependencyGroupIdAndArtifactIdTest.java @@ -1359,7 +1359,7 @@ void changePropertyVersionProfileManagedDependency() { my-app 1 - 2.1.0 + 2.1.0 @@ -1369,7 +1369,7 @@ void changePropertyVersionProfileManagedDependency() { jakarta.activation jakarta.activation-api - ${jakarta.activation-api.version} + ${javax.activation.version} @@ -1431,14 +1431,14 @@ void changePropertyVersionManagedDependency() { my-app 1 - 2.1.0 + 2.1.0 jakarta.activation jakarta.activation-api - ${jakarta.activation-api.version} + ${javax.activation.version} @@ -1501,7 +1501,7 @@ void changePropertyVersionProfileDependency() { my-app 1 - 2.1.0 + 2.1.0 @@ -1510,7 +1510,7 @@ void changePropertyVersionProfileDependency() { jakarta.activation jakarta.activation-api - ${jakarta.activation-api.version} + ${javax.activation.version} From 0807d5678e063ea3d24b61dc37a67b8633aa5648 Mon Sep 17 00:00:00 2001 From: marcel-gepardec Date: Mon, 15 Jul 2024 13:44:51 +0200 Subject: [PATCH 3/6] changed default value for changePropertyVersionNames in ChangeManagedDependencyGroupIdAndArtifactId --- .../ChangeManagedDependencyGroupIdAndArtifactId.java | 4 ++-- .../ChangeManagedDependencyGroupIdAndArtifactIdTest.java | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) 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 060e759e5cd..6b54dceb2e6 100644 --- a/rewrite-maven/src/main/java/org/openrewrite/maven/ChangeManagedDependencyGroupIdAndArtifactId.java +++ b/rewrite-maven/src/main/java/org/openrewrite/maven/ChangeManagedDependencyGroupIdAndArtifactId.java @@ -86,11 +86,11 @@ public class ChangeManagedDependencyGroupIdAndArtifactId extends Recipe { Boolean changePropertyVersionNames; public ChangeManagedDependencyGroupIdAndArtifactId(String oldGroupId, String oldArtifactId, String newGroupId, String newArtifactId, @Nullable String newVersion) { - this(oldGroupId, oldArtifactId, newGroupId, newArtifactId, newVersion, null, true); + this(oldGroupId, oldArtifactId, newGroupId, newArtifactId, newVersion, null, false); } public ChangeManagedDependencyGroupIdAndArtifactId(String oldGroupId, String oldArtifactId, String newGroupId, String newArtifactId, @Nullable String newVersion, @Nullable String versionPattern) { - this(oldGroupId, oldArtifactId, newGroupId, newArtifactId, newVersion, versionPattern, true); + this(oldGroupId, oldArtifactId, newGroupId, newArtifactId, newVersion, versionPattern, false); } @JsonCreator 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 d6603523c74..05a5e49ea19 100644 --- a/rewrite-maven/src/test/java/org/openrewrite/maven/ChangeManagedDependencyGroupIdAndArtifactIdTest.java +++ b/rewrite-maven/src/test/java/org/openrewrite/maven/ChangeManagedDependencyGroupIdAndArtifactIdTest.java @@ -215,14 +215,14 @@ void changePropertyVersionManagedDependency() { my-app 1 - 2.1.0 + 2.1.0 jakarta.activation jakarta.activation-api - ${jakarta.activation-api.version} + ${javax.activation.version} @@ -332,7 +332,7 @@ void changePropertyVersionProfileManagedDependency() { my-app 1 - 2.1.0 + 2.1.0 @@ -342,7 +342,7 @@ void changePropertyVersionProfileManagedDependency() { jakarta.activation jakarta.activation-api - ${jakarta.activation-api.version} + ${javax.activation.version} From ca036dfa49fc76be48c080a7d92868ac496e3705 Mon Sep 17 00:00:00 2001 From: marcel-gepardec Date: Thu, 25 Jul 2024 14:29:09 +0200 Subject: [PATCH 4/6] Added new recipe implementation for changing a version value within a dependency --- .../ChangeDependencyGroupIdAndArtifactId.java | 16 +- ...ManagedDependencyGroupIdAndArtifactId.java | 67 +-- .../openrewrite/maven/ChangeVersionValue.java | 267 +++++++++ .../maven/ChangeVersionValueTest.java | 530 ++++++++++++++++++ 4 files changed, 802 insertions(+), 78 deletions(-) create mode 100644 rewrite-maven/src/main/java/org/openrewrite/maven/ChangeVersionValue.java create mode 100644 rewrite-maven/src/test/java/org/openrewrite/maven/ChangeVersionValueTest.java 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 4da3c55ccf4..bd1dedafcb6 100755 --- a/rewrite-maven/src/main/java/org/openrewrite/maven/ChangeDependencyGroupIdAndArtifactId.java +++ b/rewrite-maven/src/main/java/org/openrewrite/maven/ChangeDependencyGroupIdAndArtifactId.java @@ -213,21 +213,7 @@ public Xml visitTag(Xml.Tag tag, ExecutionContext ctx) { if (!configuredToOverrideManageVersion && newDependencyManaged || (oldDependencyManaged && configuredToChangeManagedDependency)) { t = (Xml.Tag) new RemoveContentVisitor<>(versionTag.get(), false).visit(t, ctx); } else { - // Otherwise, change the version to the new value. - if (Objects.requireNonNull(currentVersion).contains("$")) { - // If the version is a property value, change version in property section and rename the property if changePropertyNames is true. - String propertyVariable = currentVersion.substring(2, currentVersion.length() - 1); - String newPropertyVariable = propertyVariable; - if (Boolean.TRUE.equals(changePropertyVersionNames)){ - newPropertyVariable = newArtifactId + ".version"; - doAfterVisit(new RenamePropertyKey(propertyVariable, newPropertyVariable).getVisitor()); - t = changeChildTagValue(t, "version", "${"+newPropertyVariable+"}", ctx); - } - doAfterVisit(new ChangePropertyValue(newPropertyVariable, resolvedNewVersion, false, false).getVisitor()); - } else { - //If the version is no property value, change it right here. - t = changeChildTagValue(t, "version", resolvedNewVersion, ctx); - } + doAfterVisit(new ChangeVersionValue(groupId, artifactId, newVersion, versionPattern, ChangeVersionValue.Changes.DEPENDENCY.name()).getVisitor()); } } else if (!(newDependencyManaged && configuredToChangeManagedDependency)) { 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 6b54dceb2e6..c0f8a854cdb 100644 --- a/rewrite-maven/src/main/java/org/openrewrite/maven/ChangeManagedDependencyGroupIdAndArtifactId.java +++ b/rewrite-maven/src/main/java/org/openrewrite/maven/ChangeManagedDependencyGroupIdAndArtifactId.java @@ -21,9 +21,7 @@ import org.openrewrite.*; import org.openrewrite.internal.lang.Nullable; import org.openrewrite.maven.table.MavenMetadataFailures; -import org.openrewrite.maven.tree.MavenMetadata; import org.openrewrite.semver.Semver; -import org.openrewrite.semver.VersionComparator; import org.openrewrite.xml.ChangeTagValueVisitor; import org.openrewrite.xml.tree.Xml; @@ -137,10 +135,6 @@ public String getDescription() { @Override public TreeVisitor getVisitor() { return new MavenIsoVisitor() { - @Nullable - final VersionComparator versionComparator = newVersion != null ? Semver.validate(newVersion, versionPattern).getValue() : null; - @Nullable - private Collection availableVersions; @Override public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) { @@ -158,31 +152,10 @@ public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) { doAfterVisit(new ChangeTagValueVisitor<>(artifactIdTag.get(), newArtifactId)); changed = true; } - if (newVersion != null) { - try { - Optional versionTag = t.getChild("version"); - - if (versionTag.isPresent()) { - String version = versionTag.get().getValue().orElse(null); - String resolvedNewVersion = resolveSemverVersion(ctx, newGroupId, newArtifactId, version); - if (Objects.requireNonNull(version).contains("$")) { - String propertyVariable = version.substring(2, version.length() - 1); - String newPropertyVariable = propertyVariable; - if (Boolean.TRUE.equals(changePropertyVersionNames)){ - newPropertyVariable = newArtifactId + ".version"; - doAfterVisit(new RenamePropertyKey(propertyVariable, newPropertyVariable).getVisitor()); - t = changeChildTagValue(t, "version", "${"+newPropertyVariable+"}", ctx); - } - doAfterVisit(new ChangePropertyValue(newPropertyVariable, resolvedNewVersion, false, false).getVisitor()); - } else { - t = changeChildTagValue(t, "version", resolvedNewVersion, ctx); - } - - } - changed = true; - } catch(MavenDownloadingException e) { - return e.warn(t); - } + Optional versionTag = t.getChild("version"); + if (versionTag.isPresent() && newVersion != null && !newVersion.equals(versionTag.get().getValue().orElse(null))) { + doAfterVisit(new ChangeVersionValue(newGroupId, newArtifactId, newVersion, versionPattern, ChangeVersionValue.Changes.MANAGED_DEPENDENCY.name()).getVisitor()); + changed = true; } if (changed) { maybeUpdateModel(); @@ -191,38 +164,6 @@ public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) { } return t; } - - private Xml.Tag changeChildTagValue(Xml.Tag tag, String childTagName, String newValue, ExecutionContext ctx) { - Optional childTag = tag.getChild(childTagName); - if (childTag.isPresent() && !newValue.equals(childTag.get().getValue().orElse(null))) { - tag = (Xml.Tag) new ChangeTagValueVisitor<>(childTag.get(), newValue).visitNonNull(tag, ctx); - } - return tag; - } - - @SuppressWarnings("ConstantConditions") - private String resolveSemverVersion(ExecutionContext ctx, String groupId, String artifactId, @Nullable String currentVersion) throws MavenDownloadingException { - if (currentVersion.contains("$")){ - String propertyVariable = currentVersion.substring(2, currentVersion.length() - 1); - currentVersion = getResolutionResult().getPom().getProperties().get(propertyVariable); - } - - 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(finalCurrentVersion, v)) { - availableVersions.add(v); - } - } - - } - return availableVersions.isEmpty() ? newVersion : Collections.max(availableVersions, versionComparator); - } }; } } diff --git a/rewrite-maven/src/main/java/org/openrewrite/maven/ChangeVersionValue.java b/rewrite-maven/src/main/java/org/openrewrite/maven/ChangeVersionValue.java new file mode 100644 index 00000000000..e3ee6b2a0a5 --- /dev/null +++ b/rewrite-maven/src/main/java/org/openrewrite/maven/ChangeVersionValue.java @@ -0,0 +1,267 @@ +/* + * Copyright 2022 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; + +import com.fasterxml.jackson.annotation.JsonCreator; +import lombok.EqualsAndHashCode; +import lombok.Value; +import org.openrewrite.*; +import org.openrewrite.internal.lang.Nullable; +import org.openrewrite.maven.table.MavenMetadataFailures; +import org.openrewrite.maven.tree.MavenMetadata; +import org.openrewrite.semver.Semver; +import org.openrewrite.semver.VersionComparator; +import org.openrewrite.xml.ChangeTagValueVisitor; +import org.openrewrite.xml.tree.Xml; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.*; + +import static org.openrewrite.Validated.test; +import static org.openrewrite.internal.StringUtils.*; + +@Value +@EqualsAndHashCode(callSuper = false) +public class ChangeVersionValue extends Recipe { + private static final Logger log = LoggerFactory.getLogger(ChangeVersionValue.class); + @EqualsAndHashCode.Exclude + MavenMetadataFailures metadataFailures = new MavenMetadataFailures(this); + + // there are several implicitly defined version properties that we should never attempt to update + private static final Set implicitlyDefinedVersionProperties = new HashSet<>(Arrays.asList( + "${version}", "${project.version}", "${pom.version}", "${project.parent.version}" + )); + + public enum Changes { + DEPENDENCY ("DEPENDENCY"), + MANAGED_DEPENDENCY ("MANAGED_DEPENDENCY"), + PLUGIN_DEPENDENCY ("PLUGIN_DEPENDENCY"); + + private final String name; + + Changes(String name) { + this.name = name; + } + + public String toString() { + return this.name; + } + } + + + @Option(displayName = "New groupId", + description = "The new groupId to use.", + example = "corp.internal.openrewrite.recipe") + String groupId; + + @Option(displayName = "New artifactId", + description = "The new artifactId to use.", + example = "rewrite-testing-frameworks") + String artifactId; + + @Option(displayName = "New version", + description = "The new version to use.", + example = "2.0.0", + required = false) + @Nullable + String newVersion; + + @Option(displayName = "Version pattern", + description = "Allows version selection to be extended beyond the original Node Semver semantics. So for example," + + "Setting 'version' to \"25-29\" can be paired with a metadata pattern of \"-jre\" to select Guava 29.0-jre", + example = "-jre", + required = false) + @Nullable + String versionPattern; + + @Option(displayName = "Change property version names", + description = "Allows property version names to be changed to best practice naming convention", + example = "-jre", + required = false) + @Nullable + Boolean changePropertyVersionNames; + + @Option(displayName = "Change property version names", + description = "Allows property version names to be changed to best practice naming convention", + example = "-jre", + required = false) + @Nullable + Changes versionChangePlace; + + public ChangeVersionValue(String groupId, String artifactId, @Nullable String newVersion, @Nullable String versionChangePlace) { + this(groupId, artifactId, newVersion, null, false, versionChangePlace); + } + + public ChangeVersionValue(String groupId, String artifactId, @Nullable String newVersion, @Nullable String versionPattern, @Nullable String versionChangePlace) { + this(groupId, artifactId, newVersion, versionPattern, false, versionChangePlace); + } + + @JsonCreator + public ChangeVersionValue(String groupId, String artifactId, @Nullable String newVersion, @Nullable String versionPattern, @Nullable Boolean changePropertyVersionNames, @Nullable String versionChangePlace) { + this.groupId = groupId; + this.artifactId = artifactId; + this.newVersion = newVersion; + this.versionPattern = versionPattern; + this.changePropertyVersionNames = changePropertyVersionNames; + this.versionChangePlace = versionChangePlace != null ? Changes.valueOf(versionChangePlace) : null; + } + + @Override + public Validated validate() { + Validated validated = super.validate(); + if (newVersion != null) { + validated = validated.and(Semver.validate(newVersion, versionPattern)); + } + validated = + validated.and(test( + "coordinates", + "newGroupId OR newArtifactId must be different from before", + this, + r -> { + boolean sameGroupId = isBlank(r.groupId); + boolean sameArtifactId = isBlank(r.artifactId); + return !(sameGroupId && sameArtifactId); + } + )); + return validated; + } + + @Override + public String getDisplayName() { + return "Change the version or the referenced property version of dependencies or plugins"; + } + + @Override + public String getDescription() { + return "Change the version or the referenced property version of dependencies or plugins."; + } + + @Override + public TreeVisitor getVisitor() { + return new MavenIsoVisitor() { + @Nullable + final VersionComparator versionComparator = newVersion != null ? Semver.validate(newVersion, versionPattern).getValue() : null; + @Nullable + private Collection availableVersions; + + @Override + public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) { + + Xml.Tag t = super.visitTag(tag, ctx); + + String artifact = artifactId == null ? t.getChildValue("artifactId").orElse(null) : artifactId; + + if (matchesGlob(tag.getChildValue("groupId").orElse(null), groupId) && (artifactId == null || matchesGlob(tag.getChildValue("artifactId").orElse(null), artifactId))) { + if (versionChangePlace == null){ + return changeVersion(t, ctx, groupId, artifact, newVersion, changePropertyVersionNames); + } + switch (Objects.requireNonNull(versionChangePlace)) { + case DEPENDENCY: + if (isDependencyTag() || PROFILE_DEPENDENCY_MATCHER.matches(getCursor())){ + return changeVersion(t, ctx, groupId, artifact, newVersion, changePropertyVersionNames); + } + break; + case MANAGED_DEPENDENCY: + if (isManagedDependencyTag() || PROFILE_MANAGED_DEPENDENCY_MATCHER.matches(getCursor())){ + return changeVersion(t, ctx, groupId, artifact, newVersion, changePropertyVersionNames); + } + break; + case PLUGIN_DEPENDENCY: + if (isPluginDependencyTag(groupId, artifact) || PROFILE_PLUGIN_DEPENDENCY_MATCHER.matches(getCursor())){ + return changeVersion(t, ctx, groupId, artifact, newVersion, changePropertyVersionNames); + } + break; + } + } + return t; + } + + private Xml.Tag changeChildTagValue(Xml.Tag tag, String childTagName, String newValue, ExecutionContext ctx) { + Optional childTag = tag.getChild(childTagName); + if (childTag.isPresent() && !newValue.equals(childTag.get().getValue().orElse(null))) { + tag = (Xml.Tag) new ChangeTagValueVisitor<>(childTag.get(), newValue).visitNonNull(tag, ctx); + } + return tag; + } + + @SuppressWarnings("ConstantConditions") + private String resolveSemverVersion(ExecutionContext ctx, String groupId, String artifactId, @Nullable String currentVersion) throws MavenDownloadingException { + if (currentVersion.contains("$")) { + String propertyVariable = currentVersion.substring(2, currentVersion.length() - 1); + currentVersion = getResolutionResult().getPom().getProperties().get(propertyVariable); + } + + 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(finalCurrentVersion, v)) { + availableVersions.add(v); + } + } + + } + return availableVersions.isEmpty() ? newVersion : Collections.max(availableVersions, versionComparator); + } + + public Xml.Tag changeVersion(Xml.Tag t, ExecutionContext ctx, String groupId, String artifactId, String newVersion, Boolean changePropertyVersionNames) { + + boolean changed = false; + if (newVersion != null) { + try { + Optional versionTag = t.getChild("version"); + if (versionTag.isPresent()) { + String version = versionTag.get().getValue().orElse(null); + String resolvedNewVersion = resolveSemverVersion(ctx, groupId, artifactId == null ? t.getChildValue("artifactId").orElse(null) : artifactId, version); + if (Objects.requireNonNull(version).contains("$")) { + String propertyVariable = version.substring(2, version.length() - 1); + String newPropertyVariable = propertyVariable; + if (!matchesGlob(getResolutionResult().getPom().getProperties().get(propertyVariable), resolvedNewVersion)) { + if (Boolean.TRUE.equals(changePropertyVersionNames)) { + newPropertyVariable = artifactId + ".version"; + doAfterVisit(new RenamePropertyKey(propertyVariable, newPropertyVariable).getVisitor()); + t = changeChildTagValue(t, "version", "${" + newPropertyVariable + "}", ctx); + } + doAfterVisit(new ChangePropertyValue(newPropertyVariable, resolvedNewVersion, false, false).getVisitor()); + changed = true; + } + } else { + if (!matchesGlob(t.getChildValue("version").orElse(null), resolvedNewVersion)){ + t = changeChildTagValue(t, "version", resolvedNewVersion, ctx); + changed = true; + } + } + + } + + } catch (MavenDownloadingException e) { + return e.warn(t); + } + } + if (changed) { + maybeUpdateModel(); + } + + return t; + } + }; + } +} diff --git a/rewrite-maven/src/test/java/org/openrewrite/maven/ChangeVersionValueTest.java b/rewrite-maven/src/test/java/org/openrewrite/maven/ChangeVersionValueTest.java new file mode 100644 index 00000000000..c2a32e7e482 --- /dev/null +++ b/rewrite-maven/src/test/java/org/openrewrite/maven/ChangeVersionValueTest.java @@ -0,0 +1,530 @@ +package org.openrewrite.maven; + +import org.junit.jupiter.api.Test; +import org.openrewrite.DocumentExample; +import org.openrewrite.test.RewriteTest; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.openrewrite.maven.Assertions.pomXml; + +class ChangeVersionValueTest implements RewriteTest { + + @DocumentExample + @Test + void changeVersionValue() { + rewriteRun( + spec -> spec.recipe(new ChangeVersionValue( + "jakarta.activation", + "jakarta.activation-api", + "2.1.0", + ChangeVersionValue.Changes.MANAGED_DEPENDENCY.name() + )), + pomXml( + """ + + 4.0.0 + com.mycompany.app + my-app + 1 + + + + jakarta.activation + jakarta.activation-api + 1.2.0 + + + + + """, + """ + + 4.0.0 + com.mycompany.app + my-app + 1 + + + + jakarta.activation + jakarta.activation-api + 2.1.0 + + + + + """ + ) + ); + } + + @Test + void changeVersionValueWithDynamicVersion() { + rewriteRun( + spec -> spec.recipe(new ChangeVersionValue( + "jakarta.activation", + "jakarta.activation-api", + "2.1.x", + ChangeVersionValue.Changes.MANAGED_DEPENDENCY.name() + )), + pomXml( + """ + + 4.0.0 + com.mycompany.app + my-app + 1 + + + + jakarta.activation + jakarta.activation-api + 1.2.0 + + + + + """, + spec -> spec.after(pom -> { + assertThat(pom).containsPattern("2.1.(\\d+)"); + return pom; + }) + ) + ); + } + + @Test + void latestPatchVersionValue() { + rewriteRun( + spec -> spec.recipe(new ChangeVersionValue( + "jakarta.activation", + "jakarta.activation-api", + "latest.patch", + null, + ChangeVersionValue.Changes.MANAGED_DEPENDENCY.name() + )), + pomXml( + """ + + 4.0.0 + com.mycompany.app + my-app + 1 + + + + jakarta.activation + jakarta.activation-api + 1.2.0 + + + + + """, + """ + + 4.0.0 + com.mycompany.app + my-app + 1 + + + + jakarta.activation + jakarta.activation-api + 1.2.2 + + + + + """ + ) + ); + } + + @Test + void changePropertyVersionValue() { + rewriteRun( + spec -> spec.recipe(new ChangeVersionValue( + "jakarta.activation", + "jakarta.activation-api", + "2.1.0", + ChangeVersionValue.Changes.MANAGED_DEPENDENCY.name() + )), + pomXml( + """ + + 4.0.0 + com.mycompany.app + my-app + 1 + + 1.2.0 + + + + + jakarta.activation + jakarta.activation-api + ${javax.activation.version} + + + + + """, + """ + + 4.0.0 + com.mycompany.app + my-app + 1 + + 2.1.0 + + + + + jakarta.activation + jakarta.activation-api + ${javax.activation.version} + + + + + """ + ) + ); + } + + @Test + void changeProfileManagedDependencyVersionValue() { + rewriteRun( + spec -> spec.recipe(new ChangeVersionValue( + "jakarta.activation", + "jakarta.activation-api", + "2.1.0", + ChangeVersionValue.Changes.MANAGED_DEPENDENCY.name() + )), + pomXml( + """ + + 4.0.0 + com.mycompany.app + my-app + 1 + + + + + + jakarta.activation + jakarta.activation-api + 1.2.0 + + + + + + + """, + """ + + 4.0.0 + com.mycompany.app + my-app + 1 + + + + + + jakarta.activation + jakarta.activation-api + 2.1.0 + + + + + + + """ + ) + ); + } + + @Test + void changePropertyVersionProfileManagedDependency() { + rewriteRun( + spec -> spec.recipe(new ChangeVersionValue( + "jakarta.activation", + "jakarta.activation-api", + "2.1.0", + ChangeVersionValue.Changes.MANAGED_DEPENDENCY.name() + )), + pomXml( + """ + + 4.0.0 + com.mycompany.app + my-app + 1 + + 1.2.0 + + + + test + + + + jakarta.activation + jakarta.activation-api + ${javax.activation.version} + + + + + + + """, + """ + + 4.0.0 + com.mycompany.app + my-app + 1 + + 2.1.0 + + + + test + + + + jakarta.activation + jakarta.activation-api + ${javax.activation.version} + + + + + + + """ + ) + ); + } + + @Test + void changeEveryVersionOfEveryDependencyWithChangePropertyVersionName() { + rewriteRun( + spec -> spec.recipe(new ChangeVersionValue( + "jakarta.activation", + "jakarta.activation-api", + "2.1.0", + null, + true, + null + )), + pomXml( + """ + + 4.0.0 + com.mycompany.app + my-app + 1 + + 1.2.0 + + + + test + + + jakarta.activation + jakarta.activation-api + 1.2.0 + + + + + + jakarta.activation + jakarta.activation-api + ${javax.activation.version} + + + + + + + + + jakarta.activation + jakarta.activation-api + 1.2.0 + + + + + + + + + """, + """ + + 4.0.0 + com.mycompany.app + my-app + 1 + + 2.1.0 + + + + test + + + jakarta.activation + jakarta.activation-api + 2.1.0 + + + + + + jakarta.activation + jakarta.activation-api + ${jakarta.activation-api.version} + + + + + + + + + jakarta.activation + jakarta.activation-api + 2.1.0 + + + + + + + + + """ + ) + ); + } + + @Test + void changeOnlyTheVersionOfProfileManagedDependencyWithChangePropertyVersionName() { + rewriteRun( + spec -> spec.recipe(new ChangeVersionValue( + "jakarta.activation", + "jakarta.activation-api", + "2.1.0", + null, + true, + "MANAGED_DEPENDENCY" + )), + pomXml( + """ + + 4.0.0 + com.mycompany.app + my-app + 1 + + 1.2.0 + + + + test + + + jakarta.activation + jakarta.activation-api + 1.2.0 + + + + + + jakarta.activation + jakarta.activation-api + ${javax.activation.version} + + + + + + + + + jakarta.activation + jakarta.activation-api + 1.2.0 + + + + + + + + + """, + """ + + 4.0.0 + com.mycompany.app + my-app + 1 + + 2.1.0 + + + + test + + + jakarta.activation + jakarta.activation-api + 1.2.0 + + + + + + jakarta.activation + jakarta.activation-api + ${jakarta.activation-api.version} + + + + + + + + + jakarta.activation + jakarta.activation-api + 1.2.0 + + + + + + + + + """ + ) + ); + } +} \ No newline at end of file From ec3e81f63ed9273cb400029700d580f2db2a3e9f Mon Sep 17 00:00:00 2001 From: marcel-gepardec Date: Tue, 30 Jul 2024 17:11:40 +0200 Subject: [PATCH 5/6] added the option newPropertyVersionName to ChangeDependencyVersionValue to rename the old property name. --- .../ChangeDependencyGroupIdAndArtifactId.java | 14 +- ...java => ChangeDependencyVersionValue.java} | 113 +++-- ...ManagedDependencyGroupIdAndArtifactId.java | 13 +- ...ngeDependencyGroupIdAndArtifactIdTest.java | 8 +- ... => ChangeDependencyVersionValueTest.java} | 421 +++++++++++++++++- ...gedDependencyGroupIdAndArtifactIdTest.java | 8 +- 6 files changed, 494 insertions(+), 83 deletions(-) rename rewrite-maven/src/main/java/org/openrewrite/maven/{ChangeVersionValue.java => ChangeDependencyVersionValue.java} (62%) rename rewrite-maven/src/test/java/org/openrewrite/maven/{ChangeVersionValueTest.java => ChangeDependencyVersionValueTest.java} (53%) 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 bd1dedafcb6..ed2faa43130 100755 --- a/rewrite-maven/src/main/java/org/openrewrite/maven/ChangeDependencyGroupIdAndArtifactId.java +++ b/rewrite-maven/src/main/java/org/openrewrite/maven/ChangeDependencyGroupIdAndArtifactId.java @@ -94,11 +94,11 @@ public class ChangeDependencyGroupIdAndArtifactId extends Recipe { @Nullable Boolean changeManagedDependency; - @Option(displayName = "Update dependency management", - description = "Also update the dependency management section. The default for this flag is `true`.", + @Option(displayName = "Change property version name", + description = "Allows property version name to be changed to best practice naming convention. The default for this flag is `false`.", required = false) @Nullable - Boolean changePropertyVersionNames; + Boolean changePropertyVersionName; public ChangeDependencyGroupIdAndArtifactId(String oldGroupId, String oldArtifactId, @Nullable String newGroupId, @Nullable String newArtifactId, @Nullable String newVersion, @Nullable String versionPattern) { this(oldGroupId, oldArtifactId, newGroupId, newArtifactId, newVersion, versionPattern, false, true, false); @@ -109,7 +109,7 @@ public ChangeDependencyGroupIdAndArtifactId(String oldGroupId, String oldArtifac } @JsonCreator - public ChangeDependencyGroupIdAndArtifactId(String oldGroupId, String oldArtifactId, @Nullable String newGroupId, @Nullable String newArtifactId, @Nullable String newVersion, @Nullable String versionPattern, @Nullable Boolean overrideManagedVersion, @Nullable Boolean changeManagedDependency, @Nullable Boolean changePropertyVersionNames) { + public ChangeDependencyGroupIdAndArtifactId(String oldGroupId, String oldArtifactId, @Nullable String newGroupId, @Nullable String newArtifactId, @Nullable String newVersion, @Nullable String versionPattern, @Nullable Boolean overrideManagedVersion, @Nullable Boolean changeManagedDependency, @Nullable Boolean changePropertyVersionName) { this.oldGroupId = oldGroupId; this.oldArtifactId = oldArtifactId; this.newGroupId = newGroupId; @@ -118,7 +118,7 @@ public ChangeDependencyGroupIdAndArtifactId(String oldGroupId, String oldArtifac this.versionPattern = versionPattern; this.overrideManagedVersion = overrideManagedVersion; this.changeManagedDependency = changeManagedDependency; - this.changePropertyVersionNames = changePropertyVersionNames; + this.changePropertyVersionName = changePropertyVersionName != null && changePropertyVersionName; // False by default } @Override @@ -173,7 +173,7 @@ public Xml visitDocument(Xml.Document document, ExecutionContext ctx) { oldGroupId, oldArtifactId, Optional.ofNullable(newGroupId).orElse(oldGroupId), Optional.ofNullable(newArtifactId).orElse(oldArtifactId), - newVersion, versionPattern, changePropertyVersionNames).getVisitor()); + newVersion, versionPattern, changePropertyVersionName).getVisitor()); } return super.visitDocument(document, ctx); } @@ -213,7 +213,7 @@ public Xml visitTag(Xml.Tag tag, ExecutionContext ctx) { if (!configuredToOverrideManageVersion && newDependencyManaged || (oldDependencyManaged && configuredToChangeManagedDependency)) { t = (Xml.Tag) new RemoveContentVisitor<>(versionTag.get(), false).visit(t, ctx); } else { - doAfterVisit(new ChangeVersionValue(groupId, artifactId, newVersion, versionPattern, ChangeVersionValue.Changes.DEPENDENCY.name()).getVisitor()); + doAfterVisit(new ChangeDependencyVersionValue(groupId, artifactId, newVersion, versionPattern, changePropertyVersionName, ChangeDependencyVersionValue.VersionLocation.DEPENDENCY.name()).getVisitor()); } } else if (!(newDependencyManaged && configuredToChangeManagedDependency)) { diff --git a/rewrite-maven/src/main/java/org/openrewrite/maven/ChangeVersionValue.java b/rewrite-maven/src/main/java/org/openrewrite/maven/ChangeDependencyVersionValue.java similarity index 62% rename from rewrite-maven/src/main/java/org/openrewrite/maven/ChangeVersionValue.java rename to rewrite-maven/src/main/java/org/openrewrite/maven/ChangeDependencyVersionValue.java index e3ee6b2a0a5..ac9419254b4 100644 --- a/rewrite-maven/src/main/java/org/openrewrite/maven/ChangeVersionValue.java +++ b/rewrite-maven/src/main/java/org/openrewrite/maven/ChangeDependencyVersionValue.java @@ -36,8 +36,8 @@ @Value @EqualsAndHashCode(callSuper = false) -public class ChangeVersionValue extends Recipe { - private static final Logger log = LoggerFactory.getLogger(ChangeVersionValue.class); +public class ChangeDependencyVersionValue extends Recipe { + private static final Logger log = LoggerFactory.getLogger(ChangeDependencyVersionValue.class); @EqualsAndHashCode.Exclude MavenMetadataFailures metadataFailures = new MavenMetadataFailures(this); @@ -46,14 +46,14 @@ public class ChangeVersionValue extends Recipe { "${version}", "${project.version}", "${pom.version}", "${project.parent.version}" )); - public enum Changes { + public enum VersionLocation { DEPENDENCY ("DEPENDENCY"), MANAGED_DEPENDENCY ("MANAGED_DEPENDENCY"), PLUGIN_DEPENDENCY ("PLUGIN_DEPENDENCY"); private final String name; - Changes(String name) { + VersionLocation(String name) { this.name = name; } @@ -88,36 +88,45 @@ public String toString() { @Nullable String versionPattern; - @Option(displayName = "Change property version names", - description = "Allows property version names to be changed to best practice naming convention", - example = "-jre", + @Option(displayName = "Change property version name", + description = "Allows property version name to be changed to best practice naming convention or what ever is in the newPropertyVersionName option. The default for this flag is `false`.", required = false) @Nullable - Boolean changePropertyVersionNames; + Boolean changePropertyVersionName; - @Option(displayName = "Change property version names", - description = "Allows property version names to be changed to best practice naming convention", - example = "-jre", + @Option(displayName = "New property version name", + description = "The new property version name used as property variable name. If it is set it will be changed. No need to set the changePropertyVersionName to true.", + example = "example.property.version", + required = false) + @Nullable + String newPropertyVersionName; + + @Option(displayName = "Declare the location where the dependency should be changed.", + description = "Changes dependency version right where you want it. The default is set to change it everywhere." + + "But you can also specifically target `DEPENDENCY`, `MANAGED_DEPENDENCY` or `PLUGIN_DEPENDENCY`." + + "It also doesn't matter if they are in a profile or not.", + example = "DEPENDENCY", required = false) @Nullable - Changes versionChangePlace; + ChangeDependencyVersionValue.VersionLocation versionLocation; - public ChangeVersionValue(String groupId, String artifactId, @Nullable String newVersion, @Nullable String versionChangePlace) { - this(groupId, artifactId, newVersion, null, false, versionChangePlace); + public ChangeDependencyVersionValue(String groupId, String artifactId, @Nullable String newVersion, @Nullable String versionLocation) { + this(groupId, artifactId, newVersion, null, false, null, versionLocation); } - public ChangeVersionValue(String groupId, String artifactId, @Nullable String newVersion, @Nullable String versionPattern, @Nullable String versionChangePlace) { - this(groupId, artifactId, newVersion, versionPattern, false, versionChangePlace); + public ChangeDependencyVersionValue(String groupId, String artifactId, @Nullable String newVersion, @Nullable String versionPattern, @Nullable Boolean changePropertyVersionName, @Nullable String versionLocation) { + this(groupId, artifactId, newVersion, versionPattern, changePropertyVersionName, null, versionLocation); } @JsonCreator - public ChangeVersionValue(String groupId, String artifactId, @Nullable String newVersion, @Nullable String versionPattern, @Nullable Boolean changePropertyVersionNames, @Nullable String versionChangePlace) { + public ChangeDependencyVersionValue(String groupId, String artifactId, @Nullable String newVersion, @Nullable String versionPattern, @Nullable Boolean changePropertyVersionName, @Nullable String newPropertyVersionName, @Nullable String versionLocation) { this.groupId = groupId; this.artifactId = artifactId; this.newVersion = newVersion; this.versionPattern = versionPattern; - this.changePropertyVersionNames = changePropertyVersionNames; - this.versionChangePlace = versionChangePlace != null ? Changes.valueOf(versionChangePlace) : null; + this.changePropertyVersionName = changePropertyVersionName != null && changePropertyVersionName; // False by default + this.newPropertyVersionName = newPropertyVersionName; + this.versionLocation = versionLocation != null ? VersionLocation.valueOf(versionLocation) : null; } @Override @@ -142,12 +151,13 @@ public Validated validate() { @Override public String getDisplayName() { - return "Change the version or the referenced property version of dependencies or plugins"; + return "Change Maven dependency version"; } @Override public String getDescription() { - return "Change the version or the referenced property version of dependencies or plugins."; + return "Change a Maven dependency version. Declare `groupId` and `artifactId` of the dependency in which the version should be changed. " + + "By adding `versionLocation`, a set of dependencies can be targeted such as managed Dependencies with `MANAGED_DEPENDENCY`."; } @Override @@ -163,30 +173,37 @@ public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) { Xml.Tag t = super.visitTag(tag, ctx); - String artifact = artifactId == null ? t.getChildValue("artifactId").orElse(null) : artifactId; + if (artifactId != null){ + if (matchesGlob(tag.getChildValue("groupId").orElse(null), groupId) && (artifactId == null || matchesGlob(tag.getChildValue("artifactId").orElse(null), artifactId))) { + String artifact = artifactId == null ? t.getChildValue("artifactId").orElse(null) : artifactId; - if (matchesGlob(tag.getChildValue("groupId").orElse(null), groupId) && (artifactId == null || matchesGlob(tag.getChildValue("artifactId").orElse(null), artifactId))) { - if (versionChangePlace == null){ - return changeVersion(t, ctx, groupId, artifact, newVersion, changePropertyVersionNames); - } - switch (Objects.requireNonNull(versionChangePlace)) { - case DEPENDENCY: - if (isDependencyTag() || PROFILE_DEPENDENCY_MATCHER.matches(getCursor())){ - return changeVersion(t, ctx, groupId, artifact, newVersion, changePropertyVersionNames); - } - break; - case MANAGED_DEPENDENCY: - if (isManagedDependencyTag() || PROFILE_MANAGED_DEPENDENCY_MATCHER.matches(getCursor())){ - return changeVersion(t, ctx, groupId, artifact, newVersion, changePropertyVersionNames); + if (newVersion != null){ + if (versionLocation == null){ + return changeVersion(t, ctx); } - break; - case PLUGIN_DEPENDENCY: - if (isPluginDependencyTag(groupId, artifact) || PROFILE_PLUGIN_DEPENDENCY_MATCHER.matches(getCursor())){ - return changeVersion(t, ctx, groupId, artifact, newVersion, changePropertyVersionNames); + switch (Objects.requireNonNull(versionLocation)) { + case DEPENDENCY: + if (isDependencyTag() || PROFILE_DEPENDENCY_MATCHER.matches(getCursor())){ + return changeVersion(t, ctx); + } + break; + case MANAGED_DEPENDENCY: + if (isManagedDependencyTag() || PROFILE_MANAGED_DEPENDENCY_MATCHER.matches(getCursor())){ + return changeVersion(t, ctx); + } + break; + case PLUGIN_DEPENDENCY: + if (isPluginDependencyTag(groupId, artifact) || PROFILE_PLUGIN_DEPENDENCY_MATCHER.matches(getCursor())){ + return changeVersion(t, ctx); + } + break; } - break; + } + } } + + return t; } @@ -222,7 +239,7 @@ private String resolveSemverVersion(ExecutionContext ctx, String groupId, String return availableVersions.isEmpty() ? newVersion : Collections.max(availableVersions, versionComparator); } - public Xml.Tag changeVersion(Xml.Tag t, ExecutionContext ctx, String groupId, String artifactId, String newVersion, Boolean changePropertyVersionNames) { + public Xml.Tag changeVersion(Xml.Tag t, ExecutionContext ctx) { boolean changed = false; if (newVersion != null) { @@ -235,14 +252,18 @@ public Xml.Tag changeVersion(Xml.Tag t, ExecutionContext ctx, String groupId, St String propertyVariable = version.substring(2, version.length() - 1); String newPropertyVariable = propertyVariable; if (!matchesGlob(getResolutionResult().getPom().getProperties().get(propertyVariable), resolvedNewVersion)) { - if (Boolean.TRUE.equals(changePropertyVersionNames)) { - newPropertyVariable = artifactId + ".version"; - doAfterVisit(new RenamePropertyKey(propertyVariable, newPropertyVariable).getVisitor()); - t = changeChildTagValue(t, "version", "${" + newPropertyVariable + "}", ctx); - } doAfterVisit(new ChangePropertyValue(newPropertyVariable, resolvedNewVersion, false, false).getVisitor()); changed = true; } + newPropertyVariable = artifactId + ".version"; + if (newPropertyVersionName != null) { + newPropertyVariable = newPropertyVersionName; + } + if (!getResolutionResult().getPom().getProperties().containsKey(newPropertyVariable) && (Boolean.TRUE.equals(changePropertyVersionName) || newPropertyVersionName != null)) { + doAfterVisit(new RenamePropertyKey(propertyVariable, newPropertyVariable).getVisitor()); + t = changeChildTagValue(t, "version", "${" + newPropertyVariable + "}", ctx); + changed = true; + } } else { if (!matchesGlob(t.getChildValue("version").orElse(null), resolvedNewVersion)){ t = changeChildTagValue(t, "version", resolvedNewVersion, ctx); 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 c0f8a854cdb..790e35e42fe 100644 --- a/rewrite-maven/src/main/java/org/openrewrite/maven/ChangeManagedDependencyGroupIdAndArtifactId.java +++ b/rewrite-maven/src/main/java/org/openrewrite/maven/ChangeManagedDependencyGroupIdAndArtifactId.java @@ -76,12 +76,11 @@ public class ChangeManagedDependencyGroupIdAndArtifactId extends Recipe { @Nullable String versionPattern; - @Option(displayName = "Change property version names", - description = "Allows property version names to be changed to best practice naming convention", - example = "-jre", + @Option(displayName = "Change property version name", + description = "Allows property version name to be changed to best practice naming convention. The default for this flag is `false`.", required = false) @Nullable - Boolean changePropertyVersionNames; + Boolean changePropertyVersionName; public ChangeManagedDependencyGroupIdAndArtifactId(String oldGroupId, String oldArtifactId, String newGroupId, String newArtifactId, @Nullable String newVersion) { this(oldGroupId, oldArtifactId, newGroupId, newArtifactId, newVersion, null, false); @@ -92,14 +91,14 @@ public ChangeManagedDependencyGroupIdAndArtifactId(String oldGroupId, String old } @JsonCreator - public ChangeManagedDependencyGroupIdAndArtifactId(String oldGroupId, String oldArtifactId, String newGroupId, String newArtifactId, @Nullable String newVersion, @Nullable String versionPattern, @Nullable Boolean changePropertyVersionNames) { + public ChangeManagedDependencyGroupIdAndArtifactId(String oldGroupId, String oldArtifactId, String newGroupId, String newArtifactId, @Nullable String newVersion, @Nullable String versionPattern, @Nullable Boolean changePropertyVersionName) { this.oldGroupId = oldGroupId; this.oldArtifactId = oldArtifactId; this.newGroupId = newGroupId; this.newArtifactId = newArtifactId; this.newVersion = newVersion; this.versionPattern = versionPattern; - this.changePropertyVersionNames = changePropertyVersionNames; + this.changePropertyVersionName = changePropertyVersionName != null && changePropertyVersionName; // False by default } @Override @@ -154,7 +153,7 @@ public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) { } Optional versionTag = t.getChild("version"); if (versionTag.isPresent() && newVersion != null && !newVersion.equals(versionTag.get().getValue().orElse(null))) { - doAfterVisit(new ChangeVersionValue(newGroupId, newArtifactId, newVersion, versionPattern, ChangeVersionValue.Changes.MANAGED_DEPENDENCY.name()).getVisitor()); + doAfterVisit(new ChangeDependencyVersionValue(newGroupId, newArtifactId, newVersion, versionPattern, changePropertyVersionName, ChangeDependencyVersionValue.VersionLocation.MANAGED_DEPENDENCY.name()).getVisitor()); changed = true; } if (changed) { 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 4980351bc11..6cf15b5a4ab 100644 --- a/rewrite-maven/src/test/java/org/openrewrite/maven/ChangeDependencyGroupIdAndArtifactIdTest.java +++ b/rewrite-maven/src/test/java/org/openrewrite/maven/ChangeDependencyGroupIdAndArtifactIdTest.java @@ -1522,7 +1522,7 @@ void changePropertyVersionProfileDependency() { } @Test - void changePropertyVersionProfileDependencyWithChangePropertyNamesFalse() { + void changePropertyVersionProfileDependencyWithChangePropertyNameTrue() { rewriteRun( spec -> spec.recipe(new ChangeDependencyGroupIdAndArtifactId( "javax.activation", @@ -1533,7 +1533,7 @@ void changePropertyVersionProfileDependencyWithChangePropertyNamesFalse() { null, false, true, - false + true )), pomXml( """ @@ -1566,7 +1566,7 @@ void changePropertyVersionProfileDependencyWithChangePropertyNamesFalse() { my-app 1 - 2.1.0 + 2.1.0 @@ -1575,7 +1575,7 @@ void changePropertyVersionProfileDependencyWithChangePropertyNamesFalse() { jakarta.activation jakarta.activation-api - ${javax.activation.version} + ${jakarta.activation-api.version} diff --git a/rewrite-maven/src/test/java/org/openrewrite/maven/ChangeVersionValueTest.java b/rewrite-maven/src/test/java/org/openrewrite/maven/ChangeDependencyVersionValueTest.java similarity index 53% rename from rewrite-maven/src/test/java/org/openrewrite/maven/ChangeVersionValueTest.java rename to rewrite-maven/src/test/java/org/openrewrite/maven/ChangeDependencyVersionValueTest.java index c2a32e7e482..6604b9097e3 100644 --- a/rewrite-maven/src/test/java/org/openrewrite/maven/ChangeVersionValueTest.java +++ b/rewrite-maven/src/test/java/org/openrewrite/maven/ChangeDependencyVersionValueTest.java @@ -7,17 +7,17 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.openrewrite.maven.Assertions.pomXml; -class ChangeVersionValueTest implements RewriteTest { +class ChangeDependencyVersionValueTest implements RewriteTest { @DocumentExample @Test void changeVersionValue() { rewriteRun( - spec -> spec.recipe(new ChangeVersionValue( + spec -> spec.recipe(new ChangeDependencyVersionValue( "jakarta.activation", "jakarta.activation-api", "2.1.0", - ChangeVersionValue.Changes.MANAGED_DEPENDENCY.name() + ChangeDependencyVersionValue.VersionLocation.MANAGED_DEPENDENCY.name() )), pomXml( """ @@ -61,11 +61,11 @@ void changeVersionValue() { @Test void changeVersionValueWithDynamicVersion() { rewriteRun( - spec -> spec.recipe(new ChangeVersionValue( + spec -> spec.recipe(new ChangeDependencyVersionValue( "jakarta.activation", "jakarta.activation-api", "2.1.x", - ChangeVersionValue.Changes.MANAGED_DEPENDENCY.name() + ChangeDependencyVersionValue.VersionLocation.MANAGED_DEPENDENCY.name() )), pomXml( """ @@ -96,12 +96,13 @@ void changeVersionValueWithDynamicVersion() { @Test void latestPatchVersionValue() { rewriteRun( - spec -> spec.recipe(new ChangeVersionValue( + spec -> spec.recipe(new ChangeDependencyVersionValue( "jakarta.activation", "jakarta.activation-api", "latest.patch", null, - ChangeVersionValue.Changes.MANAGED_DEPENDENCY.name() + false, + ChangeDependencyVersionValue.VersionLocation.MANAGED_DEPENDENCY.name() )), pomXml( """ @@ -145,11 +146,11 @@ void latestPatchVersionValue() { @Test void changePropertyVersionValue() { rewriteRun( - spec -> spec.recipe(new ChangeVersionValue( + spec -> spec.recipe(new ChangeDependencyVersionValue( "jakarta.activation", "jakarta.activation-api", "2.1.0", - ChangeVersionValue.Changes.MANAGED_DEPENDENCY.name() + ChangeDependencyVersionValue.VersionLocation.MANAGED_DEPENDENCY.name() )), pomXml( """ @@ -199,11 +200,11 @@ void changePropertyVersionValue() { @Test void changeProfileManagedDependencyVersionValue() { rewriteRun( - spec -> spec.recipe(new ChangeVersionValue( + spec -> spec.recipe(new ChangeDependencyVersionValue( "jakarta.activation", "jakarta.activation-api", "2.1.0", - ChangeVersionValue.Changes.MANAGED_DEPENDENCY.name() + ChangeDependencyVersionValue.VersionLocation.MANAGED_DEPENDENCY.name() )), pomXml( """ @@ -255,11 +256,11 @@ void changeProfileManagedDependencyVersionValue() { @Test void changePropertyVersionProfileManagedDependency() { rewriteRun( - spec -> spec.recipe(new ChangeVersionValue( + spec -> spec.recipe(new ChangeDependencyVersionValue( "jakarta.activation", "jakarta.activation-api", "2.1.0", - ChangeVersionValue.Changes.MANAGED_DEPENDENCY.name() + ChangeDependencyVersionValue.VersionLocation.MANAGED_DEPENDENCY.name() )), pomXml( """ @@ -319,12 +320,13 @@ void changePropertyVersionProfileManagedDependency() { @Test void changeEveryVersionOfEveryDependencyWithChangePropertyVersionName() { rewriteRun( - spec -> spec.recipe(new ChangeVersionValue( + spec -> spec.recipe(new ChangeDependencyVersionValue( "jakarta.activation", "jakarta.activation-api", "2.1.0", null, true, + null, null )), pomXml( @@ -425,12 +427,13 @@ void changeEveryVersionOfEveryDependencyWithChangePropertyVersionName() { @Test void changeOnlyTheVersionOfProfileManagedDependencyWithChangePropertyVersionName() { rewriteRun( - spec -> spec.recipe(new ChangeVersionValue( + spec -> spec.recipe(new ChangeDependencyVersionValue( "jakarta.activation", "jakarta.activation-api", "2.1.0", null, true, + null, "MANAGED_DEPENDENCY" )), pomXml( @@ -527,4 +530,392 @@ void changeOnlyTheVersionOfProfileManagedDependencyWithChangePropertyVersionName ) ); } + + @Test + void changeOnlyTheVersionOfDependencyWithChangePropertyVersionName() { + rewriteRun( + spec -> spec.recipe(new ChangeDependencyVersionValue( + "jakarta.activation", + "jakarta.activation-api", + "2.1.0", + null, + true, + null, + "DEPENDENCY" + )), + pomXml( + """ + + 4.0.0 + com.mycompany.app + my-app + 1 + + 1.2.0 + + + + test + + + jakarta.activation + jakarta.activation-api + ${javax.activation.version} + + + + + + jakarta.activation + jakarta.activation-api + 1.2.0 + + + + + + + + + jakarta.activation + jakarta.activation-api + 1.2.0 + + + + + + + + + """, + """ + + 4.0.0 + com.mycompany.app + my-app + 1 + + 2.1.0 + + + + test + + + jakarta.activation + jakarta.activation-api + ${jakarta.activation-api.version} + + + + + + jakarta.activation + jakarta.activation-api + 1.2.0 + + + + + + + + + jakarta.activation + jakarta.activation-api + 1.2.0 + + + + + + + + + """ + ) + ); + } + + @Test + void changeOnlyTheVersionOfPluginDependencyWithChangePropertyVersionName() { + rewriteRun( + spec -> spec.recipe(new ChangeDependencyVersionValue( + "jakarta.activation", + "jakarta.activation-api", + "2.1.0", + null, + true, + null, + "PLUGIN_DEPENDENCY" + )), + pomXml( + """ + + 4.0.0 + com.mycompany.app + my-app + 1 + + 1.2.0 + + + + test + + + jakarta.activation + jakarta.activation-api + 1.2.0 + + + + + + jakarta.activation + jakarta.activation-api + 1.2.0 + + + + + + + + + jakarta.activation + jakarta.activation-api + ${javax.activation.version} + + + + + + + + + """, + """ + + 4.0.0 + com.mycompany.app + my-app + 1 + + 2.1.0 + + + + test + + + jakarta.activation + jakarta.activation-api + 1.2.0 + + + + + + jakarta.activation + jakarta.activation-api + 1.2.0 + + + + + + + + + jakarta.activation + jakarta.activation-api + ${jakarta.activation-api.version} + + + + + + + + + """ + ) + ); + } + + @Test + void changeOnlyTheVersionOfProfileManagedDependencyWithNewPropertyVersionName() { + rewriteRun( + spec -> spec.recipe(new ChangeDependencyVersionValue( + "jakarta.activation", + "jakarta.activation-api", + "2.1.0", + null, + false, + "test", + "MANAGED_DEPENDENCY" + )), + pomXml( + """ + + 4.0.0 + com.mycompany.app + my-app + 1 + + 1.2.0 + + + + test + + + jakarta.activation + jakarta.activation-api + 1.2.0 + + + + + + jakarta.activation + jakarta.activation-api + ${javax.activation.version} + + + + + + + + + jakarta.activation + jakarta.activation-api + 1.2.0 + + + + + + + + + """, + """ + + 4.0.0 + com.mycompany.app + my-app + 1 + + 2.1.0 + + + + test + + + jakarta.activation + jakarta.activation-api + 1.2.0 + + + + + + jakarta.activation + jakarta.activation-api + ${test} + + + + + + + + + jakarta.activation + jakarta.activation-api + 1.2.0 + + + + + + + + + """ + ) + ); + } + + @Test + void alreadyLatestVersionButWithPropertyChange() { + rewriteRun( + spec -> spec.recipe(new ChangeDependencyVersionValue( + "jakarta.activation", + "jakarta.activation-api", + "2.1.0", + null, + true, + "test", + null + )), + pomXml( + """ + + 4.0.0 + com.mycompany.app + my-app + 1 + + 2.1.0 + + + + test + + + + jakarta.activation + jakarta.activation-api + ${jakarta.activation-api.version} + + + + + + + """, + """ + + 4.0.0 + com.mycompany.app + my-app + 1 + + 2.1.0 + + + + test + + + + jakarta.activation + jakarta.activation-api + ${test} + + + + + + + """ + ) + ); + } } \ No newline at end of file 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 05a5e49ea19..e799e6544c4 100644 --- a/rewrite-maven/src/test/java/org/openrewrite/maven/ChangeManagedDependencyGroupIdAndArtifactIdTest.java +++ b/rewrite-maven/src/test/java/org/openrewrite/maven/ChangeManagedDependencyGroupIdAndArtifactIdTest.java @@ -355,7 +355,7 @@ void changePropertyVersionProfileManagedDependency() { } @Test - void changePropertyVersionProfileManagedDependencyWithChangePropertyVersionName() { + void changePropertyVersionProfileManagedDependencyWithChangePropertyVersionNameTrue() { rewriteRun( spec -> spec.recipe(new ChangeManagedDependencyGroupIdAndArtifactId( "javax.activation", @@ -364,7 +364,7 @@ void changePropertyVersionProfileManagedDependencyWithChangePropertyVersionName( "jakarta.activation-api", "2.1.0", null, - false + true )), pomXml( """ @@ -399,7 +399,7 @@ void changePropertyVersionProfileManagedDependencyWithChangePropertyVersionName( my-app 1 - 2.1.0 + 2.1.0 @@ -409,7 +409,7 @@ void changePropertyVersionProfileManagedDependencyWithChangePropertyVersionName( jakarta.activation jakarta.activation-api - ${javax.activation.version} + ${jakarta.activation-api.version} From 26469cfe25dcb0e2788db1826be9852ff022e0de Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Wed, 31 Jul 2024 22:26:08 +0200 Subject: [PATCH 6/6] Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- .../maven/ChangeDependencyVersionValue.java | 2 ++ .../maven/ChangeDependencyVersionValueTest.java | 17 ++++++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/rewrite-maven/src/main/java/org/openrewrite/maven/ChangeDependencyVersionValue.java b/rewrite-maven/src/main/java/org/openrewrite/maven/ChangeDependencyVersionValue.java index ac9419254b4..5f56855184d 100644 --- a/rewrite-maven/src/main/java/org/openrewrite/maven/ChangeDependencyVersionValue.java +++ b/rewrite-maven/src/main/java/org/openrewrite/maven/ChangeDependencyVersionValue.java @@ -38,6 +38,7 @@ @EqualsAndHashCode(callSuper = false) public class ChangeDependencyVersionValue extends Recipe { private static final Logger log = LoggerFactory.getLogger(ChangeDependencyVersionValue.class); + @EqualsAndHashCode.Exclude MavenMetadataFailures metadataFailures = new MavenMetadataFailures(this); @@ -57,6 +58,7 @@ public enum VersionLocation { this.name = name; } + @Override public String toString() { return this.name; } diff --git a/rewrite-maven/src/test/java/org/openrewrite/maven/ChangeDependencyVersionValueTest.java b/rewrite-maven/src/test/java/org/openrewrite/maven/ChangeDependencyVersionValueTest.java index 6604b9097e3..080261b8f55 100644 --- a/rewrite-maven/src/test/java/org/openrewrite/maven/ChangeDependencyVersionValueTest.java +++ b/rewrite-maven/src/test/java/org/openrewrite/maven/ChangeDependencyVersionValueTest.java @@ -1,3 +1,18 @@ +/* + * Copyright 2024 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; import org.junit.jupiter.api.Test; @@ -918,4 +933,4 @@ void alreadyLatestVersionButWithPropertyChange() { ) ); } -} \ No newline at end of file +}