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

Do not override version property placeholder in UpgradeDependencyVersion #4579

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -382,12 +382,17 @@ private String resolveVersion(String version) {

public @Nullable TreeVisitor<Xml, ExecutionContext> upgradeVersion(ExecutionContext ctx, Xml.Tag tag, @Nullable String requestedVersion, String groupId, String artifactId, String version2) throws MavenDownloadingException {
String newerVersion = findNewerVersion(groupId, artifactId, version2, ctx);
String requestedGroupId = getRequestedGroupIdForArtifact(artifactId);
if (newerVersion == null) {
return null;
} else if (requestedVersion != null && requestedVersion.startsWith("${")) {
//noinspection unchecked
return (TreeVisitor<Xml, ExecutionContext>) new ChangePropertyValue(requestedVersion.substring(2, requestedVersion.length() - 1), newerVersion, overrideManagedVersion, false)
.getVisitor();
} else if (requestedVersion != null && requestedGroupId != null && requestedGroupId.startsWith("${")) {
//noinspection unchecked
return (TreeVisitor<Xml, ExecutionContext>) new ChangePropertyValue(requestedGroupId.substring(2, requestedGroupId.length() - 1), newerVersion, overrideManagedVersion, false)
.getVisitor();
} else {
Xml.Tag childVersionTag = tag.getChild("version").orElse(null);
if (childVersionTag != null) {
Expand All @@ -402,6 +407,23 @@ private String resolveVersion(String version) {
return MavenDependency.findNewerVersion(groupId, artifactId, version, getResolutionResult(), metadataFailures,
versionComparator, ctx);
}

private @Nullable String getRequestedGroupIdForArtifact(String artifactId) {
for (ManagedDependency managedDependency : getResolutionResult().getPom().getRequested().getDependencyManagement()) {
if (managedDependency instanceof ManagedDependency.Imported) {
ManagedDependency.Imported imported = (ManagedDependency.Imported) managedDependency;
if (artifactId.equals(imported.getGav().getArtifactId())) {
return imported.getGav().getVersion();
}
} else if (managedDependency instanceof ManagedDependency.Defined) {
ManagedDependency.Defined defined = (ManagedDependency.Defined) managedDependency;
if (artifactId.equals(defined.getGav().getArtifactId())) {
return defined.getGav().getVersion();
}
}
}
return null;
}
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1889,4 +1889,81 @@ void exactVersionMissingInMavenMetadata() {
)
);
}

@Test
@Issue("https://github.com/openrewrite/rewrite-spring/issues/474")
void upgradePropertyAdditionalCheck() {
rewriteRun(
spec -> spec.recipe(new UpgradeDependencyVersion("org.springframework", "*", "5.2.x", "",
false, null)),
mavenProject("project",
//language=xml
pomXml(
"""
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.openrewrite.example</groupId>
<artifactId>example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>example</name>
<description>Demo project for Spring Boot</description>
<properties>
<spring.boot.version>2.2.13.RELEASE</spring.boot.version>
<spring.jms.version>5.2.22.RELEASE</spring.jms.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring.boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>${spring.jms.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
""",
"""
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.openrewrite.example</groupId>
<artifactId>example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>example</name>
<description>Demo project for Spring Boot</description>
<properties>
<spring.boot.version>2.2.13.RELEASE</spring.boot.version>
<spring.jms.version>5.2.25.RELEASE</spring.jms.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring.boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>${spring.jms.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
"""
)
)
);
}

}
Loading