Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -19,9 +19,11 @@
import lombok.Value;
import org.jspecify.annotations.Nullable;
import org.openrewrite.*;
import org.openrewrite.internal.ListUtils;
import org.openrewrite.maven.search.FindPlugin;
import org.openrewrite.maven.table.MavenMetadataFailures;
import org.openrewrite.maven.tree.MavenMetadata;
import org.openrewrite.maven.tree.Plugin;
import org.openrewrite.maven.tree.ResolvedPom;
import org.openrewrite.semver.Semver;
import org.openrewrite.semver.VersionComparator;
Expand All @@ -31,6 +33,7 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.Objects;
import java.util.Optional;

import static java.util.Objects.requireNonNull;
Expand Down Expand Up @@ -118,22 +121,26 @@ public Xml visitTag(Xml.Tag tag, ExecutionContext ctx) {
Optional<Xml.Tag> versionTag = tag.getChild("version");
Optional<String> maybeVersionValue = versionTag.flatMap(Xml.Tag::getValue);
if (maybeVersionValue.isPresent() || Boolean.TRUE.equals(addVersionIfMissing)) {
final String versionLookup;
if (maybeVersionValue.isPresent()) {
String versionValue = maybeVersionValue.get();
versionLookup = versionValue.startsWith("${") ?
super.getResolutionResult().getPom().getValue(versionValue.trim()) :
versionValue;
} else {
versionLookup = "0.0.0";
}

try {
ResolvedPom resolvedPom = getResolutionResult().getPom();
String tagGroupId = resolvedPom.getValue(tag.getChildValue("groupId").orElse(groupId));
String tagArtifactId = resolvedPom.getValue(tag.getChildValue("artifactId").orElse(artifactId));
assert tagGroupId != null;
assert tagArtifactId != null;

final String versionLookup;
if (maybeVersionValue.isPresent()) {
String versionValue = maybeVersionValue.get();
versionLookup = versionValue.startsWith("${") ?
resolvedPom.getValue(versionValue.trim()) :
versionValue;
} else {
if (hasManagedPluginVersion(resolvedPom, tagGroupId, tagArtifactId)) {
return tag;
}
versionLookup = "0.0.0";
}

findNewerDependencyVersion(tagGroupId, tagArtifactId, versionLookup, ctx).ifPresent(newer ->
doAfterVisit(new ChangePluginVersionVisitor(tagGroupId, tagArtifactId, newer, Boolean.TRUE.equals(addVersionIfMissing)))
);
Expand All @@ -146,6 +153,18 @@ public Xml visitTag(Xml.Tag tag, ExecutionContext ctx) {
return super.visitTag(tag, ctx);
}

private boolean hasManagedPluginVersion(ResolvedPom resolvedPom, String groupId, String artifactId) {
for (Plugin p : ListUtils.concatAll(resolvedPom.getPluginManagement(),
resolvedPom.getRequested().getPluginManagement())) {
if (Objects.equals(p.getGroupId(), groupId) &&
Comment thread
timtebeek marked this conversation as resolved.
Outdated
Objects.equals(p.getArtifactId(), artifactId) &&
p.getVersion() != null) {
return true;
}
}
return false;
}

private Optional<String> findNewerDependencyVersion(String groupId, String artifactId,
String currentVersion, ExecutionContext ctx) throws MavenDownloadingException {
MavenMetadata mavenMetadata = metadataFailures.insertRows(ctx, () -> downloadPluginMetadata(groupId, artifactId, ctx));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -941,4 +941,86 @@ void defaultPluginGroupId() {
)
);
}

@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/992")
@Test
void shouldNotAddVersionWhenManagedByParent() {
rewriteRun(
spec -> spec.recipe(new UpgradePluginVersion(
"org.apache.maven.plugins",
"maven-compiler-plugin",
"3.11.0",
null,
null,
true
)),
pomXml(
"""
<project>
<groupId>org.openrewrite.example</groupId>
<artifactId>my-app-parent</artifactId>
<version>1</version>
<packaging>pom</packaging>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
""",
"""
<project>
<groupId>org.openrewrite.example</groupId>
<artifactId>my-app-parent</artifactId>
<version>1</version>
<packaging>pom</packaging>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
"""
),
mavenProject("child",
pomXml(
"""
<project>
<parent>
<groupId>org.openrewrite.example</groupId>
<artifactId>my-app-parent</artifactId>
<version>1</version>
</parent>
<artifactId>my-app-child</artifactId>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
"""
// No changes expected — version is managed by parent
)
)
);
}
}