private ProjectRelocation retrieveRelocatedProject()

in compat/maven-compat/src/main/java/org/apache/maven/project/artifact/MavenMetadataSource.java [515:669]


    private ProjectRelocation retrieveRelocatedProject(Artifact artifact, MetadataResolutionRequest repositoryRequest)
            throws ArtifactMetadataRetrievalException {
        MavenProject project;

        Artifact pomArtifact;
        Artifact relocatedArtifact = null;
        boolean done = false;
        do {
            project = null;

            pomArtifact = artifactFactory.createProjectArtifact(
                    artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), artifact.getScope());

            if ("pom".equals(artifact.getType())) {
                pomArtifact.setFile(artifact.getFile());
            }

            if (Artifact.SCOPE_SYSTEM.equals(artifact.getScope())) {
                done = true;
            } else {
                try {
                    ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
                    configuration.setLocalRepository(repositoryRequest.getLocalRepository());
                    configuration.setRemoteRepositories(repositoryRequest.getRemoteRepositories());
                    configuration.setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL);
                    configuration.setProcessPlugins(false);
                    configuration.setRepositoryMerging(ProjectBuildingRequest.RepositoryMerging.REQUEST_DOMINANT);
                    MavenSession session = legacySupport.getSession();
                    if (session != null) {
                        configuration.setSystemProperties(session.getSystemProperties());
                        configuration.setUserProperties(session.getUserProperties());
                    } else {
                        configuration.setSystemProperties(getSystemProperties());
                        configuration.setUserProperties(new Properties());
                    }
                    configuration.setRepositorySession(legacySupport.getRepositorySession());

                    project = projectBuilder.build(pomArtifact, configuration).getProject();
                } catch (ProjectBuildingException e) {
                    ModelProblem missingParentPom = hasMissingParentPom(e);
                    if (missingParentPom != null) {
                        throw new ArtifactMetadataRetrievalException(
                                "Failed to process POM for " + artifact.getId() + ": " + missingParentPom.getMessage(),
                                missingParentPom.getException(),
                                artifact);
                    }

                    String message;

                    if (isMissingPom(e)) {
                        message = "Missing POM for " + artifact.getId();
                    } else if (isNonTransferablePom(e)) {
                        throw new ArtifactMetadataRetrievalException(
                                "Failed to retrieve POM for " + artifact.getId() + ": "
                                        + e.getCause().getMessage(),
                                e.getCause(),
                                artifact);
                    } else {
                        message = "Invalid POM for " + artifact.getId()
                                + ", transitive dependencies (if any) will not be available"
                                + ", enable verbose output (-X) for more details";
                    }

                    if (logger.isDebugEnabled()) {
                        message += ": " + e.getMessage();
                    }

                    logger.warn(message);
                }

                if (project != null) {
                    Relocation relocation = null;

                    DistributionManagement distMgmt = project.getModel().getDistributionManagement();
                    if (distMgmt != null) {
                        relocation = distMgmt.getRelocation();

                        artifact.setDownloadUrl(distMgmt.getDownloadUrl());
                        pomArtifact.setDownloadUrl(distMgmt.getDownloadUrl());
                    }

                    if (relocation != null) {
                        if (relocation.getGroupId() != null) {
                            artifact.setGroupId(relocation.getGroupId());
                            relocatedArtifact = artifact;
                            project.setGroupId(relocation.getGroupId());
                        }
                        if (relocation.getArtifactId() != null) {
                            artifact.setArtifactId(relocation.getArtifactId());
                            relocatedArtifact = artifact;
                            project.setArtifactId(relocation.getArtifactId());
                        }
                        if (relocation.getVersion() != null) {
                            // note: see MNG-3454. This causes a problem, but fixing it may break more.
                            artifact.setVersionRange(VersionRange.createFromVersion(relocation.getVersion()));
                            relocatedArtifact = artifact;
                            project.setVersion(relocation.getVersion());
                        }

                        if (artifact.getDependencyFilter() != null
                                && !artifact.getDependencyFilter().include(artifact)) {
                            return null;
                        }

                        // MNG-2861: the artifact data has changed. If the available versions where previously
                        // retrieved, we need to update it.
                        // TODO shouldn't the versions be merged across relocations?
                        List<ArtifactVersion> available = artifact.getAvailableVersions();
                        if (available != null && !available.isEmpty()) {
                            MetadataResolutionRequest metadataRequest =
                                    new DefaultMetadataResolutionRequest(repositoryRequest);
                            metadataRequest.setArtifact(artifact);
                            available = retrieveAvailableVersions(metadataRequest);
                            artifact.setAvailableVersions(available);
                        }

                        String message = "  this artifact has been relocated to " + artifact.getGroupId() + ":"
                                + artifact.getArtifactId() + ":" + artifact.getVersion() + ".";

                        if (relocation.getMessage() != null) {
                            message += "  " + relocation.getMessage();
                        }

                        if (artifact.getDependencyTrail() != null
                                && artifact.getDependencyTrail().size() == 1) {
                            logger.warn(
                                    "While downloading {}:{}:{}{}",
                                    pomArtifact.getGroupId(),
                                    pomArtifact.getArtifactId(),
                                    pomArtifact.getVersion(),
                                    message);
                        } else {
                            logger.debug(
                                    "While downloading {}:{}:{}{}",
                                    pomArtifact.getGroupId(),
                                    pomArtifact.getArtifactId(),
                                    pomArtifact.getVersion(),
                                    message);
                        }
                    } else {
                        done = true;
                    }
                } else {
                    done = true;
                }
            }
        } while (!done);

        ProjectRelocation rel = new ProjectRelocation();
        rel.project = project;
        rel.pomArtifact = pomArtifact;
        rel.relocatedArtifact = relocatedArtifact;

        return rel;
    }