Skip to content

Commit 0a91b12

Browse files
authored
groupId and artifactId on project POM definition need to be resolved fully to prevent being interpreted as an external POM (#6477)
* Add test to reproduce the issue * Resolve `groupId` and `artifactId` on project POMs as well
1 parent d3ba15f commit 0a91b12

File tree

2 files changed

+86
-2
lines changed

2 files changed

+86
-2
lines changed

rewrite-maven/src/main/java/org/openrewrite/maven/internal/MavenPomDownloader.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,8 @@ private Map<GroupArtifactVersion, Pom> projectPomsByGav(Map<Path, Pom> projectPo
176176
List<Pom> ancestryWithinProject = getAncestryWithinProject(projectPom, projectPoms);
177177
Map<String, String> mergedProperties = mergeProperties(ancestryWithinProject);
178178
GroupArtifactVersion gav = new GroupArtifactVersion(
179-
projectPom.getGroupId(),
180-
projectPom.getArtifactId(),
179+
ResolvedPom.placeholderHelper.replacePlaceholders(projectPom.getGroupId(), mergedProperties::get),
180+
ResolvedPom.placeholderHelper.replacePlaceholders(projectPom.getArtifactId(), mergedProperties::get),
181181
ResolvedPom.placeholderHelper.replacePlaceholders(projectPom.getVersion(), mergedProperties::get)
182182
);
183183
result.put(gav, projectPom);

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

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.openrewrite.maven.internal.MavenParsingException;
4040
import org.openrewrite.maven.tree.*;
4141
import org.openrewrite.test.RewriteTest;
42+
import org.openrewrite.test.SourceSpecs;
4243
import org.openrewrite.test.TypeValidation;
4344
import org.openrewrite.tree.ParseError;
4445

@@ -51,6 +52,7 @@
5152
import static org.assertj.core.api.Assertions.*;
5253
import static org.openrewrite.java.Assertions.mavenProject;
5354
import static org.openrewrite.maven.Assertions.pomXml;
55+
import static org.openrewrite.test.SourceSpecs.dir;
5456

5557
class MavenParserTest implements RewriteTest {
5658

@@ -4810,4 +4812,86 @@ void transitiveDependencyVersion() {
48104812
)
48114813
);
48124814
}
4815+
4816+
@Test
4817+
void groupIdAndArtifactIdAsProperties() {
4818+
rewriteRun(
4819+
mavenProject("my-app",
4820+
pomXml("""
4821+
<project>
4822+
<groupId>com.example</groupId>
4823+
<artifactId>parent</artifactId>
4824+
<version>1</version>
4825+
<packaging>pom</packaging>
4826+
<properties>
4827+
<my-app.child-a.groupId>com.example</my-app.child-a.groupId>
4828+
<my-app.child-a.artifactId>child-a</my-app.child-a.artifactId>
4829+
</properties>
4830+
<modules>
4831+
<module>child-a</module>
4832+
<module>child-b</module>
4833+
</modules>
4834+
</project>
4835+
"""
4836+
),
4837+
dir("child-a",
4838+
pomXml(
4839+
"""
4840+
<project>
4841+
<groupId>${my-app.child-a.groupId}</groupId>
4842+
<artifactId>${my-app.child-a.artifactId}</artifactId>
4843+
<version>1</version>
4844+
<parent>
4845+
<groupId>com.example</groupId>
4846+
<artifactId>parent</artifactId>
4847+
<version>1</version>
4848+
</parent>
4849+
<dependencies>
4850+
<dependency>
4851+
<groupId>org.springframework</groupId>
4852+
<artifactId>spring-core</artifactId>
4853+
<version>6.2.15</version>
4854+
</dependency>
4855+
</dependencies>
4856+
</project>
4857+
"""
4858+
)
4859+
),
4860+
dir("child-b",
4861+
pomXml(
4862+
"""
4863+
<project>
4864+
<groupId>com.example</groupId>
4865+
<artifactId>child-b</artifactId>
4866+
<version>1</version>
4867+
<parent>
4868+
<groupId>com.example</groupId>
4869+
<artifactId>parent</artifactId>
4870+
<version>1</version>
4871+
</parent>
4872+
<dependencies>
4873+
<dependency>
4874+
<groupId>${my-app.child-a.groupId}</groupId>
4875+
<artifactId>${my-app.child-a.artifactId}</artifactId>
4876+
<version>1</version>
4877+
</dependency>
4878+
</dependencies>
4879+
</project>
4880+
""",
4881+
spec -> spec.afterRecipe(pom -> {
4882+
assertThat(pom).isNotNull();
4883+
Optional<MavenResolutionResult> maybeMrr = pom.getMarkers().findFirst(MavenResolutionResult.class);
4884+
assertThat(maybeMrr).isPresent();
4885+
4886+
MavenResolutionResult mrr = maybeMrr.get();
4887+
assertThat(mrr.getDependencies().get(Scope.Compile).stream())
4888+
.anyMatch(resolvedDependency -> "org.springframework".equals(resolvedDependency.getGroupId()) &&
4889+
"spring-core".equals(resolvedDependency.getArtifactId()) &&
4890+
"6.2.15".equals(resolvedDependency.getVersion()));
4891+
})
4892+
)
4893+
)
4894+
)
4895+
);
4896+
}
48134897
}

0 commit comments

Comments
 (0)