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

Add version tag for managed dependency with property #4692

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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 @@ -299,12 +299,27 @@ private Xml.Tag upgradeDependency(ExecutionContext ctx, Xml.Tag t) throws MavenD
ResolvedManagedDependency dm = findManagedDependency(t);
// if a managed dependency is expressed as a property, change the property value
if (dm != null &&
dm.getRequested().getVersion() != null &&
dm.getRequested().getVersion().startsWith("${") &&
!implicitlyDefinedVersionProperties.contains(dm.getRequested().getVersion())) {
doAfterVisit(new ChangePropertyValue(dm.getRequested().getVersion().substring(2,
dm.getRequested().getVersion().length() - 1),
newerVersion, overrideManagedVersion, false).getVisitor());
dm.getRequested().getVersion() != null &&
dm.getRequested().getVersion().startsWith("${") &&
!implicitlyDefinedVersionProperties.contains(dm.getRequested().getVersion())) {
Parent parent = getResolutionResult().getPom().getRequested().getParent();
ManagedDependency requestedBom = dm.getRequestedBom();

// change property only if the dependency is managed by the parent
if (parent != null && requestedBom != null && requestedBom.getGroupId().equals(parent.getGroupId()) &&
requestedBom.getArtifactId().equals(parent.getArtifactId()) &&
Objects.equals(requestedBom.getVersion(), parent.getVersion())) {
doAfterVisit(new ChangePropertyValue(dm.getRequested().getVersion().substring(2,
dm.getRequested().getVersion().length() - 1),
newerVersion, overrideManagedVersion, false).getVisitor());
} else {
// add a new explicit version tag
Xml.Tag versionTag = Xml.Tag.build("<version>" + newerVersion + "</version>");

//noinspection ConstantConditions
t = (Xml.Tag) new AddToTagVisitor<>(t, versionTag, new MavenTagInsertionComparator(t.getChildren()))
.visitNonNull(t, 0, getCursor().getParent());
}
} else {
// if the version is not present and the override managed version is set,
// add a new explicit version tag
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,64 @@ void upgradeVersionDefinedViaPropertyInLocalParent() {
);
}

@Test
void upgradeVersionDefinedViaImplicitPropertyInDependencyManagementBom() {
rewriteRun(
spec -> spec.recipe(new UpgradeDependencyVersion("org.flywaydb", "flyway-core", "10.15.0", "", true, null)),
pomXml(
"""
<project>
<groupId>com.mycompany</groupId>
<artifactId>my-child</artifactId>
<version>1</version>
<dependencies>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>3.3.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
""",
"""
<project>
<groupId>com.mycompany</groupId>
<artifactId>my-child</artifactId>
<version>1</version>
<dependencies>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
<version>10.15.0</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>3.3.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
"""
)
);
}

@Issue("https://github.com/openrewrite/rewrite/issues/4193")
@Test
void upgradeVersionDefinedViaImplicitPropertyInRemoteParent() {
Expand Down