This is an automated email from the ASF dual-hosted git repository.

gnodet pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven.git


The following commit(s) were added to refs/heads/master by this push:
     new 3036958397 Add LOAD_PROJECT request type for projects loaded from a 
POM file (#13669)
3036958397 is described below

commit 303695839709b80d888ae40fa49adc7edb476207
Author: Shreekar-cloud <[email protected]>
AuthorDate: Tue Nov 3 23:03:22 2026 +0530

    Add LOAD_PROJECT request type for projects loaded from a POM file (#13669)
    
    * Add LOAD_PROJECT request type for projects loaded from a POM file
    
    Fixes #13395.
    
    Co-authored-by: Abdul Hannan <[email protected]>
    
    * Simplify processPlugins handling in the LOAD_PROJECT regression test
    
    * Address review issues: improve test robustness and clarify 
isBuildRequest/isStrictRequest Javadoc
    
    - Add comment explaining why the child POM uses modelVersion 4.1.0 (the
      validation warning for redundant relativePath only fires for non-4.0.0
      model versions, so 4.0.0 would make the test a no-op)
    - Make the problem assertion more robust: match on Severity.WARNING and
      the stable field name 'parent.relativePath' in the message, rather than
      a substring of the mutable message body
    - Make isStrictRequest() an explicit positive check instead of negating
      isBuildRequest(), and add a Javadoc to isBuildRequest() clarifying that
      LOAD_PROJECT is included for project-level semantics (local parents,
      profile activation) but not strict validation
    
    ---------
    
    Co-authored-by: Abdul Hannan <[email protected]>
    Co-authored-by: Guillaume Nodet <[email protected]>
---
 .../maven/api/services/ModelBuilderRequest.java    |  9 +++-
 .../maven/project/DefaultProjectBuilder.java       | 23 ++++----
 .../apache/maven/project/ProjectBuilderTest.java   | 62 ++++++++++++++++++++++
 .../maven/impl/model/DefaultModelBuilder.java      | 31 +++++++++--
 4 files changed, 110 insertions(+), 15 deletions(-)

diff --git 
a/api/maven-api-core/src/main/java/org/apache/maven/api/services/ModelBuilderRequest.java
 
b/api/maven-api-core/src/main/java/org/apache/maven/api/services/ModelBuilderRequest.java
index 826ffe8fc4..496715802c 100644
--- 
a/api/maven-api-core/src/main/java/org/apache/maven/api/services/ModelBuilderRequest.java
+++ 
b/api/maven-api-core/src/main/java/org/apache/maven/api/services/ModelBuilderRequest.java
@@ -69,7 +69,14 @@ enum RequestType {
         /**
          * The request is for building a model from a dependency POM file from 
a downloaded artifact.
          */
-        CONSUMER_DEPENDENCY
+        CONSUMER_DEPENDENCY,
+        /**
+         * The request is for loading a project from a POM file on the 
filesystem when build-time strictness is not
+         * wanted, for example by IDEs. The model gets the same project 
treatment as {@link #BUILD_EFFECTIVE}, such as
+         * local parent lookup and profile activation, but it is parsed 
leniently and validated at the minimal level,
+         * like for the consumer request types.
+         */
+        LOAD_PROJECT
     }
 
     /**
diff --git 
a/impl/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java
 
b/impl/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java
index 1d46f65112..40ac523ee7 100644
--- 
a/impl/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java
+++ 
b/impl/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java
@@ -466,16 +466,19 @@ void prepare() throws ProjectBuildingException {
                             && 
source.getLocation().endsWith("/org/apache/maven/project/standalone.xml");
 
                     ModelBuilderRequest.ModelBuilderRequestBuilder builder = 
getModelBuildingRequest();
-                    ModelBuilderRequest.RequestType type = reactorMember
-                                    || isStandalone
-                                    || (pomFile != null
-                                            && request.isProcessPlugins()
-                                            && request.getValidationLevel()
-                                                    == 
ModelBuildingRequest.VALIDATION_LEVEL_STRICT)
-                            ? ModelBuilderRequest.RequestType.BUILD_EFFECTIVE
-                            : (parent
-                                    ? 
ModelBuilderRequest.RequestType.CONSUMER_PARENT
-                                    : 
ModelBuilderRequest.RequestType.CONSUMER_DEPENDENCY);
+                    ModelBuilderRequest.RequestType type;
+                    if (reactorMember || isStandalone) {
+                        type = ModelBuilderRequest.RequestType.BUILD_EFFECTIVE;
+                    } else if (pomFile != null && request.isProcessPlugins()) {
+                        // a project loaded from its POM file: only a STRICT 
request asks for the strictness of a build
+                        type = request.getValidationLevel() == 
ModelBuildingRequest.VALIDATION_LEVEL_STRICT
+                                ? 
ModelBuilderRequest.RequestType.BUILD_EFFECTIVE
+                                : ModelBuilderRequest.RequestType.LOAD_PROJECT;
+                    } else {
+                        type = parent
+                                ? 
ModelBuilderRequest.RequestType.CONSUMER_PARENT
+                                : 
ModelBuilderRequest.RequestType.CONSUMER_DEPENDENCY;
+                    }
                     MavenProject theProject = project;
                     ModelBuilderRequest modelRequest = builder.source(source)
                             .requestType(type)
diff --git 
a/impl/maven-core/src/test/java/org/apache/maven/project/ProjectBuilderTest.java
 
b/impl/maven-core/src/test/java/org/apache/maven/project/ProjectBuilderTest.java
index fc0b19024f..36646f9a26 100644
--- 
a/impl/maven-core/src/test/java/org/apache/maven/project/ProjectBuilderTest.java
+++ 
b/impl/maven-core/src/test/java/org/apache/maven/project/ProjectBuilderTest.java
@@ -283,6 +283,68 @@ void testFailedParentStillInitializesChild(@TempDir Path 
tempDir) throws Excepti
         }
     }
 
+    @Test
+    void testLocalParentResolutionWithValidationLevels(@TempDir Path tempDir) 
throws Exception {
+        Path parentPom = tempDir.resolve("pom.xml");
+        Files.writeString(parentPom, """
+            <project>
+              <modelVersion>4.0.0</modelVersion>
+              <groupId>test</groupId>
+              <artifactId>parent</artifactId>
+              <version>1</version>
+              <packaging>pom</packaging>
+            </project>
+            """);
+
+        // The child POM intentionally uses modelVersion 4.1.0: the validation 
warning
+        // "only specify relativePath or groupId/artifactId" is only emitted 
for non-4.0.0 model
+        // versions when the parent declares both a relativePath and explicit 
groupId/artifactId.
+        Path childPom = 
Files.createDirectory(tempDir.resolve("child")).resolve("pom.xml");
+        Files.writeString(childPom, """
+            <project xmlns="http://maven.apache.org/POM/4.1.0";>
+              <modelVersion>4.1.0</modelVersion>
+              <parent>
+                <groupId>test</groupId>
+                <artifactId>parent</artifactId>
+                <version>1</version>
+                <relativePath>../pom.xml</relativePath>
+              </parent>
+              <artifactId>child</artifactId>
+            </project>
+            """);
+
+        MavenSession session = createMavenSession(null);
+        session.getRequest().setRootDirectory(tempDir);
+        org.apache.maven.project.ProjectBuilder builder =
+                
getContainer().lookup(org.apache.maven.project.ProjectBuilder.class);
+
+        for (int validationLevel :
+                List.of(ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL, 
ModelBuildingRequest.VALIDATION_LEVEL_STRICT)) {
+            ProjectBuildingRequest request = new 
DefaultProjectBuildingRequest();
+            request.setRepositorySession(session.getRepositorySession());
+            request.setValidationLevel(validationLevel);
+            // processPlugins=false would be a consumer request, which never 
looks up a local parent
+            request.setProcessPlugins(true);
+            ProjectBuildingResult result = builder.build(childPom.toFile(), 
request);
+            MavenProject child = result.getProject();
+
+            assertNotNull(child.getParentArtifact());
+            assertEquals("parent", child.getParentArtifact().getArtifactId());
+            assertEquals(parentPom.toFile(), child.getParentFile());
+
+            // Match on severity + field name rather than the full message 
body to stay resilient to wording changes.
+            // The validator emits: "'parent.relativePath' only specify 
relativePath or groupId/artifactId …"
+            boolean hasRelativePathWarning = result.getProblems().stream()
+                    .anyMatch(problem -> problem.getSeverity() == 
ModelProblem.Severity.WARNING
+                            && 
problem.getMessage().contains("'parent.relativePath'"));
+
+            assertEquals(
+                    validationLevel == 
ModelBuildingRequest.VALIDATION_LEVEL_STRICT,
+                    hasRelativePathWarning,
+                    "Unexpected validation result for level " + 
validationLevel);
+        }
+    }
+
     private void assertParentHierarchy(Path tempDir, int depth) throws 
Exception {
         for (int i = 0; i < depth; i++) {
             Path directory = Files.createDirectory(tempDir.resolve("parent-" + 
i));
diff --git 
a/impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultModelBuilder.java
 
b/impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultModelBuilder.java
index 40c22740b7..f6e4d56cb5 100644
--- 
a/impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultModelBuilder.java
+++ 
b/impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultModelBuilder.java
@@ -1275,7 +1275,9 @@ void buildEffectiveModel(Collection<String> importIds) 
throws ModelBuilderExcept
             modelValidator.validateEffectiveModel(
                     session,
                     resultModel,
-                    isBuildRequest() ? ModelValidator.VALIDATION_LEVEL_STRICT 
: ModelValidator.VALIDATION_LEVEL_MINIMAL,
+                    isStrictRequest()
+                            ? ModelValidator.VALIDATION_LEVEL_STRICT
+                            : ModelValidator.VALIDATION_LEVEL_MINIMAL,
                     this);
 
             if (hasErrors()) {
@@ -1834,7 +1836,7 @@ Model doReadFileModel(Set<Path> activeModelReads) throws 
ModelBuilderException {
             boolean trackRead = normalizedPath != null && 
activeModelReads.add(normalizedPath);
             try {
                 try {
-                    boolean strict = isBuildRequest();
+                    boolean strict = isStrictRequest();
                     try {
                         rootDirectory = 
request.getSession().getRootDirectory();
                         rootDirectoryFromSession = true;
@@ -2056,7 +2058,7 @@ Model doReadFileModel(Set<Path> activeModelReads) throws 
ModelBuilderException {
                 modelValidator.validateFileModel(
                         session,
                         model,
-                        isBuildRequest()
+                        isStrictRequest()
                                 ? ModelValidator.VALIDATION_LEVEL_STRICT
                                 : ModelValidator.VALIDATION_LEVEL_MINIMAL,
                         this);
@@ -2187,7 +2189,9 @@ private Model doReadRawModel() throws 
ModelBuilderException {
             modelValidator.validateRawModel(
                     session,
                     rawModel,
-                    isBuildRequest() ? ModelValidator.VALIDATION_LEVEL_STRICT 
: ModelValidator.VALIDATION_LEVEL_MINIMAL,
+                    isStrictRequest()
+                            ? ModelValidator.VALIDATION_LEVEL_STRICT
+                            : ModelValidator.VALIDATION_LEVEL_MINIMAL,
                     this);
 
             if (hasFatalErrors()) {
@@ -2986,7 +2990,26 @@ private <T> T cache(Source source, String tag, 
Supplier<T> supplier) throws Mode
             }
         }
 
+        /**
+         * Whether the request uses project-level semantics (local parent 
lookup, profile activation, etc.).
+         * This includes both strict build requests and the lenient {@link 
ModelBuilderRequest.RequestType#LOAD_PROJECT}
+         * type, which behaves like a build request for resolution purposes 
but does not enforce strict validation.
+         *
+         * @see #isStrictRequest() for requests that also require strict 
parsing and validation
+         */
         boolean isBuildRequest() {
+            return request.getRequestType() == 
ModelBuilderRequest.RequestType.BUILD_PROJECT
+                    || request.getRequestType() == 
ModelBuilderRequest.RequestType.BUILD_EFFECTIVE
+                    || request.getRequestType() == 
ModelBuilderRequest.RequestType.BUILD_CONSUMER
+                    || request.getRequestType() == 
ModelBuilderRequest.RequestType.LOAD_PROJECT;
+        }
+
+        /**
+         * Whether the model has to be parsed and validated strictly. This is 
the case for the build requests, but not
+         * for {@link ModelBuilderRequest.RequestType#LOAD_PROJECT}, which is 
a project only in terms of semantics
+         * (local parents, profile activation) and keeps the lenient defaults 
of the consumer requests.
+         */
+        boolean isStrictRequest() {
             return request.getRequestType() == 
ModelBuilderRequest.RequestType.BUILD_PROJECT
                     || request.getRequestType() == 
ModelBuilderRequest.RequestType.BUILD_EFFECTIVE
                     || request.getRequestType() == 
ModelBuilderRequest.RequestType.BUILD_CONSUMER;

Reply via email to