Skip to content

Commit e629be2

Browse files
Circular Maven ${project.version} resolution (#5573)
* Circular Maven project.version resolution * Update rewrite-maven/src/test/java/org/openrewrite/maven/tree/ResolvedPomTest.java Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 9c1a58e commit e629be2

3 files changed

Lines changed: 59 additions & 9 deletions

File tree

rewrite-core/src/main/java/org/openrewrite/internal/PropertyPlaceholderHelper.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,10 @@ protected String parseStringValue(String value, Function<String, @Nullable Strin
9090
if (visitedPlaceholders == null) {
9191
visitedPlaceholders = new HashSet<>(4);
9292
}
93-
if (!visitedPlaceholders.add(originalPlaceholder)) {
94-
throw new IllegalArgumentException(
95-
"Circular placeholder reference '" + originalPlaceholder + "' in property definitions");
93+
if (visitedPlaceholders.add(originalPlaceholder)) {
94+
placeholder = parseStringValue(placeholder, placeholderResolver, visitedPlaceholders);
9695
}
9796
// Recursive invocation, parsing placeholders contained in the placeholder key.
98-
placeholder = parseStringValue(placeholder, placeholderResolver, visitedPlaceholders);
9997
// Now obtain the value for the fully resolved key...
10098
String propVal = placeholderResolver.apply(placeholder);
10199
if (propVal == null && valueSeparator != null) {

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,11 @@ public String getPackaging() {
316316
case "version":
317317
case "project.version":
318318
case "pom.version":
319-
return requested.getVersion();
319+
String version = requested.getVersion();
320+
if (version.contains(property) && requested.getParent() != null) {
321+
return requested.getParent().getVersion();
322+
}
323+
return version;
320324
case "project.parent.version":
321325
case "parent.version":
322326
return requested.getParent() != null ? requested.getParent().getVersion() : null;

rewrite-maven/src/test/java/org/openrewrite/maven/tree/ResolvedPomTest.java

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.util.List;
3434

3535
import static org.assertj.core.api.Assertions.assertThat;
36+
import static org.openrewrite.java.Assertions.mavenProject;
3637
import static org.openrewrite.maven.Assertions.pomXml;
3738

3839
class ResolvedPomTest implements RewriteTest {
@@ -106,6 +107,53 @@ void resolveDependencyWithPlaceholderClassifier() {
106107
);
107108
}
108109

110+
@Test
111+
void projectVersion() {
112+
rewriteRun(
113+
pomXml(
114+
"""
115+
<project>
116+
<groupId>org.example</groupId>
117+
<artifactId>parent</artifactId>
118+
<version>3.17.0</version>
119+
</project>
120+
"""
121+
),
122+
mavenProject(
123+
"app",
124+
pomXml(
125+
"""
126+
<project>
127+
<groupId>org.example</groupId>
128+
<artifactId>app</artifactId>
129+
<version>${project.version}</version>
130+
<parent>
131+
<groupId>org.example</groupId>
132+
<artifactId>parent</artifactId>
133+
<version>3.17.0</version>
134+
<relativeParent>../pom.xml</relativeParent>
135+
</parent>
136+
<dependencies>
137+
<dependency>
138+
<groupId>org.apache.commons</groupId>
139+
<artifactId>commons-lang3</artifactId>
140+
<version>${project.version}</version>
141+
</dependency>
142+
</dependencies>
143+
</project>
144+
""",
145+
spec -> spec
146+
.afterRecipe(doc -> {
147+
List<ResolvedDependency> deps = doc.getMarkers().findFirst(MavenResolutionResult.class).orElseThrow().getDependencies()
148+
.get(Scope.Compile);
149+
assertThat(deps).hasSize(1);
150+
assertThat(deps.getFirst().getVersion()).isEqualTo("3.17.0");
151+
})
152+
)
153+
)
154+
);
155+
}
156+
109157
@Test
110158
void resolveExecutionsFromDifferentParents() {
111159
String grandFather = """
@@ -226,7 +274,7 @@ void resolveExecutionsFromDifferentParents() {
226274
pomXml(father, spec -> spec.path("father/pom.xml")),
227275
pomXml(child, spec -> spec.path("father/child/pom.xml").afterRecipe(doc -> {
228276
List<Plugin> pluginManagement = doc.getMarkers().findFirst(MavenResolutionResult.class)
229-
.get().getPom().getPluginManagement();
277+
.orElseThrow().getPom().getPluginManagement();
230278
assertThat(pluginManagement).hasSize(1);
231279
Plugin plugin = pluginManagement.getFirst();
232280
assertThat(plugin).extracting(Plugin::getArtifactId).isEqualTo("maven-enforcer-plugin");
@@ -469,7 +517,7 @@ public void downloadError(GroupArtifactVersion gav, List<String> attemptedUris,
469517
pomXml(father, spec -> spec.path("pom.xml")),
470518
pomXml(childA, spec -> spec.path("childA/pom.xml")),
471519
pomXml(childB, spec -> spec.path("childB/pom.xml").afterRecipe(doc -> {
472-
ResolvedPom pom = doc.getMarkers().findFirst(MavenResolutionResult.class).get().getPom();
520+
ResolvedPom pom = doc.getMarkers().findFirst(MavenResolutionResult.class).orElseThrow().getPom();
473521
String version = pom.getManagedVersion("com.some", "some-artifact", null, null);
474522
// Assert that version is not null!
475523
assertThat(version).isEqualTo("1");
@@ -540,7 +588,7 @@ void ignoreScopeInDependencyManagement(@TempDir Path localRepository) throws IOE
540588
""",
541589
spec -> spec.path("child/pom.xml")
542590
.afterRecipe(doc -> {
543-
ResolvedPom pom = doc.getMarkers().findFirst(MavenResolutionResult.class).get().getPom();
591+
ResolvedPom pom = doc.getMarkers().findFirst(MavenResolutionResult.class).orElseThrow().getPom();
544592
assertThat(pom.getDependencyManagement()).hasSize(1);
545593
})
546594
)
@@ -586,7 +634,7 @@ void firstUniqueManagedDependencyWins() {
586634
""",
587635
spec -> spec
588636
.afterRecipe(doc -> {
589-
ResolvedPom pom = doc.getMarkers().findFirst(MavenResolutionResult.class).get().getPom();
637+
ResolvedPom pom = doc.getMarkers().findFirst(MavenResolutionResult.class).orElseThrow().getPom();
590638
assertThat(pom.getManagedVersion("com.fasterxml.jackson.core", "jackson-core", null, null)).isEqualTo("2.16.1");
591639
})
592640
)

0 commit comments

Comments
 (0)