Skip to content

Commit 919b26f

Browse files
committed
model-builder: simplify subproject auto-discovery decision (apache#11124)
- Treat explicit empty <subprojects/> as disabling discovery. - Base decision solely on explicit subprojects/modules in the main model; ignore profiles. - Inline the check using location tracking and remove profile scanning. (cherry picked from commit b2690b7)
1 parent 3fc29ff commit 919b26f

File tree

6 files changed

+85
-1
lines changed

6 files changed

+85
-1
lines changed

impl/maven-core/src/test/java/org/apache/maven/project/DefaultMavenProjectBuilderTest.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,4 +545,44 @@ public void testSubprojectDiscovery() throws Exception {
545545
MavenProject parent = p1.getArtifactId().equals("parent") ? p1 : p2;
546546
assertEquals(List.of("child"), parent.getModel().getDelegate().getSubprojects());
547547
}
548+
549+
@Test
550+
public void testEmptySubprojectsElementPreventsDiscovery() throws Exception {
551+
File pom = getTestFile("src/test/resources/projects/subprojects-empty/pom.xml");
552+
ProjectBuildingRequest configuration = newBuildingRequest();
553+
InternalSession internalSession = InternalSession.from(configuration.getRepositorySession());
554+
InternalMavenSession mavenSession = InternalMavenSession.from(internalSession);
555+
mavenSession
556+
.getMavenSession()
557+
.getRequest()
558+
.setRootDirectory(pom.toPath().getParent());
559+
560+
List<ProjectBuildingResult> results = projectBuilder.build(List.of(pom), true, configuration);
561+
// Should only build the parent project, not discover the child
562+
assertEquals(1, results.size());
563+
MavenProject parent = results.get(0).getProject();
564+
assertEquals("parent", parent.getArtifactId());
565+
// The subprojects list should be empty since we explicitly defined an empty <subprojects /> element
566+
assertTrue(parent.getModel().getDelegate().getSubprojects().isEmpty());
567+
}
568+
569+
@Test
570+
public void testEmptyModulesElementPreventsDiscovery() throws Exception {
571+
File pom = getTestFile("src/test/resources/projects/modules-empty/pom.xml");
572+
ProjectBuildingRequest configuration = newBuildingRequest();
573+
InternalSession internalSession = InternalSession.from(configuration.getRepositorySession());
574+
InternalMavenSession mavenSession = InternalMavenSession.from(internalSession);
575+
mavenSession
576+
.getMavenSession()
577+
.getRequest()
578+
.setRootDirectory(pom.toPath().getParent());
579+
580+
List<ProjectBuildingResult> results = projectBuilder.build(List.of(pom), true, configuration);
581+
// Should only build the parent project, not discover the child
582+
assertEquals(1, results.size());
583+
MavenProject parent = results.get(0).getProject();
584+
assertEquals("parent", parent.getArtifactId());
585+
// The modules list should be empty since we explicitly defined an empty <modules /> element
586+
assertTrue(parent.getModel().getDelegate().getModules().isEmpty());
587+
}
548588
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<project xmlns="http://maven.apache.org/POM/4.1.0">
2+
<parent>
3+
<groupId>modules-empty</groupId>
4+
<artifactId>parent</artifactId>
5+
</parent>
6+
<artifactId>child</artifactId>
7+
<packaging>jar</packaging>
8+
</project>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<project xmlns="http://maven.apache.org/POM/4.1.0">
2+
<groupId>modules-empty</groupId>
3+
<artifactId>parent</artifactId>
4+
<version>1</version>
5+
<packaging>pom</packaging>
6+
<modules />
7+
</project>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<project xmlns="http://maven.apache.org/POM/4.1.0">
2+
<parent>
3+
<groupId>subprojects-empty</groupId>
4+
<artifactId>parent</artifactId>
5+
</parent>
6+
<artifactId>child</artifactId>
7+
<packaging>jar</packaging>
8+
</project>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<project xmlns="http://maven.apache.org/POM/4.1.0">
2+
<groupId>subprojects-empty</groupId>
3+
<artifactId>parent</artifactId>
4+
<version>1</version>
5+
<packaging>pom</packaging>
6+
<subprojects />
7+
</project>

impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultModelBuilder.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1366,7 +1366,7 @@ Model doReadFileModel() throws ModelBuilderException {
13661366
}
13671367

13681368
// subprojects discovery
1369-
if (getSubprojects(model).isEmpty()
1369+
if (!hasSubprojectsDefined(model)
13701370
// only discover subprojects if POM > 4.0.0
13711371
&& !MODEL_VERSION_4_0_0.equals(model.getModelVersion())
13721372
// and if packaging is POM (we check type, but the session is not yet available,
@@ -1902,6 +1902,20 @@ private static List<String> getSubprojects(Model activated) {
19021902
return subprojects;
19031903
}
19041904

1905+
/**
1906+
* Checks if subprojects are explicitly defined in the main model.
1907+
* This method distinguishes between:
1908+
* 1. No subprojects/modules element present - returns false (should auto-discover)
1909+
* 2. Empty subprojects/modules element present - returns true (should NOT auto-discover)
1910+
* 3. Non-empty subprojects/modules - returns true (should NOT auto-discover)
1911+
*/
1912+
@SuppressWarnings("deprecation")
1913+
private static boolean hasSubprojectsDefined(Model model) {
1914+
// Only consider the main model: profiles do not influence auto-discovery
1915+
// Inline the check for explicit elements using location tracking
1916+
return model.getLocation("subprojects") != null || model.getLocation("modules") != null;
1917+
}
1918+
19051919
@Override
19061920
public Model buildRawModel(ModelBuilderRequest request) throws ModelBuilderException {
19071921
RequestTraceHelper.ResolverTrace trace = RequestTraceHelper.enter(request.getSession(), request);

0 commit comments

Comments
 (0)