private void importDependencyManagement()

in compat/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java [1108:1269]


    private void importDependencyManagement(
            Model model,
            ModelBuildingRequest request,
            DefaultModelProblemCollector problems,
            Collection<String> importIds) {
        DependencyManagement depMgmt = model.getDependencyManagement();

        if (depMgmt == null) {
            return;
        }

        String importing = model.getGroupId() + ':' + model.getArtifactId() + ':' + model.getVersion();

        importIds.add(importing);

        final WorkspaceModelResolver workspaceResolver = request.getWorkspaceModelResolver();
        final ModelResolver modelResolver = request.getModelResolver();

        ModelBuildingRequest importRequest = null;

        List<DependencyManagement> importMgmts = null;

        for (Iterator<Dependency> it = depMgmt.getDependencies().iterator(); it.hasNext(); ) {
            Dependency dependency = it.next();

            if (!"pom".equals(dependency.getType()) || !"import".equals(dependency.getScope())) {
                continue;
            }

            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;
            }

            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()));

                continue;
            }

            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));
                        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));
                        continue;
                    }

                    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());
                    }

                    importRequest.setModelSource(importSource);
                    importRequest.setModelResolver(modelResolver.newCopy());

                    final ModelBuildingResult importResult;
                    try {
                        importResult = build(importRequest, importIds);
                    } catch (ModelBuildingException e) {
                        problems.addAll(e.getProblems());
                        continue;
                    }

                    problems.addAll(importResult.getProblems());

                    importModel = importResult.getEffectiveModel();
                }

                importMgmt = importModel.getDependencyManagement();

                if (importMgmt == null) {
                    importMgmt = new DependencyManagement();
                }

                putCache(request.getModelCache(), groupId, artifactId, version, ModelCacheTag.IMPORT, importMgmt);
            }

            if (importMgmts == null) {
                importMgmts = new ArrayList<>();
            }

            importMgmts.add(importMgmt);
        }

        importIds.remove(importing);

        dependencyManagementImporter.importManagement(model, importMgmts, request, problems);
    }