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

ChangeParentPom's property-preservation now checks the resolved new p… #4143

Merged
merged 1 commit into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,9 @@ public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {

// Retain managed versions from the old parent that are not managed in the new parent
MavenPomDownloader mpd = new MavenPomDownloader(mrr.getProjectPoms(), ctx, mrr.getMavenSettings(), mrr.getActiveProfiles());
Pom newParentPom = mpd.download(new GroupArtifactVersion(targetGroupId, targetArtifactId, targetVersion.get()), null, null, resolvedPom.getRepositories());
List<ResolvedManagedDependency> dependenciesWithoutExplicitVersions = getDependenciesUnmanagedByNewParent(mrr, ctx);
ResolvedPom newParent = mpd.download(new GroupArtifactVersion(targetGroupId, targetArtifactId, targetVersion.get()), null, resolvedPom, resolvedPom.getRepositories())
.resolve(emptyList(), mpd, ctx);
List<ResolvedManagedDependency> dependenciesWithoutExplicitVersions = getDependenciesUnmanagedByNewParent(mrr, newParent);
for (ResolvedManagedDependency dep : dependenciesWithoutExplicitVersions) {
changeParentTagVisitors.add(new AddManagedDependencyVisitor(
dep.getGav().getGroupId(), dep.getGav().getArtifactId(), dep.getGav().getVersion(),
Expand All @@ -202,7 +203,7 @@ public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {

// Retain properties from the old parent that are not present in the new parent
Map<String, String> propertiesInUse = getPropertiesInUse(getCursor().firstEnclosingOrThrow(Xml.Document.class), ctx);
Map<String, String> newParentProps = newParentPom.getProperties();
Map<String, String> newParentProps = newParent.getProperties();
for (Map.Entry<String, String> propInUse : propertiesInUse.entrySet()) {
if(!newParentProps.containsKey(propInUse.getKey())) {
changeParentTagVisitors.add(new UnconditionalAddProperty(propInUse.getKey(), propInUse.getValue()));
Expand Down Expand Up @@ -231,6 +232,11 @@ public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {
doAfterVisit(new RemoveRedundantDependencyVersions(null, null, false, null).getVisitor());
}
} catch (MavenDownloadingException e) {
for (Map.Entry<MavenRepository, String> repositoryResponse : e.getRepositoryResponses().entrySet()) {
MavenRepository repository = repositoryResponse.getKey();
metadataFailures.insertRow(ctx, new MavenMetadataFailures.Row(targetGroupId, targetArtifactId, newVersion,
repository.getUri(), repository.getSnapshots(), repository.getReleases(), repositoryResponse.getValue()));
}
return e.warn(tag);
}
}
Expand Down Expand Up @@ -304,7 +310,7 @@ public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext executionContext) {
return properties;
}

private List<ResolvedManagedDependency> getDependenciesUnmanagedByNewParent(MavenResolutionResult mrr, ExecutionContext ctx) {
private List<ResolvedManagedDependency> getDependenciesUnmanagedByNewParent(MavenResolutionResult mrr, ResolvedPom newParent) {
ResolvedPom resolvedPom = mrr.getPom();

// Dependencies managed by the current pom's own dependency management are irrelevant to parent upgrade
Expand Down Expand Up @@ -334,28 +340,13 @@ private List<ResolvedManagedDependency> getDependenciesUnmanagedByNewParent(Mave
}

// Remove from the list any that would still be managed under the new parent
String groupId = newGroupId == null ? oldGroupId : newGroupId;
String artifactId = newArtifactId == null ? oldArtifactId : newArtifactId;
try {
GroupArtifactVersion newParentGav = new GroupArtifactVersion(groupId, artifactId, newVersion);
MavenExecutionContextView mctx = MavenExecutionContextView.view(ctx);
MavenPomDownloader mpd = new MavenPomDownloader(mrr.getProjectPoms(), mctx, mctx.getSettings(), mctx.getActiveProfiles());
ResolvedPom newParent = mpd.download(newParentGav, null, resolvedPom, resolvedPom.getRepositories())
.resolve(emptyList(), mpd, ctx);
Set<GroupArtifact> newParentManagedGa = newParent.getDependencyManagement().stream()
.map(dep -> new GroupArtifact(dep.getGav().getGroupId(), dep.getGav().getArtifactId()))
.collect(Collectors.toSet());

depsWithoutExplicitVersion = depsWithoutExplicitVersion.stream()
.filter(it -> !newParentManagedGa.contains(new GroupArtifact(it.getGav().getGroupId(), it.getGav().getArtifactId())))
.collect(Collectors.toList());
} catch (MavenDownloadingException e) {
for (Map.Entry<MavenRepository, String> repositoryResponse : e.getRepositoryResponses().entrySet()) {
MavenRepository repository = repositoryResponse.getKey();
metadataFailures.insertRow(ctx, new MavenMetadataFailures.Row(groupId, artifactId, newVersion,
repository.getUri(), repository.getSnapshots(), repository.getReleases(), repositoryResponse.getValue()));
}
}
Set<GroupArtifact> newParentManagedGa = newParent.getDependencyManagement().stream()
.map(dep -> new GroupArtifact(dep.getGav().getGroupId(), dep.getGav().getArtifactId()))
.collect(Collectors.toSet());

depsWithoutExplicitVersion = depsWithoutExplicitVersion.stream()
.filter(it -> !newParentManagedGa.contains(new GroupArtifact(it.getGav().getGroupId(), it.getGav().getArtifactId())))
.collect(Collectors.toList());
return depsWithoutExplicitVersion;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1418,4 +1418,51 @@ void doesNotAddMavenDefaultProperties() {
)
);
}

@Test
void doesNotAddGrandparentProperties() {
rewriteRun(
spec -> spec.recipe(new ChangeParentPom("org.springframework.boot", null, "spring-boot-starter-parent", null, "2.7.18", null, null, null, null)),
pomXml(
"""
<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.sample</groupId>
<artifactId>sample</artifactId>
<version>1.0.0</version>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.17</version>
</parent>

<properties>
<my-cool-prop>${junit.version}</my-cool-prop>
</properties>
</project>
""",
"""
<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.sample</groupId>
<artifactId>sample</artifactId>
<version>1.0.0</version>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.18</version>
</parent>

<properties>
<my-cool-prop>${junit.version}</my-cool-prop>
</properties>
</project>
"""
)
);
}
}