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

cstamas 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 84ed8bea95 [MNG-8331] Sometimes versioned dependencies disappear 
(#1821)
84ed8bea95 is described below

commit 84ed8bea95c7ad45270a2a8e940f8ce3914b751d
Author: Jan-Jelle Kester <janje...@jjkester.nl>
AuthorDate: Sat Oct 19 20:01:42 2024 +0200

    [MNG-8331] Sometimes versioned dependencies disappear (#1821)
    
    Ensure all dependencies end up in the model. Avoid work when there are no 
dependencies. Extract method, re-introduce optimization to return original model
    
    ---
    
    https://issues.apache.org/jira/browse/MNG-8331
---
 .../internal/impl/model/DefaultModelBuilder.java   | 65 ++++++++++++----------
 1 file changed, 36 insertions(+), 29 deletions(-)

diff --git 
a/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/DefaultModelBuilder.java
 
b/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/DefaultModelBuilder.java
index 38266f3032..377e0be7ea 100644
--- 
a/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/DefaultModelBuilder.java
+++ 
b/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/DefaultModelBuilder.java
@@ -564,40 +564,47 @@ public class DefaultModelBuilder implements ModelBuilder {
         // Infer inner reactor dependencies version
         //
         Model transformFileToRaw(Model model) {
-            List<Dependency> newDeps = new ArrayList<>();
-            boolean modified = false;
+            if (model.getDependencies().isEmpty()) {
+                return model;
+            }
+            List<Dependency> newDeps = new 
ArrayList<>(model.getDependencies().size());
+            boolean changed = false;
             for (Dependency dep : model.getDependencies()) {
+                Dependency newDep = null;
                 if (dep.getVersion() == null) {
-                    Dependency.Builder depBuilder = null;
-                    Model depModel = getRawModel(model.getPomFile(), 
dep.getGroupId(), dep.getArtifactId());
-                    if (depModel != null) {
-                        String version = depModel.getVersion();
-                        InputLocation versionLocation = 
depModel.getLocation("version");
-                        if (version == null && depModel.getParent() != null) {
-                            version = depModel.getParent().getVersion();
-                            versionLocation = 
depModel.getParent().getLocation("version");
-                        }
-                        depBuilder = Dependency.newBuilder(dep);
-                        depBuilder.version(version).location("version", 
versionLocation);
-                        if (dep.getGroupId() == null) {
-                            String depGroupId = depModel.getGroupId();
-                            InputLocation groupIdLocation = 
depModel.getLocation("groupId");
-                            if (depGroupId == null && depModel.getParent() != 
null) {
-                                depGroupId = depModel.getParent().getGroupId();
-                                groupIdLocation = 
depModel.getParent().getLocation("groupId");
-                            }
-                            depBuilder.groupId(depGroupId).location("groupId", 
groupIdLocation);
-                        }
-                    }
-                    if (depBuilder != null) {
-                        newDeps.add(depBuilder.build());
-                        modified = true;
-                    } else {
-                        newDeps.add(dep);
+                    newDep = inferDependencyVersion(model, dep);
+                    if (newDep != null) {
+                        changed = true;
                     }
                 }
+                newDeps.add(newDep == null ? dep : newDep);
+            }
+            return changed ? model.withDependencies(newDeps) : model;
+        }
+
+        private Dependency inferDependencyVersion(Model model, Dependency dep) 
{
+            Model depModel = getRawModel(model.getPomFile(), dep.getGroupId(), 
dep.getArtifactId());
+            if (depModel == null) {
+                return null;
+            }
+            Dependency.Builder depBuilder = Dependency.newBuilder(dep);
+            String version = depModel.getVersion();
+            InputLocation versionLocation = depModel.getLocation("version");
+            if (version == null && depModel.getParent() != null) {
+                version = depModel.getParent().getVersion();
+                versionLocation = depModel.getParent().getLocation("version");
+            }
+            depBuilder.version(version).location("version", versionLocation);
+            if (dep.getGroupId() == null) {
+                String depGroupId = depModel.getGroupId();
+                InputLocation groupIdLocation = 
depModel.getLocation("groupId");
+                if (depGroupId == null && depModel.getParent() != null) {
+                    depGroupId = depModel.getParent().getGroupId();
+                    groupIdLocation = 
depModel.getParent().getLocation("groupId");
+                }
+                depBuilder.groupId(depGroupId).location("groupId", 
groupIdLocation);
             }
-            return modified ? model.withDependencies(newDeps) : model;
+            return depBuilder.build();
         }
 
         String replaceCiFriendlyVersion(Map<String, String> properties, String 
version) {

Reply via email to