Skip to content

Commit

Permalink
Don't assume module that has child modules is the parent of those mod…
Browse files Browse the repository at this point in the history
…ules
  • Loading branch information
aloubyansky committed Jan 25, 2024
1 parent 52292c7 commit 09c312d
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ LocalProject load() throws BootstrapMavenException {
private void loadModule(RawModule rawModule, List<RawModule> newModules) {
var moduleDir = rawModule.pom.getParent();
if (moduleDir == null) {
moduleDir = Path.of("/");
moduleDir = getFsRootDir();
}
if (loadedPoms.containsKey(moduleDir)) {
return;
Expand All @@ -190,25 +190,39 @@ private void loadModule(RawModule rawModule, List<RawModule> newModules) {
return;
}
for (var module : rawModule.model.getModules()) {
moduleQueue.add(
new RawModule(rawModule, rawModule.model.getProjectDirectory().toPath().resolve(module).resolve(POM_XML)));
queueModule(rawModule.model.getProjectDirectory().toPath().resolve(module));
}
for (var profile : rawModule.model.getProfiles()) {
for (var module : profile.getModules()) {
moduleQueue.add(new RawModule(rawModule,
rawModule.model.getProjectDirectory().toPath().resolve(module).resolve(POM_XML)));
queueModule(rawModule.model.getProjectDirectory().toPath().resolve(module));
}
}
if (rawModule.parent == null) {
final Path parentPom = rawModule.getParentPom();
if (parentPom != null) {
var parent = new RawModule(parentPom);
rawModule.parent = parent;
moduleQueue.add(parent);
var parentDir = parentPom.getParent();
if (parentDir == null) {
parentDir = getFsRootDir();
}
if (!loadedPoms.containsKey(parentDir)) {
var parent = new RawModule(parentPom);
rawModule.parent = parent;
moduleQueue.add(parent);
}
}
}
}

private static Path getFsRootDir() {
return Path.of("/");
}

private void queueModule(Path dir) {
if (!loadedPoms.containsKey(dir)) {
moduleQueue.add(new RawModule(dir.resolve(POM_XML)));
}
}

@Override
public Model resolveRawModel(String groupId, String artifactId, String versionConstraint) {
return loadedModules.get(new GAV(groupId, artifactId, versionConstraint));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,15 +375,32 @@ public void loadWorkspaceFromModuleDirWithParentInChildDir() throws Exception {
assertParents(project, "acme-parent", "acme-dependencies");
}

@Test
public void loadWorkspaceFromModuleDirWithParentInSiblingDir() throws Exception {
final URL projectUrl = Thread.currentThread().getContextClassLoader()
.getResource("workspace-parent-is-not-root-dir/acme-backend/acme-backend-lib");
assertNotNull(projectUrl);
final Path projectDir = Paths.get(projectUrl.toURI());
assertTrue(Files.exists(projectDir));
final LocalProject project = LocalProject.loadWorkspace(projectDir);

assertEquals("acme-backend-lib", project.getArtifactId());
assertWorkspaceWithParentInChildDir(project);

assertParents(project, "acme-backend", "acme-backend-parent", "acme-parent", "acme-dependencies");
}

private void assertWorkspaceWithParentInChildDir(final LocalProject project) {
final LocalWorkspace workspace = project.getWorkspace();
assertNotNull(workspace.getProject("org.acme", "acme"));
assertNotNull(workspace.getProject("org.acme", "acme-parent"));
assertNotNull(workspace.getProject("org.acme", "acme-dependencies"));
assertNotNull(workspace.getProject("org.acme", "acme-backend"));
assertNotNull(workspace.getProject("org.acme", "acme-backend-parent"));
assertNotNull(workspace.getProject("org.acme", "acme-backend-lib"));
assertNotNull(workspace.getProject("org.acme", "acme-backend-rest-api"));
assertNotNull(workspace.getProject("org.acme", "acme-application"));
assertEquals(6, workspace.getProjects().size());
assertEquals(8, workspace.getProjects().size());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.acme</groupId>
<artifactId>acme-backend</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../</relativePath>
</parent>

<artifactId>acme-backend-lib</artifactId>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.acme</groupId>
<artifactId>acme-parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../../acme-parent</relativePath>
</parent>

<artifactId>acme-backend-parent</artifactId>
<packaging>pom</packaging>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@

<parent>
<groupId>org.acme</groupId>
<artifactId>acme-parent</artifactId>
<artifactId>acme-backend-parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../acme-parent</relativePath>
<relativePath>acme-backend-parent/</relativePath>
</parent>

<artifactId>acme-backend</artifactId>
<packaging>pom</packaging>

<modules>
<module>acme-backend-rest-api</module>
<module>acme-backend-parent</module>
<module>acme-backend-lib</module>
</modules>

</project>

0 comments on commit 09c312d

Please sign in to comment.