apache/maven · warning
The top project ({}) cannot be found in the reactor from roo
Error message
The top project ({}) cannot be found in the reactor from root project ({}). Make sure the root directory is correct (a missing '.mvn' directory in the root project is the most common cause) and the project is correctly included in the reactor (missing activated profiles, command line options, etc.). For this build, the top project will be used as the root project. What it means
In the Maven 4 recursive model builder, buildBuildPom() located a root POM via the session root directory (detected through the .mvn marker) and loaded the model tree from it, but the requested top POM (request source path) never appeared in that tree: result.getFileModel() stayed null. Maven then discards the mapped sources, logs this warning, and retries with the top POM itself acting as the root. The message lists the two classic causes: an incorrect root directory because a .mvn directory is missing in the actual root project, or the top project legitimately not being part of the root's reactor (profile/module selection).
Source
Thrown at impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultModelBuilder.java:908
* result and recursively including all its child results.
*
* @param r The initial DefaultModelBuilderResult object from which to generate the stream.
* @return A Stream of DefaultModelBuilderResult objects, starting with the provided result
* and including all its child results.
*/
Stream<DefaultModelBuilderResult> results(DefaultModelBuilderResult r) {
return Stream.concat(Stream.of(r), r.getChildren().stream().flatMap(this::results));
}
private void loadFromRoot(Path root, Path top) {
try (PhasingExecutor executor = createExecutor()) {
DefaultModelBuilderResult r = Objects.equals(top, root)
? result
: new DefaultModelBuilderResult(request, ProblemCollector.create(session));
loadFilePom(executor, top, root, Set.of(), r);
}
if (result.getFileModel() == null && !Objects.equals(top, root)) {
logger.warn(
"The top project ({}) cannot be found in the reactor from root project ({}). "
+ "Make sure the root directory is correct (a missing '.mvn' directory in the root "
+ "project is the most common cause) and the project is correctly included "
+ "in the reactor (missing activated profiles, command line options, etc.). For this "
+ "build, the top project will be used as the root project.",
top,
root);
mappedSources.clear();
loadFromRoot(top, top);
}
}
private void loadFilePom(
Executor executor, Path top, Path pom, Set<Path> parents, DefaultModelBuilderResult r) {
try {
Path pomDirectory = Files.isDirectory(pom) ? pom : pom.getParent();
ModelSource src = Sources.buildSource(pom);
Model model = derive(src, r).readFileModel();View on GitHub (pinned to e4093d4e12)
Solutions
- Add a .mvn directory to the intended root project (even empty): mkdir -p root/.mvn, so root detection stops at the right level
- Verify the top project is reachable from the root POM: listed in <modules> (directly or via intermediate aggregators) without relying on non-activated profiles
- Run the build from the root with module selection (mvn -pl :module -am) instead of mvn -f sub/pom.xml
- Remove stray .mvn directories in ancestor directories that hijack root detection, or point Maven at the right root explicitly with the --root/-m option
Example fix
# before: no .mvn at the real root, detection climbs too far
my-repo/
.mvn/ # stray marker at some outer level
real-root/ # no .mvn here
pom.xml
sub/pom.xml
mvn -f real-root/sub/pom.xml package # -> warning, top used as root
# after: mark the real root
mkdir -p real-root/.mvn
mvn -f real-root/sub/pom.xml package # reactor found from real-root Defensive patterns
Strategy: validation
Validate before calling
// Embedding: ensure the requested POM is inside the detected root's reactor before building
Path top = request.getSource().getPath().toAbsolutePath().normalize();
Path root = session.getRootDirectory(); // throws if no root found
if (!top.startsWith(root)) {
throw new IllegalStateException(top + " is not under the detected root " + root
+ "; check .mvn placement or pass the correct --root");
}
// Shell check: warn when building a module whose aggregator does not list it
# grep -q "<module>sub</module>" root/pom.xml || echo "sub not in root reactor" Prevention
- Put a .mvn directory (even empty) in the repository root you want Maven to treat as root
- List every buildable module in the root aggregator's <modules>, including profile-gated ones you build in CI
- Prefer mvn -pl :module -am from the root over mvn -f sub/pom.xml
- Delete stray .mvn directories in ancestors, or pin the root explicitly with the --root option
When it happens
Trigger: Running mvn -f sub/pom.xml (or from a subdirectory) where the discovered root directory contains a different project tree that does not include sub: e.g. a .mvn directory exists in a parent checkout but the target module is not listed in its <modules>, or no .mvn exists in the intended root so root detection climbed to an outer directory that has one.
Common situations: Building one module of a large checkout without .mvn at the intended root; adding a new submodule but forgetting to list it in the aggregator POM's <modules>; module inclusion hidden behind a profile (-P) that was not activated; monorepos where an unrelated ancestor directory carries a stray .mvn.
Related errors
- Unable to find the root directory. Create a .mvn directory i
- Two or more projects in the reactor have the same identifier
- Unable to find the root directory. Create a .mvn directory i
- The project exclusion%s in --projects/-pl resulted in an emp
- Invalid reactor make behavior: {}
AI-assisted analysis of apache/maven@e4093d4e12 (2026-08-21).
Data as JSON: /api/errors/d2a8984ab545e7d6.
Report an issue: GitHub.