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 e6d038ac07 [MNG-8252] Fully infer the parent coordinates if the 
location points to a valid model (#1706)
e6d038ac07 is described below

commit e6d038ac0717bfe72a707d9432ae6803805093f7
Author: Guillaume Nodet <[email protected]>
AuthorDate: Wed Sep 11 18:30:15 2024 +0200

    [MNG-8252] Fully infer the parent coordinates if the location points to a 
valid model (#1706)
---
 .../internal/impl/model/BuildModelTransformer.java | 32 +++++++-----
 .../internal/impl/model/DefaultModelValidator.java | 60 +++++++++++-----------
 2 files changed, 48 insertions(+), 44 deletions(-)

diff --git 
a/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/BuildModelTransformer.java
 
b/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/BuildModelTransformer.java
index c1ff4d8efa..d8c622380d 100644
--- 
a/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/BuildModelTransformer.java
+++ 
b/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/BuildModelTransformer.java
@@ -24,7 +24,6 @@ import java.nio.file.Paths;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
-import java.util.Objects;
 import java.util.Optional;
 
 import org.apache.maven.api.di.Named;
@@ -59,18 +58,34 @@ public class BuildModelTransformer implements 
ModelTransformer {
     void handleParent(ModelTransformerContext context, Model model, Path 
pomFile, Model.Builder builder) {
         Parent parent = model.getParent();
         if (parent != null) {
+            String groupId = parent.getGroupId();
+            String artifactId = parent.getArtifactId();
             String version = parent.getVersion();
             String path = 
Optional.ofNullable(parent.getRelativePath()).orElse("..");
             if (version == null && !path.isEmpty()) {
                 Optional<RelativeProject> resolvedParent = resolveRelativePath(
                         pomFile, context, Paths.get(path), 
parent.getGroupId(), parent.getArtifactId());
                 if (resolvedParent.isPresent()) {
-                    version = resolvedParent.get().getVersion();
+                    RelativeProject project = resolvedParent.get();
+                    if (groupId == null
+                            || groupId.equals(project.getGroupId()) && 
artifactId == null
+                            || artifactId.equals(project.getArtifactId())) {
+                        groupId = project.getGroupId();
+                        artifactId = project.getArtifactId();
+                        version = resolvedParent.get().getVersion();
+                    }
                 }
             }
+
             // CI Friendly version for parent
             String modVersion = replaceCiFriendlyVersion(context, version);
-            builder.parent(parent.withVersion(modVersion));
+
+            // Update parent
+            builder.parent(parent.with()
+                    .groupId(groupId)
+                    .artifactId(artifactId)
+                    .version(modVersion)
+                    .build());
         }
     }
 
@@ -131,17 +146,8 @@ public class BuildModelTransformer implements 
ModelTransformer {
             return Optional.empty();
         }
 
-        Optional<RelativeProject> mappedProject = 
Optional.ofNullable(context.getRawModel(pomFile, pomPath.normalize()))
+        return Optional.ofNullable(context.getRawModel(pomFile, 
pomPath.normalize()))
                 .map(BuildModelTransformer::toRelativeProject);
-
-        if (mappedProject.isPresent()) {
-            RelativeProject project = mappedProject.get();
-
-            if (Objects.equals(groupId, project.getGroupId()) && 
Objects.equals(artifactId, project.getArtifactId())) {
-                return mappedProject;
-            }
-        }
-        return Optional.empty();
     }
 
     private static RelativeProject toRelativeProject(final Model m) {
diff --git 
a/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/DefaultModelValidator.java
 
b/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/DefaultModelValidator.java
index cd757439a2..07c26da3c9 100644
--- 
a/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/DefaultModelValidator.java
+++ 
b/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/DefaultModelValidator.java
@@ -301,37 +301,6 @@ public class DefaultModelValidator implements 
ModelValidator {
     public void validateFileModel(Model m, ModelBuilderRequest request, 
ModelProblemCollector problems) {
 
         Parent parent = m.getParent();
-        if (parent != null) {
-            validateStringNotEmpty(
-                    "parent.groupId", problems, Severity.FATAL, Version.BASE, 
parent.getGroupId(), parent);
-
-            validateStringNotEmpty(
-                    "parent.artifactId", problems, Severity.FATAL, 
Version.BASE, parent.getArtifactId(), parent);
-
-            if (equals(parent.getGroupId(), m.getGroupId()) && 
equals(parent.getArtifactId(), m.getArtifactId())) {
-                addViolation(
-                        problems,
-                        Severity.FATAL,
-                        Version.BASE,
-                        "parent.artifactId",
-                        null,
-                        "must be changed"
-                                + ", the parent element cannot have the same 
groupId:artifactId as the project.",
-                        parent);
-            }
-
-            if (equals("LATEST", parent.getVersion()) || equals("RELEASE", 
parent.getVersion())) {
-                addViolation(
-                        problems,
-                        Severity.WARNING,
-                        Version.BASE,
-                        "parent.version",
-                        null,
-                        "is either LATEST or RELEASE (both of them are being 
deprecated)",
-                        parent);
-            }
-        }
-
         if (request.getValidationLevel() == 
ModelBuilderRequest.VALIDATION_LEVEL_MINIMAL) {
             // profiles: they are essential for proper model building (may 
contribute profiles, dependencies...)
             HashSet<String> minProfileIds = new HashSet<>();
@@ -552,8 +521,37 @@ public class DefaultModelValidator implements 
ModelValidator {
         Parent parent = m.getParent();
 
         if (parent != null) {
+            validateStringNotEmpty(
+                    "parent.groupId", problems, Severity.FATAL, Version.BASE, 
parent.getGroupId(), parent);
+
+            validateStringNotEmpty(
+                    "parent.artifactId", problems, Severity.FATAL, 
Version.BASE, parent.getArtifactId(), parent);
+
             validateStringNotEmpty(
                     "parent.version", problems, Severity.FATAL, Version.BASE, 
parent.getVersion(), parent);
+
+            if (equals(parent.getGroupId(), m.getGroupId()) && 
equals(parent.getArtifactId(), m.getArtifactId())) {
+                addViolation(
+                        problems,
+                        Severity.FATAL,
+                        Version.BASE,
+                        "parent.artifactId",
+                        null,
+                        "must be changed"
+                                + ", the parent element cannot have the same 
groupId:artifactId as the project.",
+                        parent);
+            }
+
+            if (equals("LATEST", parent.getVersion()) || equals("RELEASE", 
parent.getVersion())) {
+                addViolation(
+                        problems,
+                        Severity.WARNING,
+                        Version.BASE,
+                        "parent.version",
+                        null,
+                        "is either LATEST or RELEASE (both of them are being 
deprecated)",
+                        parent);
+            }
         }
     }
 

Reply via email to