diff --git a/rewrite-maven/src/main/java/org/openrewrite/maven/UpgradeDependencyVersion.java b/rewrite-maven/src/main/java/org/openrewrite/maven/UpgradeDependencyVersion.java index 94e9523f0d5..000830b1239 100644 --- a/rewrite-maven/src/main/java/org/openrewrite/maven/UpgradeDependencyVersion.java +++ b/rewrite-maven/src/main/java/org/openrewrite/maven/UpgradeDependencyVersion.java @@ -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("" + newerVersion + ""); + + //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 diff --git a/rewrite-maven/src/test/java/org/openrewrite/maven/UpgradeDependencyVersionTest.java b/rewrite-maven/src/test/java/org/openrewrite/maven/UpgradeDependencyVersionTest.java index 63b2e70bc43..4f362f2cc9e 100644 --- a/rewrite-maven/src/test/java/org/openrewrite/maven/UpgradeDependencyVersionTest.java +++ b/rewrite-maven/src/test/java/org/openrewrite/maven/UpgradeDependencyVersionTest.java @@ -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( + """ + + com.mycompany + my-child + 1 + + + org.flywaydb + flyway-core + + + + + + org.springframework.boot + spring-boot-dependencies + 3.3.0 + pom + import + + + + + """, + """ + + com.mycompany + my-child + 1 + + + org.flywaydb + flyway-core + 10.15.0 + + + + + + org.springframework.boot + spring-boot-dependencies + 3.3.0 + pom + import + + + + + """ + ) + ); + } + @Issue("https://github.com/openrewrite/rewrite/issues/4193") @Test void upgradeVersionDefinedViaImplicitPropertyInRemoteParent() {