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

Resove dependencies bom with properties on ChangeParentPom #4735

Merged
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.
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 @@ -68,11 +68,11 @@ public String replacePlaceholders(String value, final Properties properties) {
return replacePlaceholders(value, properties::getProperty);
}

public String replacePlaceholders(String value, Function<String, String> placeholderResolver) {
public String replacePlaceholders(String value, Function<String, @Nullable String> placeholderResolver) {
return parseStringValue(value, placeholderResolver, null);
}

protected String parseStringValue(String value, Function<String, String> placeholderResolver,
protected String parseStringValue(String value, Function<String, @Nullable String> placeholderResolver,
@Nullable Set<String> visitedPlaceholders) {
int startIndex = value.indexOf(placeholderPrefix);
if (startIndex == -1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,19 @@ private List<ResolvedManagedDependency> getDependenciesUnmanagedByNewParent(Mave
.filter(dep -> dep.getVersion() == null)
// Dependencies explicitly managed by the current pom require no changes
.filter(dep -> locallyManaged.stream()
.noneMatch(localManagedDep -> localManagedDep.getGroupId().equals(dep.getGroupId()) && localManagedDep.getArtifactId().equals(dep.getArtifactId())))
.noneMatch(localManagedDep -> {
String resolvedGroupId = resolvedPom.getValue(localManagedDep.getGroupId());
String resolvedArtifactId = resolvedPom.getValue(localManagedDep.getArtifactId());
if (resolvedArtifactId.contains("${")) {
Map<String, String> properties = resolvedPom.getProperties();
resolvedArtifactId = ResolvedPom.placeholderHelper.replacePlaceholders(resolvedArtifactId, properties::get);
}
if (resolvedGroupId.contains("${")) {
Map<String, String> properties = resolvedPom.getProperties();
resolvedGroupId = ResolvedPom.placeholderHelper.replacePlaceholders(resolvedGroupId, properties::get);
}
return resolvedGroupId.equals(dep.getGroupId()) && resolvedArtifactId.equals(dep.getArtifactId());
}))
.map(dep -> new GroupArtifactVersion(dep.getGroupId(), dep.getArtifactId(), null))
.collect(Collectors.toCollection(LinkedHashSet::new));

Expand All @@ -355,7 +367,19 @@ private List<ResolvedManagedDependency> getDependenciesUnmanagedByNewParent(Mave
.filter(dep -> requestedWithoutExplicitVersion.contains(dep.getGav().withVersion(null)))
// Exclude dependencies managed by a bom imported by the current pom
.filter(dep -> dep.getBomGav() == null || locallyManaged.stream()
.noneMatch(localManagedDep -> localManagedDep.getGroupId().equals(dep.getBomGav().getGroupId()) && localManagedDep.getArtifactId().equals(dep.getBomGav().getArtifactId())))
.noneMatch(localManagedDep -> {
String resolvedGroupId = resolvedPom.getValue(localManagedDep.getGroupId());
String resolvedArtifactId = resolvedPom.getValue(localManagedDep.getArtifactId());
if (resolvedArtifactId.contains("${")) {
Map<String, String> properties = resolvedPom.getProperties();
resolvedArtifactId = ResolvedPom.placeholderHelper.replacePlaceholders(resolvedArtifactId, properties::get);
}
if (resolvedGroupId.contains("${")) {
Map<String, String> properties = resolvedPom.getProperties();
resolvedGroupId = ResolvedPom.placeholderHelper.replacePlaceholders(resolvedGroupId, properties::get);
}
return resolvedGroupId.equals(dep.getBomGav().getGroupId()) && resolvedArtifactId.equals(dep.getBomGav().getArtifactId());
}))
.collect(Collectors.toList());

if(depsWithoutExplicitVersion.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,103 @@ void changeParent() {
);
}

@Test
void changeParentShouldResolveDependenciesMangementWithMavenProperties() {
rewriteRun(
spec -> spec.recipe(new ChangeParentPom(
"org.jenkins-ci.plugins",
"org.jenkins-ci.plugins",
"plugin",
"plugin",
"5.3",
null,
null,
null,
false
)),
pomXml(
"""
<project>
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>4.86</version>
<relativePath/>
</parent>
<artifactId>example-plugin</artifactId>
<version>0.8-SNAPSHOT</version>
<properties>
<jenkins.baseline>2.462</jenkins.baseline>
<jenkins.version>${jenkins.baseline}.3</jenkins.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.jenkins.tools.bom</groupId>
<artifactId>bom-${jenkins.baseline}.x</artifactId>
<version>3722.vcc62e7311580</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>scm-api</artifactId>
</dependency>
</dependencies>
<repositories>
<repository>
<id>repo.jenkins-ci.org</id>
<url>https://repo.jenkins-ci.org/public/</url>
</repository>
</repositories>
</project>
""",
"""
<project>
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>5.3</version>
<relativePath/>
</parent>
<artifactId>example-plugin</artifactId>
<version>0.8-SNAPSHOT</version>
<properties>
<jenkins.baseline>2.462</jenkins.baseline>
<jenkins.version>${jenkins.baseline}.3</jenkins.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.jenkins.tools.bom</groupId>
<artifactId>bom-${jenkins.baseline}.x</artifactId>
<version>3722.vcc62e7311580</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>scm-api</artifactId>
</dependency>
</dependencies>
<repositories>
<repository>
<id>repo.jenkins-ci.org</id>
<url>https://repo.jenkins-ci.org/public/</url>
</repository>
</repositories>
</project>
"""
)
);
}

@Test
void changeParentWithRelativePath() {
rewriteRun(
Expand Down