goutamadwant commented on code in PR #13081:
URL: https://github.com/apache/maven/pull/13081#discussion_r3976172783


##########
compat/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java:
##########
@@ -1187,136 +1182,210 @@ private void importDependencyManagement(
 
             it.remove();
 
-            String groupId = dependency.getGroupId();
-            String artifactId = dependency.getArtifactId();
-            String version = dependency.getVersion();
-
-            if (groupId == null || groupId.length() <= 0) {
-                problems.add(new ModelProblemCollectorRequest(Severity.ERROR, 
Version.BASE)
-                        
.setMessage("'dependencyManagement.dependencies.dependency.groupId' for "
-                                + dependency.getManagementKey() + " is 
missing.")
-                        .setLocation(dependency.getLocation("")));
-                continue;
-            }
-            if (artifactId == null || artifactId.length() <= 0) {
-                problems.add(new ModelProblemCollectorRequest(Severity.ERROR, 
Version.BASE)
-                        
.setMessage("'dependencyManagement.dependencies.dependency.artifactId' for "
-                                + dependency.getManagementKey() + " is 
missing.")
-                        .setLocation(dependency.getLocation("")));
-                continue;
-            }
-            if (version == null || version.length() <= 0) {
-                problems.add(new ModelProblemCollectorRequest(Severity.ERROR, 
Version.BASE)
-                        
.setMessage("'dependencyManagement.dependencies.dependency.version' for "
-                                + dependency.getManagementKey() + " is 
missing.")
-                        .setLocation(dependency.getLocation("")));
-                continue;
+            DependencyManagement importMgmt = 
loadDependencyManagement(dependency, model, request, problems, importIds);
+            if (importMgmt != null) {
+                if (importMgmts == null) {
+                    importMgmts = new ArrayList<>();
+                }
+                importMgmts.add(importMgmt);
             }
+        }
 
-            String imported = groupId + ':' + artifactId + ':' + version;
+        importIds.remove(importing);
 
-            if (importIds.contains(imported)) {
-                StringBuilder message =
-                        new StringBuilder("The dependencies of type=pom and 
with scope=import form a cycle: ");
-                for (String modelId : importIds) {
-                    message.append(modelId);
-                    message.append(" -> ");
-                }
-                message.append(imported);
-                problems.add(
-                        new ModelProblemCollectorRequest(Severity.ERROR, 
Version.BASE).setMessage(message.toString()));
+        dependencyManagementImporter.importManagement(model, importMgmts, 
request, problems);
+    }
 
-                continue;
+    @SuppressWarnings("checkstyle:methodlength")
+    private DependencyManagement loadDependencyManagement(
+            Dependency dependency,
+            Model model,
+            ModelBuildingRequest request,
+            DefaultModelProblemCollector problems,
+            Collection<String> importIds) {
+        final WorkspaceModelResolver workspaceResolver = 
request.getWorkspaceModelResolver();
+        final ModelResolver modelResolver = request.getModelResolver();
+
+        String groupId = dependency.getGroupId();
+        String artifactId = dependency.getArtifactId();
+        String version = dependency.getVersion();
+
+        if (groupId == null || groupId.length() <= 0) {
+            problems.add(new ModelProblemCollectorRequest(Severity.ERROR, 
Version.BASE)
+                    
.setMessage("'dependencyManagement.dependencies.dependency.groupId' for "
+                            + dependency.getManagementKey() + " is missing.")
+                    .setLocation(dependency.getLocation("")));
+            return null;
+        }
+        if (artifactId == null || artifactId.length() <= 0) {
+            problems.add(new ModelProblemCollectorRequest(Severity.ERROR, 
Version.BASE)
+                    
.setMessage("'dependencyManagement.dependencies.dependency.artifactId' for "
+                            + dependency.getManagementKey() + " is missing.")
+                    .setLocation(dependency.getLocation("")));
+            return null;
+        }
+        if (version == null || version.length() <= 0) {
+            problems.add(new ModelProblemCollectorRequest(Severity.ERROR, 
Version.BASE)
+                    
.setMessage("'dependencyManagement.dependencies.dependency.version' for "
+                            + dependency.getManagementKey() + " is missing.")
+                    .setLocation(dependency.getLocation("")));
+            return null;
+        }
+
+        String imported = groupId + ':' + artifactId + ':' + version;
+
+        if (importIds.contains(imported)) {
+            StringBuilder message =
+                    new StringBuilder("The dependencies of type=pom and with 
scope=import form a cycle: ");
+            for (String modelId : importIds) {
+                message.append(modelId);
+                message.append(" -> ");
             }
+            message.append(imported);
+            problems.add(new ModelProblemCollectorRequest(Severity.ERROR, 
Version.BASE).setMessage(message.toString()));
 
-            DependencyManagement importMgmt =
-                    getCache(request.getModelCache(), groupId, artifactId, 
version, ModelCacheTag.IMPORT);
+            return null;
+        }
 
-            if (importMgmt == null) {
-                if (workspaceResolver == null && modelResolver == null) {
-                    throw new NullPointerException(String.format(
-                            "request.workspaceModelResolver and 
request.modelResolver cannot be null"
-                                    + " (parent POM %s and POM %s)",
-                            ModelProblemUtils.toId(groupId, artifactId, 
version),
-                            ModelProblemUtils.toSourceHint(model)));
+        DependencyManagement importMgmt =
+                getCache(request.getModelCache(), groupId, artifactId, 
version, ModelCacheTag.IMPORT);
+
+        if (importMgmt == null) {
+            if (workspaceResolver == null && modelResolver == null) {
+                throw new NullPointerException(String.format(
+                        "request.workspaceModelResolver and 
request.modelResolver cannot be null"
+                                + " (parent POM %s and POM %s)",
+                        ModelProblemUtils.toId(groupId, artifactId, version), 
ModelProblemUtils.toSourceHint(model)));
+            }
+
+            Model importModel = null;
+            if (workspaceResolver != null) {
+                try {
+                    importModel = 
workspaceResolver.resolveEffectiveModel(groupId, artifactId, version);
+                } catch (UnresolvableModelException e) {
+                    problems.add(new 
ModelProblemCollectorRequest(Severity.FATAL, Version.BASE)
+                            .setMessage(e.getMessage())
+                            .setException(e));
+                    return null;
                 }
+            }
 
-                Model importModel = null;
-                if (workspaceResolver != null) {
-                    try {
-                        importModel = 
workspaceResolver.resolveEffectiveModel(groupId, artifactId, version);
-                    } catch (UnresolvableModelException e) {
-                        problems.add(new 
ModelProblemCollectorRequest(Severity.FATAL, Version.BASE)
-                                .setMessage(e.getMessage())
-                                .setException(e));
-                        continue;
+            // no workspace resolver or workspace resolver returned null (i.e. 
model not in workspace)
+            if (importModel == null) {
+                final ModelSource importSource;
+                try {
+                    importSource = modelResolver.resolveModel(groupId, 
artifactId, version);
+                } catch (UnresolvableModelException e) {
+                    StringBuilder buffer = new StringBuilder(256);
+                    buffer.append("Non-resolvable import POM");
+                    if (!containsCoordinates(e.getMessage(), groupId, 
artifactId, version)) {
+                        buffer.append(' 
').append(ModelProblemUtils.toId(groupId, artifactId, version));
                     }
+                    buffer.append(": ").append(e.getMessage());
+
+                    problems.add(new 
ModelProblemCollectorRequest(Severity.ERROR, Version.BASE)
+                            .setMessage(buffer.toString())
+                            .setLocation(dependency.getLocation(""))
+                            .setException(e));
+                    return null;
                 }
 
-                // no workspace resolver or workspace resolver returned null 
(i.e. model not in workspace)
-                if (importModel == null) {
-                    final ModelSource importSource;
-                    try {
-                        importSource = modelResolver.resolveModel(groupId, 
artifactId, version);
-                    } catch (UnresolvableModelException e) {
-                        StringBuilder buffer = new StringBuilder(256);
-                        buffer.append("Non-resolvable import POM");
-                        if (!containsCoordinates(e.getMessage(), groupId, 
artifactId, version)) {
-                            buffer.append(' 
').append(ModelProblemUtils.toId(groupId, artifactId, version));
-                        }
-                        buffer.append(": ").append(e.getMessage());
+                ModelBuildingRequest importRequest = new 
DefaultModelBuildingRequest();
+                
importRequest.setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL);
+                importRequest.setModelCache(request.getModelCache());
+                
importRequest.setSystemProperties(request.getSystemProperties());
+                importRequest.setUserProperties(request.getUserProperties());
+                
importRequest.setLocationTracking(request.isLocationTracking());
 
-                        problems.add(new 
ModelProblemCollectorRequest(Severity.ERROR, Version.BASE)
-                                .setMessage(buffer.toString())
-                                .setLocation(dependency.getLocation(""))
-                                .setException(e));
-                        continue;
-                    }
+                importRequest.setModelSource(importSource);
+                importRequest.setModelResolver(modelResolver.newCopy());
 
-                    if (importRequest == null) {
-                        importRequest = new DefaultModelBuildingRequest();
-                        
importRequest.setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL);
-                        importRequest.setModelCache(request.getModelCache());
-                        
importRequest.setSystemProperties(request.getSystemProperties());
-                        
importRequest.setUserProperties(request.getUserProperties());
-                        
importRequest.setLocationTracking(request.isLocationTracking());
-                    }
+                final ModelBuildingResult importResult;

Review Comment:
   Sure. Moved the declaration and initialization of importResult inside the 
try block, together with the result handling. Let me know if this is good now 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to