Skip to content

Commit 50ea20c

Browse files
Improve root build file and root module detection for Maven reactors
1 parent c96893f commit 50ea20c

File tree

3 files changed

+455
-10
lines changed

3 files changed

+455
-10
lines changed

components/sbm-core/src/main/java/org/springframework/sbm/build/api/ApplicationModules.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@
2121
import org.springframework.sbm.build.impl.OpenRewriteMavenBuildFile;
2222

2323
import java.nio.file.Path;
24-
import java.util.ArrayList;
25-
import java.util.List;
26-
import java.util.Optional;
27-
import java.util.Set;
24+
import java.util.*;
2825
import java.util.stream.Collectors;
2926
import java.util.stream.Stream;
3027

@@ -43,10 +40,21 @@ public Stream<Module> stream() {
4340
}
4441

4542
public Module getRootModule() {
43+
// 1st try: module explicitly marked as having a root build file
4644
return modules.stream()
4745
.filter(m -> m.getBuildFile().isRootBuildFile())
4846
.findFirst()
49-
.orElseThrow(() -> new RootBuildFileNotFoundException("Module with root build file is missing"));
47+
// 2nd try (fallback): no module marked as root → choose the one
48+
// whose build file path is closest to the project root
49+
.orElseGet(() -> modules.stream()
50+
.min(Comparator.comparingInt(m -> pathDepth(m.getBuildFile())))
51+
.orElseThrow(() -> new RootBuildFileNotFoundException("Module with root build file is missing")));
52+
}
53+
54+
private int pathDepth(BuildFile buildFile) {
55+
Path path = buildFile.getSourcePath();
56+
// if for some reason there is no path, treat it as "very deep"
57+
return (path == null) ? Integer.MAX_VALUE : path.getNameCount();
5058
}
5159

5260
public List<Module> list() {

components/sbm-core/src/main/java/org/springframework/sbm/build/api/RootBuildFileFilter.java

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,42 @@
1515
*/
1616
package org.springframework.sbm.build.api;
1717

18+
import org.jetbrains.annotations.NotNull;
1819
import org.springframework.sbm.project.resource.ProjectResourceSet;
1920
import org.springframework.sbm.project.resource.filter.ProjectResourceFinder;
2021

22+
import java.nio.file.Path;
23+
import java.util.Comparator;
24+
import java.util.List;
25+
import java.util.stream.Collectors;
26+
2127
public class RootBuildFileFilter implements ProjectResourceFinder<BuildFile> {
28+
2229
@Override
23-
public BuildFile apply(ProjectResourceSet projectResourceSet) {
24-
return projectResourceSet.stream()
30+
public BuildFile apply(@NotNull ProjectResourceSet projectResourceSet) {
31+
// collect all build files (pom.xml, etc.)
32+
List<BuildFile> buildFiles = projectResourceSet.stream()
2533
.filter(pr -> BuildFile.class.isAssignableFrom(pr.getClass()))
2634
.map(BuildFile.class::cast)
27-
.filter(bf -> bf.isRootBuildFile())
35+
.collect(Collectors.toList());
36+
37+
if (buildFiles.isEmpty()) {
38+
throw new RootBuildFileNotFoundException("Could not find any BuildFile in project.");
39+
}
40+
41+
// 1st try: existing logic – respect explicit isRootBuildFile flag
42+
return buildFiles.stream()
43+
.filter(BuildFile::isRootBuildFile)
2844
.findFirst()
29-
.orElseThrow(() -> new RootBuildFileNotFoundException("Could not find BuildFile for root module."));
45+
// 2nd try (fallback): no explicit root → choose the build file
46+
// whose source path is closest to the project root (smallest depth)
47+
.orElseGet(() -> buildFiles.stream()
48+
.min(Comparator.comparingInt(bf -> pathDepth(bf.getSourcePath())))
49+
.orElseThrow(() -> new RootBuildFileNotFoundException("Could not find BuildFile for root module.")));
50+
}
51+
52+
private int pathDepth(Path path) {
53+
// defensive: null check, though OpenRewrite usually always has a path
54+
return (path == null) ? Integer.MAX_VALUE : path.getNameCount();
3055
}
31-
}
56+
}

0 commit comments

Comments
 (0)