Skip to content

Commit ae563f8

Browse files
authored
Revert generalized maven recipe property handling (#5516)
* Revert "Generalize changing of properties in Maven recipes (#5503)" This reverts commit 0050d30. * Already introduce the new methods we will leverage soon * Avoid conflict with method from MavenVisitor * Avoid another conflict that IDE didn't warn about
1 parent 4420254 commit ae563f8

7 files changed

Lines changed: 95 additions & 77 deletions

rewrite-maven/src/main/java/org/openrewrite/maven/ChangeDependencyGroupIdAndArtifactId.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.openrewrite.semver.Semver;
2626
import org.openrewrite.semver.VersionComparator;
2727
import org.openrewrite.xml.AddToTagVisitor;
28+
import org.openrewrite.xml.ChangeTagValueVisitor;
2829
import org.openrewrite.xml.RemoveContentVisitor;
2930
import org.openrewrite.xml.tree.Xml;
3031

@@ -177,13 +178,13 @@ public Xml visitTag(Xml.Tag tag, ExecutionContext ctx) {
177178
if (isOldDependencyTag || isPluginDependencyTag(oldGroupId, oldArtifactId)) {
178179
String groupId = newGroupId;
179180
if (groupId != null) {
180-
t = changeChildTagValue(t, "groupId", groupId, ctx);
181+
t = changeChildTagValue2(t, "groupId", groupId, ctx);
181182
} else {
182183
groupId = t.getChildValue("groupId").orElseThrow(NoSuchElementException::new);
183184
}
184185
String artifactId = newArtifactId;
185186
if (artifactId != null) {
186-
t = changeChildTagValue(t, "artifactId", artifactId, ctx);
187+
t = changeChildTagValue2(t, "artifactId", artifactId, ctx);
187188
} else {
188189
artifactId = t.getChildValue("artifactId").orElseThrow(NoSuchElementException::new);
189190
}
@@ -207,7 +208,7 @@ public Xml visitTag(Xml.Tag tag, ExecutionContext ctx) {
207208
t = (Xml.Tag) new RemoveContentVisitor<>(versionTag.get(), false, true).visit(t, ctx);
208209
} else {
209210
// Otherwise, change the version to the new value.
210-
t = changeChildTagValue(t, "version", resolvedNewVersion, ctx);
211+
t = changeChildTagValue2(t, "version", resolvedNewVersion, ctx);
211212
}
212213
} else if (configuredToOverrideManageVersion || !newDependencyManaged) {
213214
//If the version is not present, add the version if we are explicitly overriding a managed version or if no managed version exists.
@@ -238,7 +239,17 @@ private boolean checkIfNewDependencyPresents(@Nullable String groupId, @Nullable
238239
.anyMatch(rd -> (version == null) || version.equals(rd.getVersion()));
239240
}
240241

242+
@Deprecated // Use changeChildTagValue from MavenVisitor instead: https://github.com/openrewrite/rewrite/pull/5516
243+
private Xml.Tag changeChildTagValue2(Xml.Tag tag, String childTagName, String newValue, ExecutionContext ctx) {
244+
Optional<Xml.Tag> childTag = tag.getChild(childTagName);
245+
if (childTag.isPresent() && !newValue.equals(childTag.get().getValue().orElse(null))) {
246+
tag = (Xml.Tag) new ChangeTagValueVisitor<>(childTag.get(), newValue).visitNonNull(tag, ctx);
247+
}
248+
return tag;
249+
}
250+
241251
private boolean isDependencyManaged(Scope scope, String groupId, String artifactId) {
252+
242253
MavenResolutionResult result = getResolutionResult();
243254
for (ResolvedManagedDependency managedDependency : result.getPom().getDependencyManagement()) {
244255
if (groupId.equals(managedDependency.getGroupId()) && artifactId.equals(managedDependency.getArtifactId())) {

rewrite-maven/src/main/java/org/openrewrite/maven/ChangeManagedDependencyGroupIdAndArtifactId.java

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.openrewrite.maven.tree.ResolvedPom;
2727
import org.openrewrite.semver.Semver;
2828
import org.openrewrite.semver.VersionComparator;
29+
import org.openrewrite.xml.ChangeTagValueVisitor;
2930
import org.openrewrite.xml.RemoveContentVisitor;
3031
import org.openrewrite.xml.tree.Xml;
3132

@@ -40,6 +41,11 @@ public class ChangeManagedDependencyGroupIdAndArtifactId extends Recipe {
4041
@EqualsAndHashCode.Exclude
4142
MavenMetadataFailures metadataFailures = new MavenMetadataFailures(this);
4243

44+
// there are several implicitly defined version properties that we should never attempt to update
45+
private static final Set<String> implicitlyDefinedVersionProperties = new HashSet<>(Arrays.asList(
46+
"${version}", "${project.version}", "${pom.version}", "${project.parent.version}"
47+
));
48+
4349
@Option(displayName = "Old groupId",
4450
description = "The old groupId to replace. The groupId is the first part of a managed dependency coordinate `com.google.guava:guava:VERSION`.",
4551
example = "org.openrewrite.recipe")
@@ -140,17 +146,24 @@ public Xml.Document visitDocument(Xml.Document document, ExecutionContext ctx) {
140146

141147
@Override
142148
public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {
149+
143150
Xml.Tag t = super.visitTag(tag, ctx);
144151
if (isManagedDependencyTag(oldGroupId, oldArtifactId)) {
145-
if (t.getChild("groupId").isPresent()) {
146-
t = changeChildTagValue(t, "groupId", newGroupId, ctx);
152+
Optional<Xml.Tag> groupIdTag = t.getChild("groupId");
153+
boolean changed = false;
154+
if (groupIdTag.isPresent() && !newGroupId.equals(groupIdTag.get().getValue().orElse(null))) {
155+
doAfterVisit(new ChangeTagValueVisitor<>(groupIdTag.get(), newGroupId));
156+
changed = true;
147157
}
148-
if (t.getChild("artifactId").isPresent()) {
149-
t = changeChildTagValue(t, "artifactId", newArtifactId, ctx);
158+
Optional<Xml.Tag> artifactIdTag = t.getChild("artifactId");
159+
if (artifactIdTag.isPresent() && !newArtifactId.equals(artifactIdTag.get().getValue().orElse(null))) {
160+
doAfterVisit(new ChangeTagValueVisitor<>(artifactIdTag.get(), newArtifactId));
161+
changed = true;
150162
}
151163
if (newVersion != null) {
152164
try {
153165
Optional<Xml.Tag> versionTag = t.getChild("version");
166+
154167
if (versionTag.isPresent()) {
155168
String resolvedArtifactId = newArtifactId;
156169
if (resolvedArtifactId.contains("${")) {
@@ -159,13 +172,14 @@ public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {
159172
resolvedArtifactId = ResolvedPom.placeholderHelper.replacePlaceholders(newArtifactId, properties::get);
160173
}
161174
String resolvedNewVersion = resolveSemverVersion(ctx, newGroupId, resolvedArtifactId, getResolutionResult().getPom().getValue(versionTag.get().getValue().orElse(null)));
162-
t = changeChildTagValue(t, "version", resolvedNewVersion, ctx);
175+
t = (Xml.Tag) new ChangeTagValueVisitor<>(versionTag.get(), resolvedNewVersion).visitNonNull(t, 0, getCursor().getParentOrThrow());
163176
}
177+
changed = true;
164178
} catch (MavenDownloadingException e) {
165179
return e.warn(t);
166180
}
167181
}
168-
if (t != tag) {
182+
if (changed) {
169183
maybeUpdateModel();
170184
doAfterVisit(new RemoveRedundantDependencyVersions(null, null, null, null).getVisitor());
171185
if (isNewDependencyPresent) {

rewrite-maven/src/main/java/org/openrewrite/maven/ChangePluginGroupIdAndArtifactId.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@
2323
import org.openrewrite.Recipe;
2424
import org.openrewrite.TreeVisitor;
2525
import org.openrewrite.maven.table.MavenMetadataFailures;
26+
import org.openrewrite.xml.ChangeTagValueVisitor;
2627
import org.openrewrite.xml.tree.Xml;
2728

29+
import java.util.Optional;
30+
2831
@Value
2932
@EqualsAndHashCode(callSuper = false)
3033
public class ChangePluginGroupIdAndArtifactId extends Recipe {
@@ -87,13 +90,13 @@ public Xml visitTag(Xml.Tag tag, ExecutionContext ctx) {
8790
Xml.Tag t = (Xml.Tag) super.visitTag(tag, ctx);
8891
if (isPluginTag(oldGroupId, oldArtifactId)) {
8992
if (newGroupId != null) {
90-
t = changeChildTagValue(t, "groupId", newGroupId, ctx);
93+
t = changeChildTagValue2(t, "groupId", newGroupId, ctx);
9194
}
9295
if (newArtifactId != null) {
93-
t = changeChildTagValue(t, "artifactId", newArtifactId, ctx);
96+
t = changeChildTagValue2(t, "artifactId", newArtifactId, ctx);
9497
}
9598
if (newVersion != null) {
96-
t = changeChildTagValue(t, "version", newVersion, ctx);
99+
t = changeChildTagValue2(t, "version", newVersion, ctx);
97100
}
98101
if (t != tag) {
99102
maybeUpdateModel();
@@ -102,6 +105,14 @@ public Xml visitTag(Xml.Tag tag, ExecutionContext ctx) {
102105
//noinspection ConstantConditions
103106
return t;
104107
}
108+
109+
private Xml.Tag changeChildTagValue2(Xml.Tag tag, String childTagName, String newValue, ExecutionContext ctx) {
110+
Optional<Xml.Tag> childTag = tag.getChild(childTagName);
111+
if (childTag.isPresent() && !newValue.equals(childTag.get().getValue().orElse(null))) {
112+
tag = (Xml.Tag) new ChangeTagValueVisitor<>(childTag.get(), newValue).visitNonNull(tag, ctx);
113+
}
114+
return tag;
115+
}
105116
};
106117
}
107118
}

rewrite-maven/src/main/java/org/openrewrite/maven/MavenVisitor.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,13 @@ public class MavenVisitor<P> extends XmlVisitor<P> {
5050

5151
// there are several implicitly defined version properties that we should never attempt to update
5252
private static final Set<String> IMPLICITLY_DEFINED_VERSION_PROPERTIES = new HashSet<>(Arrays.asList(
53-
"${version}", "${project.version}", "${pom.version}", "${project.parent.version}"
53+
"${version}",
54+
"${project.version}",
55+
"${pom.version}",
56+
"${project.parent.version}",
57+
"${revision}",
58+
"${sha1}",
59+
"${changelist}"
5460
));
5561

5662
private transient Xml.@Nullable Document document;

rewrite-maven/src/main/java/org/openrewrite/maven/UpgradeDependencyVersion.java

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ public class UpgradeDependencyVersion extends ScanningRecipe<UpgradeDependencyVe
5252
@EqualsAndHashCode.Exclude
5353
transient MavenMetadataFailures metadataFailures = new MavenMetadataFailures(this);
5454

55+
// there are several implicitly defined version properties that we should never attempt to update
56+
private static final Collection<String> implicitlyDefinedVersionProperties = Arrays.asList(
57+
"${version}", "${project.version}", "${pom.version}", "${project.parent.version}"
58+
);
59+
5560
@Option(displayName = "Group",
5661
description = "The first part of a dependency coordinate `com.google.guava:guava:VERSION`. This can be a glob expression.",
5762
example = "com.fasterxml.jackson*")
@@ -151,7 +156,8 @@ public Xml.Tag visitTag(final Xml.Tag tag, final ExecutionContext ctx) {
151156
Optional<Xml.Tag> version = tag.getChild("version");
152157
if (version.isPresent()) {
153158
String requestedVersion = d.getRequested().getVersion();
154-
if (isProperty(requestedVersion)) {
159+
if (requestedVersion != null && requestedVersion.startsWith("${") &&
160+
!implicitlyDefinedVersionProperties.contains(requestedVersion)) {
155161
String propertyName = requestedVersion.substring(2, requestedVersion.length() - 1);
156162
if (!getResolutionResult().getPom().getRequested().getProperties().containsKey(propertyName)) {
157163
storeParentPomProperty(getResolutionResult().getParent(), propertyName, newerVersion);
@@ -174,7 +180,8 @@ public Xml.Tag visitTag(final Xml.Tag tag, final ExecutionContext ctx) {
174180
* @param propertyName the name of the property to update, if found in any the parent pom source file
175181
* @param newerVersion the resolved newer version that any matching parent pom property should be updated to
176182
*/
177-
private void storeParentPomProperty(@Nullable MavenResolutionResult currentMavenResolutionResult, String propertyName, String newerVersion) {
183+
private void storeParentPomProperty(
184+
@Nullable MavenResolutionResult currentMavenResolutionResult, String propertyName, String newerVersion) {
178185
if (currentMavenResolutionResult == null) {
179186
return; // No parent contained the property; might then be in the same source file, or an import BOM
180187
}
@@ -263,8 +270,10 @@ public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {
263270
}
264271

265272
private void retainVersions() {
266-
RetainVersions.plan(this, retainVersions == null ? emptyList() : retainVersions)
267-
.forEach(it -> doAfterVisit(it.getVisitor()));
273+
for (Recipe retainVersionRecipe : RetainVersions.plan(this, retainVersions == null ?
274+
emptyList() : retainVersions)) {
275+
doAfterVisit(retainVersionRecipe.getVisitor());
276+
}
268277
}
269278
}
270279

@@ -275,13 +284,26 @@ private Xml.Tag upgradeDependency(ExecutionContext ctx, Xml.Tag t) throws MavenD
275284
// as a source file, attempt to find a new version.
276285
String newerVersion = findNewerVersion(d.getGroupId(), d.getArtifactId(), d.getVersion(), ctx);
277286
if (newerVersion != null) {
278-
if (t.getChild("version").isPresent()) {
279-
t = changeChildTagValue(t, "version", newerVersion, overrideManagedVersion, ctx);
287+
Optional<Xml.Tag> version = t.getChild("version");
288+
if (version.isPresent()) {
289+
String requestedVersion = d.getRequested().getVersion();
290+
if (requestedVersion != null && requestedVersion.startsWith("${") && !implicitlyDefinedVersionProperties.contains(requestedVersion)) {
291+
String propertyName = requestedVersion.substring(2, requestedVersion.length() - 1);
292+
if (getResolutionResult().getPom().getRequested().getProperties().containsKey(propertyName)) {
293+
doAfterVisit(new ChangePropertyValue(propertyName, newerVersion, overrideManagedVersion, false).getVisitor());
294+
}
295+
} else {
296+
t = (Xml.Tag) new ChangeTagValueVisitor<>(version.get(), newerVersion).visitNonNull(t, 0, getCursor().getParentOrThrow());
297+
}
280298
} else if (Boolean.TRUE.equals(overrideManagedVersion)) {
281299
ResolvedManagedDependency dm = findManagedDependency(t);
282300
// if a managed dependency is expressed as a property, change the property value
283301
// do this only when a requested bom is absent, otherwise changing property has no effect
284-
if (dm != null && isProperty(dm.getRequested().getVersion()) && dm.getRequestedBom() == null) {
302+
if (dm != null &&
303+
dm.getRequested().getVersion() != null &&
304+
dm.getRequested().getVersion().startsWith("${") &&
305+
!implicitlyDefinedVersionProperties.contains(dm.getRequested().getVersion()) &&
306+
dm.getRequestedBom() == null) {
285307
doAfterVisit(new ChangePropertyValue(dm.getRequested().getVersion().substring(2,
286308
dm.getRequested().getVersion().length() - 1),
287309
newerVersion, overrideManagedVersion, false).getVisitor());
@@ -337,14 +359,20 @@ private Xml.Tag upgradePluginDependency(ExecutionContext ctx, Xml.Tag t) throws
337359
if (groupId != null && artifactId != null && version != null) {
338360
String newerVersion = findNewerVersion(groupId, artifactId, resolveVersion(version), ctx);
339361
if (newerVersion != null) {
340-
t = changeChildTagValue(t, "version", newerVersion, overrideManagedVersion, ctx);
362+
if (version.startsWith("${") && !implicitlyDefinedVersionProperties.contains(version)) {
363+
doAfterVisit(new ChangePropertyValue(version.substring(2, version.length() - 1), newerVersion, overrideManagedVersion, false).getVisitor());
364+
} else {
365+
Optional<Xml.Tag> versionTag = t.getChild("version");
366+
assert versionTag.isPresent();
367+
t = (Xml.Tag) new ChangeTagValueVisitor<>(versionTag.get(), newerVersion).visitNonNull(t, 0, getCursor().getParentOrThrow());
368+
}
341369
}
342370
}
343371
return t;
344372
}
345373

346374
private String resolveVersion(String version) {
347-
if (isProperty(version)) {
375+
if (version.startsWith("${") && !implicitlyDefinedVersionProperties.contains(version)) {
348376
Map<String, String> properties = getResolutionResult().getPom().getProperties();
349377
String property = version.substring(2, version.length() - 1);
350378
return properties.getOrDefault(property, version);
@@ -356,7 +384,7 @@ private String resolveVersion(String version) {
356384
String newerVersion = findNewerVersion(groupId, artifactId, version2, ctx);
357385
if (newerVersion == null) {
358386
return null;
359-
} else if (isProperty(requestedVersion)) {
387+
} else if (requestedVersion != null && requestedVersion.startsWith("${")) {
360388
//noinspection unchecked
361389
return (TreeVisitor<Xml, ExecutionContext>) new ChangePropertyValue(requestedVersion.substring(2, requestedVersion.length() - 1), newerVersion, overrideManagedVersion, false)
362390
.getVisitor();

0 commit comments

Comments
 (0)