Skip to content

Commit bd2b80d

Browse files
authored
UpgradePluginVersion: skip adding version when managed by pluginManagement (#6828)
* `addVersionIfMissing` should not add version when managed by pluginManagement When `addVersionIfMissing` is true, `UpgradePluginVersion` now checks whether the plugin already has a version managed via `<pluginManagement>` (local or inherited from a parent POM) before adding an explicit `<version>` tag. This prevents unwanted version additions in child modules that inherit their plugin version from a parent. Fixes openrewrite/rewrite-migrate-java#992 * Use regular `.equals` as suggested
1 parent e804713 commit bd2b80d

3 files changed

Lines changed: 111 additions & 11 deletions

File tree

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

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919
import lombok.Value;
2020
import org.jspecify.annotations.Nullable;
2121
import org.openrewrite.*;
22+
import org.openrewrite.internal.ListUtils;
2223
import org.openrewrite.maven.search.FindPlugin;
2324
import org.openrewrite.maven.table.MavenMetadataFailures;
2425
import org.openrewrite.maven.tree.MavenMetadata;
26+
import org.openrewrite.maven.tree.Plugin;
2527
import org.openrewrite.maven.tree.ResolvedPom;
2628
import org.openrewrite.semver.Semver;
2729
import org.openrewrite.semver.VersionComparator;
@@ -31,6 +33,7 @@
3133

3234
import java.util.ArrayList;
3335
import java.util.Collection;
36+
import java.util.Objects;
3437
import java.util.Optional;
3538

3639
import static java.util.Objects.requireNonNull;
@@ -118,22 +121,26 @@ public Xml visitTag(Xml.Tag tag, ExecutionContext ctx) {
118121
Optional<Xml.Tag> versionTag = tag.getChild("version");
119122
Optional<String> maybeVersionValue = versionTag.flatMap(Xml.Tag::getValue);
120123
if (maybeVersionValue.isPresent() || Boolean.TRUE.equals(addVersionIfMissing)) {
121-
final String versionLookup;
122-
if (maybeVersionValue.isPresent()) {
123-
String versionValue = maybeVersionValue.get();
124-
versionLookup = versionValue.startsWith("${") ?
125-
super.getResolutionResult().getPom().getValue(versionValue.trim()) :
126-
versionValue;
127-
} else {
128-
versionLookup = "0.0.0";
129-
}
130-
131124
try {
132125
ResolvedPom resolvedPom = getResolutionResult().getPom();
133126
String tagGroupId = resolvedPom.getValue(tag.getChildValue("groupId").orElse(groupId));
134127
String tagArtifactId = resolvedPom.getValue(tag.getChildValue("artifactId").orElse(artifactId));
135128
assert tagGroupId != null;
136129
assert tagArtifactId != null;
130+
131+
final String versionLookup;
132+
if (maybeVersionValue.isPresent()) {
133+
String versionValue = maybeVersionValue.get();
134+
versionLookup = versionValue.startsWith("${") ?
135+
resolvedPom.getValue(versionValue.trim()) :
136+
versionValue;
137+
} else {
138+
if (hasManagedPluginVersion(resolvedPom, tagGroupId, tagArtifactId)) {
139+
return tag;
140+
}
141+
versionLookup = "0.0.0";
142+
}
143+
137144
findNewerDependencyVersion(tagGroupId, tagArtifactId, versionLookup, ctx).ifPresent(newer ->
138145
doAfterVisit(new ChangePluginVersionVisitor(tagGroupId, tagArtifactId, newer, Boolean.TRUE.equals(addVersionIfMissing)))
139146
);
@@ -146,6 +153,18 @@ public Xml visitTag(Xml.Tag tag, ExecutionContext ctx) {
146153
return super.visitTag(tag, ctx);
147154
}
148155

156+
private boolean hasManagedPluginVersion(ResolvedPom resolvedPom, String groupId, String artifactId) {
157+
for (Plugin p : ListUtils.concatAll(resolvedPom.getPluginManagement(),
158+
resolvedPom.getRequested().getPluginManagement())) {
159+
if (p.getGroupId().equals(groupId) &&
160+
p.getArtifactId().equals(artifactId) &&
161+
p.getVersion() != null) {
162+
return true;
163+
}
164+
}
165+
return false;
166+
}
167+
149168
private Optional<String> findNewerDependencyVersion(String groupId, String artifactId,
150169
String currentVersion, ExecutionContext ctx) throws MavenDownloadingException {
151170
MavenMetadata mavenMetadata = metadataFailures.insertRows(ctx, () -> downloadPluginMetadata(groupId, artifactId, ctx));

rewrite-maven/src/main/java/org/openrewrite/maven/tree/Plugin.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ public class Plugin {
5555
List<Dependency> dependencies;
5656
List<Execution> executions;
5757

58-
@NonNull
5958
public String getGroupId() {
6059
return groupId == null ? PLUGIN_DEFAULT_GROUPID : groupId;
6160
}

rewrite-maven/src/test/java/org/openrewrite/maven/UpgradePluginVersionTest.java

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -941,4 +941,86 @@ void defaultPluginGroupId() {
941941
)
942942
);
943943
}
944+
945+
@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/992")
946+
@Test
947+
void shouldNotAddVersionWhenManagedByParent() {
948+
rewriteRun(
949+
spec -> spec.recipe(new UpgradePluginVersion(
950+
"org.apache.maven.plugins",
951+
"maven-compiler-plugin",
952+
"3.11.0",
953+
null,
954+
null,
955+
true
956+
)),
957+
pomXml(
958+
"""
959+
<project>
960+
<groupId>org.openrewrite.example</groupId>
961+
<artifactId>my-app-parent</artifactId>
962+
<version>1</version>
963+
<packaging>pom</packaging>
964+
<build>
965+
<pluginManagement>
966+
<plugins>
967+
<plugin>
968+
<groupId>org.apache.maven.plugins</groupId>
969+
<artifactId>maven-compiler-plugin</artifactId>
970+
<version>3.8.1</version>
971+
</plugin>
972+
</plugins>
973+
</pluginManagement>
974+
</build>
975+
</project>
976+
""",
977+
"""
978+
<project>
979+
<groupId>org.openrewrite.example</groupId>
980+
<artifactId>my-app-parent</artifactId>
981+
<version>1</version>
982+
<packaging>pom</packaging>
983+
<build>
984+
<pluginManagement>
985+
<plugins>
986+
<plugin>
987+
<groupId>org.apache.maven.plugins</groupId>
988+
<artifactId>maven-compiler-plugin</artifactId>
989+
<version>3.11.0</version>
990+
</plugin>
991+
</plugins>
992+
</pluginManagement>
993+
</build>
994+
</project>
995+
"""
996+
),
997+
mavenProject("child",
998+
pomXml(
999+
"""
1000+
<project>
1001+
<parent>
1002+
<groupId>org.openrewrite.example</groupId>
1003+
<artifactId>my-app-parent</artifactId>
1004+
<version>1</version>
1005+
</parent>
1006+
<artifactId>my-app-child</artifactId>
1007+
<build>
1008+
<plugins>
1009+
<plugin>
1010+
<groupId>org.apache.maven.plugins</groupId>
1011+
<artifactId>maven-compiler-plugin</artifactId>
1012+
<configuration>
1013+
<source>1.8</source>
1014+
<target>1.8</target>
1015+
</configuration>
1016+
</plugin>
1017+
</plugins>
1018+
</build>
1019+
</project>
1020+
"""
1021+
// No changes expected — version is managed by parent
1022+
)
1023+
)
1024+
);
1025+
}
9441026
}

0 commit comments

Comments
 (0)