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

Add processing for nested modules with a parent version property #4472

Merged
merged 2 commits into from
Oct 11, 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.
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 @@ -495,9 +495,24 @@ public Pom download(GroupArtifactVersion gav,
// The requested gav might itself have an unresolved placeholder in the version, so also check raw values
for (Pom projectPom : projectPoms.values()) {
if (gav.getGroupId().equals(projectPom.getGroupId()) &&
gav.getArtifactId().equals(projectPom.getArtifactId()) &&
(gav.getVersion().equals(projectPom.getVersion()) || projectPom.getVersion().equals(projectPom.getValue(gav.getVersion())))) {
return projectPom;
gav.getArtifactId().equals(projectPom.getArtifactId())){
if (gav.getVersion().equals(projectPom.getVersion()) || projectPom.getVersion().equals(projectPom.getValue(gav.getVersion()))) {
return projectPom;
}
Parent parent = projectPom.getParent();
while (parent != null){
for (Pom project : projectPoms.values()) {
if (parent.getGroupId().equals(project.getGroupId()) && parent.getArtifactId().equals(project.getArtifactId())){
if (projectPom.getVersion().equals(project.getValue(gav.getVersion()))){
return projectPom;
}
parent = project.getParent();
if (parent == null){
break;
}
}
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -788,4 +788,112 @@ private static GroupArtifactVersion createArtifact(Path repository) throws IOExc
return new GroupArtifactVersion("org.openrewrite", "rewrite", "1.0.0");
}

@Test
void shouldNotThrowExceptionForModulesInModulesWithRightProperty() {
var gav = new GroupArtifactVersion("test", "test2", "${test}");

Path pomPath = Paths.get("test/test2/pom.xml");
Pom pom = Pom.builder()
.sourcePath(pomPath)
.repository(MAVEN_CENTRAL)
.properties(singletonMap("REPO_URL", MAVEN_CENTRAL.getUri()))
.parent(new Parent(new GroupArtifactVersion("test", "test", "${test}"), "../pom.xml"))
.gav(new ResolvedGroupArtifactVersion(
"${REPO_URL}", "test", "test2", "7.0.0", null))
.build();

ResolvedPom resolvedPom = ResolvedPom.builder()
.requested(pom)
.properties(singletonMap("REPO_URL", MAVEN_CENTRAL.getUri()))
.repositories(singletonList(MAVEN_CENTRAL))
.build();

Path pomPath2 = Paths.get("test/pom.xml");
Pom pom2 = Pom.builder()
.sourcePath(pomPath)
.repository(MAVEN_CENTRAL)
.properties(singletonMap("REPO_URL", MAVEN_CENTRAL.getUri()))
.parent(new Parent(new GroupArtifactVersion("test", "root-test", "${test}"), "../pom.xml"))
.gav(new ResolvedGroupArtifactVersion(
"${REPO_URL}", "test", "test", "7.0.0", null))
.build();


Path parentPomPath = Paths.get("pom.xml");
Pom parentPom = Pom.builder()
.sourcePath(parentPomPath)
.repository(MAVEN_CENTRAL)
.properties(singletonMap("test", "7.0.0"))
.parent(null)
.gav(new ResolvedGroupArtifactVersion(
"${REPO_URL}", "test", "root-test", "7.0.0", null))
.build();

Map<Path, Pom> pomsByPath = new HashMap<>();
pomsByPath.put(parentPomPath, parentPom);
pomsByPath.put(pomPath, pom);
pomsByPath.put(pomPath2, pom2);

String httpUrl = "http://%s.com".formatted(UUID.randomUUID());
MavenRepository nonexistentRepo = new MavenRepository("repo", httpUrl, null, null, false, null, null, null, null);

MavenPomDownloader downloader = new MavenPomDownloader(pomsByPath, ctx);

assertDoesNotThrow(() -> downloader.download(gav, Objects.requireNonNull(pom.getParent()).getRelativePath(), resolvedPom, singletonList(nonexistentRepo)));
}

@Test
void shouldThrowExceptionForModulesInModulesWithNoRightProperty() {
var gav = new GroupArtifactVersion("test", "test2", "${test}");

Path pomPath = Paths.get("test/test2/pom.xml");
Pom pom = Pom.builder()
.sourcePath(pomPath)
.repository(MAVEN_CENTRAL)
.properties(singletonMap("REPO_URL", MAVEN_CENTRAL.getUri()))
.parent(new Parent(new GroupArtifactVersion("test", "test", "${test}"), "../pom.xml"))
.gav(new ResolvedGroupArtifactVersion(
"${REPO_URL}", "test", "test2", "7.0.0", null))
.build();

ResolvedPom resolvedPom = ResolvedPom.builder()
.requested(pom)
.properties(singletonMap("REPO_URL", MAVEN_CENTRAL.getUri()))
.repositories(singletonList(MAVEN_CENTRAL))
.build();

Path pomPath2 = Paths.get("test/pom.xml");
Pom pom2 = Pom.builder()
.sourcePath(pomPath)
.repository(MAVEN_CENTRAL)
.properties(singletonMap("REPO_URL", MAVEN_CENTRAL.getUri()))
.parent(new Parent(new GroupArtifactVersion("test", "root-test", "${test}"), "../pom.xml"))
.gav(new ResolvedGroupArtifactVersion(
"${REPO_URL}", "test", "test", "7.0.0", null))
.build();


Path parentPomPath = Paths.get("pom.xml");
Pom parentPom = Pom.builder()
.sourcePath(parentPomPath)
.repository(MAVEN_CENTRAL)
.properties(singletonMap("tt", "7.0.0"))
.parent(null)
.gav(new ResolvedGroupArtifactVersion(
"${REPO_URL}", "test", "root-test", "7.0.0", null))
.build();

Map<Path, Pom> pomsByPath = new HashMap<>();
pomsByPath.put(parentPomPath, parentPom);
pomsByPath.put(pomPath, pom);
pomsByPath.put(pomPath2, pom2);

String httpUrl = "http://%s.com".formatted(UUID.randomUUID());
MavenRepository nonexistentRepo = new MavenRepository("repo", httpUrl, null, null, false, null, null, null, null);

MavenPomDownloader downloader = new MavenPomDownloader(pomsByPath, ctx);

assertThrows(IllegalArgumentException.class, () -> downloader.download(gav, Objects.requireNonNull(pom.getParent()).getRelativePath(), resolvedPom, singletonList(nonexistentRepo)));
}

}