Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UpgradeParentVersion: option to only upgrade external parents #4592

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.openrewrite.maven.tree.*;
import org.openrewrite.semver.Semver;
import org.openrewrite.semver.VersionComparator;
import org.openrewrite.xml.AddOrUpdateChild;
import org.openrewrite.xml.AddToTagVisitor;
import org.openrewrite.xml.ChangeTagValueVisitor;
import org.openrewrite.xml.TagNameComparator;
Expand All @@ -38,6 +39,7 @@

import static java.util.Collections.emptyList;
import static org.openrewrite.internal.StringUtils.matchesGlob;
import static org.openrewrite.maven.tree.Parent.DEFAULT_RELATIVE_PATH;

@Value
@EqualsAndHashCode(callSuper = false)
Expand Down Expand Up @@ -74,7 +76,8 @@ public class ChangeParentPom extends Recipe {
String newVersion;

@Option(displayName = "Old relative path",
description = "The relativePath of the maven parent pom to be changed away from.",
description = "The relativePath of the maven parent pom to be changed away from. " +
"Use an empty String to match `<relativePath />`, use `../pom.xml` to match the default value.",
example = "../../pom.xml",
required = false)
@Nullable
Expand Down Expand Up @@ -158,7 +161,7 @@ public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {

if (matchesGlob(resolvedPom.getValue(tag.getChildValue("groupId").orElse(null)), oldGroupId) &&
matchesGlob(resolvedPom.getValue(tag.getChildValue("artifactId").orElse(null)), oldArtifactId) &&
(oldRelativePath == null || matchesGlob(resolvedPom.getValue(tag.getChildValue("relativePath").orElse(null)), oldRelativePath))) {
(oldRelativePath == null || matchesGlob(determineRelativePath(tag, resolvedPom), oldRelativePath))) {
String oldVersion = resolvedPom.getValue(tag.getChildValue("version").orElse(null));
assert oldVersion != null;
String currentGroupId = tag.getChildValue("groupId").orElse(oldGroupId);
Expand Down Expand Up @@ -211,9 +214,15 @@ public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {
}

// Update or add relativePath
if (oldRelativePath != null && !oldRelativePath.equals(targetRelativePath)) {
changeParentTagVisitors.add(new ChangeTagValueVisitor<>(t.getChild("relativePath").get(), targetRelativePath));
} else if (mismatches(tag.getChild("relativePath").orElse(null), targetRelativePath)) {
Optional<Xml.Tag> existingRelativePath = t.getChild("relativePath");
if (oldRelativePath != null && !oldRelativePath.equals(targetRelativePath) && existingRelativePath.isPresent()) {
if (StringUtils.isBlank(targetRelativePath)) {
// ChangeTagValueVisitor would keep the closing tag
changeParentTagVisitors.add(new AddOrUpdateChild<>(t, Xml.Tag.build("<relativePath />")));
} else {
changeParentTagVisitors.add(new ChangeTagValueVisitor<>(existingRelativePath.get(), targetRelativePath));
}
} else if (mismatches(existingRelativePath.orElse(null), targetRelativePath)) {
final Xml.Tag relativePathTag;
if (StringUtils.isBlank(targetRelativePath)) {
relativePathTag = Xml.Tag.build("<relativePath />");
Expand Down Expand Up @@ -281,6 +290,14 @@ private Optional<String> findAcceptableVersion(String groupId, String artifactId
});
}

private static @Nullable String determineRelativePath(Xml.Tag tag, ResolvedPom resolvedPom) {
Optional<Xml.Tag> relativePath = tag.getChild("relativePath");
if (relativePath.isPresent()) {
return resolvedPom.getValue(relativePath.get().getValue().orElse(""));
}
return DEFAULT_RELATIVE_PATH;
}

private static final Pattern PROPERTY_PATTERN = Pattern.compile("\\$\\{([^}]+)}");

private static Map<String, String> getPropertiesInUse(Xml.Document pomXml, ExecutionContext ctx) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ public class UpgradeParentVersion extends Recipe {
@Nullable
String versionPattern;

@Option(displayName = "Only external",
description = "Only upgrade `<parent>` if external to the project, i.e. it has an empty `<relativePath>`. Defaults to `false`.",
required = false)
@Nullable
Boolean onlyExternal;

@Override
public String getDisplayName() {
return "Upgrade Maven parent project version";
Expand Down Expand Up @@ -82,7 +88,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
}

private ChangeParentPom changeParentPom() {
return new ChangeParentPom(groupId, null, artifactId, null, newVersion, null, null,
return new ChangeParentPom(groupId, null, artifactId, null, newVersion, Boolean.TRUE.equals(onlyExternal) ? "" : null, null,
versionPattern, false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
@Value
@With
public class Parent {
public static final String DEFAULT_RELATIVE_PATH = "../pom.xml";

GroupArtifactVersion gav;

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ void updateToSpringBoot30Snapshot() {
new AddRepository("boot-snapshots", "https://repo.spring.io/snapshot", null, null,
true, null, null,
null, null, null, null),
new UpgradeParentVersion("org.springframework.boot", "spring-boot-starter-parent", "3.0.0-SNAPSHOT", null)
new UpgradeParentVersion("org.springframework.boot", "spring-boot-starter-parent", "3.0.0-SNAPSHOT", null, null)
),
pomXml(
"""
Expand Down
Loading