{"record":{"id":"d2a8984ab545e7d6","repo":"apache/maven","slug":"the-top-project-cannot-be-found-in-the-reacto","errorCode":null,"errorMessage":"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.","messagePattern":"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\\.","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultModelBuilder.java","lineNumber":908,"sourceCode":"         * result and recursively including all its child results.\n         *\n         * @param r The initial DefaultModelBuilderResult object from which to generate the stream.\n         * @return A Stream of DefaultModelBuilderResult objects, starting with the provided result\n         *         and including all its child results.\n         */\n        Stream<DefaultModelBuilderResult> results(DefaultModelBuilderResult r) {\n            return Stream.concat(Stream.of(r), r.getChildren().stream().flatMap(this::results));\n        }\n\n        private void loadFromRoot(Path root, Path top) {\n            try (PhasingExecutor executor = createExecutor()) {\n                DefaultModelBuilderResult r = Objects.equals(top, root)\n                        ? result\n                        : new DefaultModelBuilderResult(request, ProblemCollector.create(session));\n                loadFilePom(executor, top, root, Set.of(), r);\n            }\n            if (result.getFileModel() == null && !Objects.equals(top, root)) {\n                logger.warn(\n                        \"The top project ({}) cannot be found in the reactor from root project ({}). \"\n                                + \"Make sure the root directory is correct (a missing '.mvn' directory in the root \"\n                                + \"project is the most common cause) and the project is correctly included \"\n                                + \"in the reactor (missing activated profiles, command line options, etc.). For this \"\n                                + \"build, the top project will be used as the root project.\",\n                        top,\n                        root);\n                mappedSources.clear();\n                loadFromRoot(top, top);\n            }\n        }\n\n        private void loadFilePom(\n                Executor executor, Path top, Path pom, Set<Path> parents, DefaultModelBuilderResult r) {\n            try {\n                Path pomDirectory = Files.isDirectory(pom) ? pom : pom.getParent();\n                ModelSource src = Sources.buildSource(pom);\n                Model model = derive(src, r).readFileModel();","sourceCodeStart":890,"sourceCodeEnd":926,"githubUrl":"https://github.com/apache/maven/blob/e4093d4e120eac99d6bdce5ba67cace2f3085c97/impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultModelBuilder.java#L890-L926","documentation":"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).","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"# before: no .mvn at the real root, detection climbs too far\nmy-repo/\n  .mvn/            # stray marker at some outer level\n  real-root/       # no .mvn here\n    pom.xml\n    sub/pom.xml\nmvn -f real-root/sub/pom.xml package   # -> warning, top used as root\n\n# after: mark the real root\nmkdir -p real-root/.mvn\nmvn -f real-root/sub/pom.xml package   # reactor found from real-root","handlingStrategy":"validation","validationCode":"// Embedding: ensure the requested POM is inside the detected root's reactor before building\nPath top = request.getSource().getPath().toAbsolutePath().normalize();\nPath root = session.getRootDirectory(); // throws if no root found\nif (!top.startsWith(root)) {\n    throw new IllegalStateException(top + \" is not under the detected root \" + root\n        + \"; check .mvn placement or pass the correct --root\");\n}\n// Shell check: warn when building a module whose aggregator does not list it\n# grep -q \"<module>sub</module>\" root/pom.xml || echo \"sub not in root reactor\"","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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"],"tags":["maven","maven4","root-directory","reactor","mvn-directory"],"backgroundTag":"maven-root-directory-detection","analyzedSha":"e4093d4e120eac99d6bdce5ba67cace2f3085c97","analyzedAt":"2026-08-21T22:58:24.034Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}