From 09c312d2a53fdd3a198eb48816d2ea65acc9b5f6 Mon Sep 17 00:00:00 2001 From: Alexey Loubyansky Date: Thu, 25 Jan 2024 23:02:10 +0100 Subject: [PATCH] Don't assume module that has child modules is the parent of those modules --- .../maven/workspace/WorkspaceLoader.java | 30 ++++++++++++++----- .../test/LocalWorkspaceDiscoveryTest.java | 19 +++++++++++- .../acme-backend/acme-backend-lib/pom.xml | 18 +++++++++++ .../acme-backend/acme-backend-parent/pom.xml | 19 ++++++++++++ .../acme-backend/pom.xml | 6 ++-- 5 files changed, 81 insertions(+), 11 deletions(-) create mode 100644 independent-projects/bootstrap/maven-resolver/src/test/resources/workspace-parent-is-not-root-dir/acme-backend/acme-backend-lib/pom.xml create mode 100644 independent-projects/bootstrap/maven-resolver/src/test/resources/workspace-parent-is-not-root-dir/acme-backend/acme-backend-parent/pom.xml diff --git a/independent-projects/bootstrap/maven-resolver/src/main/java/io/quarkus/bootstrap/resolver/maven/workspace/WorkspaceLoader.java b/independent-projects/bootstrap/maven-resolver/src/main/java/io/quarkus/bootstrap/resolver/maven/workspace/WorkspaceLoader.java index f057cecb6fbbd..77cd35b4963fd 100644 --- a/independent-projects/bootstrap/maven-resolver/src/main/java/io/quarkus/bootstrap/resolver/maven/workspace/WorkspaceLoader.java +++ b/independent-projects/bootstrap/maven-resolver/src/main/java/io/quarkus/bootstrap/resolver/maven/workspace/WorkspaceLoader.java @@ -166,7 +166,7 @@ LocalProject load() throws BootstrapMavenException { private void loadModule(RawModule rawModule, List newModules) { var moduleDir = rawModule.pom.getParent(); if (moduleDir == null) { - moduleDir = Path.of("/"); + moduleDir = getFsRootDir(); } if (loadedPoms.containsKey(moduleDir)) { return; @@ -190,25 +190,39 @@ private void loadModule(RawModule rawModule, List 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)); diff --git a/independent-projects/bootstrap/maven-resolver/src/test/java/io/quarkus/bootstrap/workspace/test/LocalWorkspaceDiscoveryTest.java b/independent-projects/bootstrap/maven-resolver/src/test/java/io/quarkus/bootstrap/workspace/test/LocalWorkspaceDiscoveryTest.java index e1b74d3a5b6ab..1af195dd079b7 100644 --- a/independent-projects/bootstrap/maven-resolver/src/test/java/io/quarkus/bootstrap/workspace/test/LocalWorkspaceDiscoveryTest.java +++ b/independent-projects/bootstrap/maven-resolver/src/test/java/io/quarkus/bootstrap/workspace/test/LocalWorkspaceDiscoveryTest.java @@ -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 diff --git a/independent-projects/bootstrap/maven-resolver/src/test/resources/workspace-parent-is-not-root-dir/acme-backend/acme-backend-lib/pom.xml b/independent-projects/bootstrap/maven-resolver/src/test/resources/workspace-parent-is-not-root-dir/acme-backend/acme-backend-lib/pom.xml new file mode 100644 index 0000000000000..25a6f3a7840f9 --- /dev/null +++ b/independent-projects/bootstrap/maven-resolver/src/test/resources/workspace-parent-is-not-root-dir/acme-backend/acme-backend-lib/pom.xml @@ -0,0 +1,18 @@ + + + + 4.0.0 + + + org.acme + acme-backend + 1.0.0-SNAPSHOT + ../ + + + acme-backend-lib + + diff --git a/independent-projects/bootstrap/maven-resolver/src/test/resources/workspace-parent-is-not-root-dir/acme-backend/acme-backend-parent/pom.xml b/independent-projects/bootstrap/maven-resolver/src/test/resources/workspace-parent-is-not-root-dir/acme-backend/acme-backend-parent/pom.xml new file mode 100644 index 0000000000000..116bd703767a8 --- /dev/null +++ b/independent-projects/bootstrap/maven-resolver/src/test/resources/workspace-parent-is-not-root-dir/acme-backend/acme-backend-parent/pom.xml @@ -0,0 +1,19 @@ + + + + 4.0.0 + + + org.acme + acme-parent + 1.0.0-SNAPSHOT + ../../acme-parent + + + acme-backend-parent + pom + + diff --git a/independent-projects/bootstrap/maven-resolver/src/test/resources/workspace-parent-is-not-root-dir/acme-backend/pom.xml b/independent-projects/bootstrap/maven-resolver/src/test/resources/workspace-parent-is-not-root-dir/acme-backend/pom.xml index 3d0a951067cb9..af09bd7d66523 100644 --- a/independent-projects/bootstrap/maven-resolver/src/test/resources/workspace-parent-is-not-root-dir/acme-backend/pom.xml +++ b/independent-projects/bootstrap/maven-resolver/src/test/resources/workspace-parent-is-not-root-dir/acme-backend/pom.xml @@ -8,9 +8,9 @@ org.acme - acme-parent + acme-backend-parent 1.0.0-SNAPSHOT - ../acme-parent + acme-backend-parent/ acme-backend @@ -18,6 +18,8 @@ acme-backend-rest-api + acme-backend-parent + acme-backend-lib