View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.project.artifact;
20  
21  import javax.inject.Inject;
22  import javax.inject.Named;
23  import javax.inject.Singleton;
24  
25  import java.io.File;
26  import java.util.ArrayList;
27  import java.util.Arrays;
28  import java.util.Collection;
29  import java.util.Collections;
30  import java.util.HashMap;
31  import java.util.LinkedHashMap;
32  import java.util.LinkedHashSet;
33  import java.util.List;
34  import java.util.Map;
35  import java.util.Properties;
36  import java.util.Set;
37  
38  import org.apache.maven.RepositoryUtils;
39  import org.apache.maven.artifact.Artifact;
40  import org.apache.maven.artifact.InvalidRepositoryException;
41  import org.apache.maven.artifact.factory.ArtifactFactory;
42  import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException;
43  import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
44  import org.apache.maven.artifact.metadata.ResolutionGroup;
45  import org.apache.maven.artifact.repository.ArtifactRepository;
46  import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata;
47  import org.apache.maven.artifact.repository.metadata.Metadata;
48  import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
49  import org.apache.maven.artifact.repository.metadata.RepositoryMetadataManager;
50  import org.apache.maven.artifact.repository.metadata.RepositoryMetadataResolutionException;
51  import org.apache.maven.artifact.resolver.ArtifactResolutionException;
52  import org.apache.maven.artifact.resolver.MultipleArtifactsNotFoundException;
53  import org.apache.maven.artifact.resolver.filter.AndArtifactFilter;
54  import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
55  import org.apache.maven.artifact.resolver.filter.ExclusionArtifactFilter;
56  import org.apache.maven.artifact.versioning.ArtifactVersion;
57  import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
58  import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
59  import org.apache.maven.artifact.versioning.VersionRange;
60  import org.apache.maven.bridge.MavenRepositorySystem;
61  import org.apache.maven.execution.MavenSession;
62  import org.apache.maven.model.Dependency;
63  import org.apache.maven.model.DependencyManagement;
64  import org.apache.maven.model.DistributionManagement;
65  import org.apache.maven.model.Model;
66  import org.apache.maven.model.Relocation;
67  import org.apache.maven.model.Repository;
68  import org.apache.maven.model.building.ModelBuildingException;
69  import org.apache.maven.model.building.ModelBuildingRequest;
70  import org.apache.maven.model.building.ModelProblem;
71  import org.apache.maven.model.resolution.UnresolvableModelException;
72  import org.apache.maven.plugin.LegacySupport;
73  import org.apache.maven.project.DefaultProjectBuildingRequest;
74  import org.apache.maven.project.MavenProject;
75  import org.apache.maven.project.ProjectBuilder;
76  import org.apache.maven.project.ProjectBuildingException;
77  import org.apache.maven.project.ProjectBuildingRequest;
78  import org.apache.maven.properties.internal.EnvironmentUtils;
79  import org.apache.maven.properties.internal.SystemProperties;
80  import org.apache.maven.repository.internal.MavenWorkspaceReader;
81  import org.apache.maven.repository.legacy.metadata.DefaultMetadataResolutionRequest;
82  import org.apache.maven.repository.legacy.metadata.MetadataResolutionRequest;
83  import org.eclipse.aether.RepositorySystemSession;
84  import org.eclipse.aether.repository.RepositoryPolicy;
85  import org.eclipse.aether.repository.WorkspaceReader;
86  import org.eclipse.aether.transfer.ArtifactNotFoundException;
87  import org.slf4j.Logger;
88  import org.slf4j.LoggerFactory;
89  
90  /**
91   * @author Jason van Zyl
92   */
93  @Named("maven")
94  @Singleton
95  public class MavenMetadataSource implements ArtifactMetadataSource {
96      private final Logger logger = LoggerFactory.getLogger(getClass());
97      private final RepositoryMetadataManager repositoryMetadataManager;
98      private final ArtifactFactory artifactFactory;
99      private final ProjectBuilder projectBuilder;
100     private final MavenMetadataCache cache;
101     private final LegacySupport legacySupport;
102 
103     private MavenRepositorySystem mavenRepositorySystem;
104 
105     @Inject
106     public MavenMetadataSource(
107             RepositoryMetadataManager repositoryMetadataManager,
108             ArtifactFactory artifactFactory,
109             ProjectBuilder projectBuilder,
110             MavenMetadataCache cache,
111             LegacySupport legacySupport,
112             MavenRepositorySystem mavenRepositorySystem) {
113         this.repositoryMetadataManager = repositoryMetadataManager;
114         this.artifactFactory = artifactFactory;
115         this.projectBuilder = projectBuilder;
116         this.cache = cache;
117         this.legacySupport = legacySupport;
118         this.mavenRepositorySystem = mavenRepositorySystem;
119     }
120 
121     private void injectSession(MetadataResolutionRequest request) {
122         RepositorySystemSession session = legacySupport.getRepositorySession();
123 
124         if (session != null) {
125             request.setOffline(session.isOffline());
126             request.setForceUpdate(RepositoryPolicy.UPDATE_POLICY_ALWAYS.equals(session.getUpdatePolicy()));
127         }
128     }
129 
130     @Override
131     public ResolutionGroup retrieve(
132             Artifact artifact, ArtifactRepository localRepository, List<ArtifactRepository> remoteRepositories)
133             throws ArtifactMetadataRetrievalException {
134         return retrieve(artifact, localRepository, remoteRepositories, false);
135     }
136 
137     public ResolutionGroup retrieve(
138             Artifact artifact,
139             ArtifactRepository localRepository,
140             List<ArtifactRepository> remoteRepositories,
141             boolean resolveManagedVersions)
142             throws ArtifactMetadataRetrievalException {
143         MetadataResolutionRequest request = new DefaultMetadataResolutionRequest();
144         injectSession(request);
145         request.setArtifact(artifact);
146         request.setLocalRepository(localRepository);
147         request.setRemoteRepositories(remoteRepositories);
148         request.setResolveManagedVersions(resolveManagedVersions);
149         return retrieve(request);
150     }
151 
152     @Override
153     public ResolutionGroup retrieve(MetadataResolutionRequest request) throws ArtifactMetadataRetrievalException {
154         Artifact artifact = request.getArtifact();
155 
156         //
157         // If we have a system scoped artifact then we do not want any searching in local or remote repositories
158         // and we want artifact resolution to only return the system scoped artifact itself.
159         //
160         if (artifact.getScope() != null && artifact.getScope().equals(Artifact.SCOPE_SYSTEM)) {
161             return new ResolutionGroup(null, null, null);
162         }
163 
164         ResolutionGroup cached = cache.get(
165                 artifact,
166                 request.isResolveManagedVersions(),
167                 request.getLocalRepository(),
168                 request.getRemoteRepositories());
169 
170         if (cached != null
171                 // if the POM has no file, we cached a missing artifact, only return the cached data if no update forced
172                 && (!request.isForceUpdate() || hasFile(cached.getPomArtifact()))) {
173             return cached;
174         }
175 
176         List<Dependency> dependencies;
177 
178         List<Dependency> managedDependencies = null;
179 
180         List<ArtifactRepository> pomRepositories = null;
181 
182         Artifact pomArtifact;
183 
184         Artifact relocatedArtifact = null;
185 
186         // TODO hack: don't rebuild model if it was already loaded during reactor resolution
187         RepositorySystemSession repositorySession = legacySupport.getRepositorySession();
188         final WorkspaceReader workspace = repositorySession.getWorkspaceReader();
189         Model model;
190         if (workspace instanceof MavenWorkspaceReader) {
191             model = ((MavenWorkspaceReader) workspace).findModel(RepositoryUtils.toArtifact(artifact));
192         } else {
193             model = null;
194         }
195 
196         if (model != null) {
197             pomArtifact = artifact;
198             dependencies = model.getDependencies();
199             DependencyManagement dependencyManagement = model.getDependencyManagement();
200             managedDependencies = dependencyManagement == null ? null : dependencyManagement.getDependencies();
201             MavenSession session = legacySupport.getSession();
202             if (session != null) {
203                 pomRepositories = session.getProjects().stream()
204                         .filter(p -> artifact.equals(p.getArtifact()))
205                         .map(MavenProject::getRemoteArtifactRepositories)
206                         .findFirst()
207                         .orElseGet(() -> getRepositoriesFromModel(repositorySession, model));
208             } else {
209                 pomRepositories = new ArrayList<>();
210             }
211         } else if (artifact instanceof ArtifactWithDependencies) {
212             pomArtifact = artifact;
213 
214             dependencies = ((ArtifactWithDependencies) artifact).getDependencies();
215 
216             managedDependencies = ((ArtifactWithDependencies) artifact).getManagedDependencies();
217         } else {
218             ProjectRelocation rel = retrieveRelocatedProject(artifact, request);
219 
220             if (rel == null) {
221                 return null;
222             }
223 
224             pomArtifact = rel.pomArtifact;
225 
226             relocatedArtifact = rel.relocatedArtifact;
227 
228             if (rel.project == null) {
229                 // When this happens we have a Maven 1.x POM, or some invalid POM.
230                 // It should have never found its way into Maven 2.x repository but it did.
231                 dependencies = Collections.emptyList();
232             } else {
233                 dependencies = rel.project.getModel().getDependencies();
234 
235                 DependencyManagement depMgmt = rel.project.getModel().getDependencyManagement();
236                 managedDependencies = (depMgmt != null) ? depMgmt.getDependencies() : null;
237 
238                 pomRepositories = rel.project.getRemoteArtifactRepositories();
239             }
240         }
241 
242         Set<Artifact> artifacts = Collections.emptySet();
243 
244         if (!artifact.getArtifactHandler().isIncludesDependencies()) {
245             artifacts = new LinkedHashSet<>();
246 
247             for (Dependency dependency : dependencies) {
248                 Artifact dependencyArtifact = createDependencyArtifact(dependency, artifact, pomArtifact);
249 
250                 if (dependencyArtifact != null) {
251                     artifacts.add(dependencyArtifact);
252                 }
253             }
254         }
255 
256         Map<String, Artifact> managedVersions = null;
257 
258         if (managedDependencies != null && request.isResolveManagedVersions()) {
259             managedVersions = new HashMap<>();
260 
261             for (Dependency managedDependency : managedDependencies) {
262                 Artifact managedArtifact = createDependencyArtifact(managedDependency, null, pomArtifact);
263 
264                 managedVersions.put(managedDependency.getManagementKey(), managedArtifact);
265             }
266         }
267 
268         List<ArtifactRepository> aggregatedRepositories =
269                 aggregateRepositories(request.getRemoteRepositories(), pomRepositories);
270 
271         ResolutionGroup result =
272                 new ResolutionGroup(pomArtifact, relocatedArtifact, artifacts, managedVersions, aggregatedRepositories);
273 
274         cache.put(
275                 artifact,
276                 request.isResolveManagedVersions(),
277                 request.getLocalRepository(),
278                 request.getRemoteRepositories(),
279                 result);
280 
281         return result;
282     }
283 
284     private List<ArtifactRepository> getRepositoriesFromModel(RepositorySystemSession repositorySession, Model model) {
285         List<ArtifactRepository> pomRepositories = new ArrayList<>();
286         for (Repository modelRepository : model.getRepositories()) {
287             try {
288                 pomRepositories.add(MavenRepositorySystem.buildArtifactRepository(modelRepository));
289             } catch (InvalidRepositoryException e) {
290                 // can not use this then
291             }
292         }
293         mavenRepositorySystem.injectMirror(repositorySession, pomRepositories);
294         mavenRepositorySystem.injectProxy(repositorySession, pomRepositories);
295         mavenRepositorySystem.injectAuthentication(repositorySession, pomRepositories);
296         return pomRepositories;
297     }
298 
299     private boolean hasFile(Artifact artifact) {
300         return artifact != null
301                 && artifact.getFile() != null
302                 && artifact.getFile().exists();
303     }
304 
305     private List<ArtifactRepository> aggregateRepositories(
306             List<ArtifactRepository> requestRepositories, List<ArtifactRepository> pomRepositories) {
307         List<ArtifactRepository> repositories = requestRepositories;
308 
309         if (pomRepositories != null && !pomRepositories.isEmpty()) {
310             Map<String, ArtifactRepository> repos = new LinkedHashMap<>();
311 
312             for (ArtifactRepository repo : requestRepositories) {
313                 if (!repos.containsKey(repo.getId())) {
314                     repos.put(repo.getId(), repo);
315                 }
316             }
317 
318             for (ArtifactRepository repo : pomRepositories) {
319                 if (!repos.containsKey(repo.getId())) {
320                     repos.put(repo.getId(), repo);
321                 }
322             }
323 
324             repositories = new ArrayList<>(repos.values());
325         }
326 
327         return repositories;
328     }
329 
330     private Artifact createDependencyArtifact(Dependency dependency, Artifact owner, Artifact pom)
331             throws ArtifactMetadataRetrievalException {
332         try {
333             String inheritedScope = (owner != null) ? owner.getScope() : null;
334 
335             ArtifactFilter inheritedFilter = (owner != null) ? owner.getDependencyFilter() : null;
336 
337             return createDependencyArtifact(artifactFactory, dependency, inheritedScope, inheritedFilter);
338         } catch (InvalidVersionSpecificationException e) {
339             throw new ArtifactMetadataRetrievalException(
340                     "Invalid version for dependency " + dependency.getManagementKey() + ": " + e.getMessage(), e, pom);
341         }
342     }
343 
344     private static Artifact createDependencyArtifact(
345             ArtifactFactory factory, Dependency dependency, String inheritedScope, ArtifactFilter inheritedFilter)
346             throws InvalidVersionSpecificationException {
347         String effectiveScope = getEffectiveScope(dependency.getScope(), inheritedScope);
348 
349         if (effectiveScope == null) {
350             return null;
351         }
352 
353         VersionRange versionRange = VersionRange.createFromVersionSpec(dependency.getVersion());
354 
355         Artifact dependencyArtifact = factory.createDependencyArtifact(
356                 dependency.getGroupId(),
357                 dependency.getArtifactId(),
358                 versionRange,
359                 dependency.getType(),
360                 dependency.getClassifier(),
361                 effectiveScope,
362                 dependency.isOptional());
363 
364         if (inheritedFilter != null && !inheritedFilter.include(dependencyArtifact)) {
365             return null;
366         }
367 
368         if (Artifact.SCOPE_SYSTEM.equals(effectiveScope)) {
369             dependencyArtifact.setFile(new File(dependency.getSystemPath()));
370         }
371 
372         dependencyArtifact.setDependencyFilter(createDependencyFilter(dependency, inheritedFilter));
373 
374         return dependencyArtifact;
375     }
376 
377     private static String getEffectiveScope(String originalScope, String inheritedScope) {
378         String effectiveScope = Artifact.SCOPE_RUNTIME;
379 
380         if (originalScope == null) {
381             originalScope = Artifact.SCOPE_COMPILE;
382         }
383 
384         if (inheritedScope == null) {
385             // direct dependency retains its scope
386             effectiveScope = originalScope;
387         } else if (Artifact.SCOPE_TEST.equals(originalScope) || Artifact.SCOPE_PROVIDED.equals(originalScope)) {
388             // test and provided are not transitive, so exclude them
389             effectiveScope = null;
390         } else if (Artifact.SCOPE_SYSTEM.equals(originalScope)) {
391             // system scope come through unchanged...
392             effectiveScope = Artifact.SCOPE_SYSTEM;
393         } else if (Artifact.SCOPE_COMPILE.equals(originalScope) && Artifact.SCOPE_COMPILE.equals(inheritedScope)) {
394             // added to retain compile scope. Remove if you want compile inherited as runtime
395             effectiveScope = Artifact.SCOPE_COMPILE;
396         } else if (Artifact.SCOPE_TEST.equals(inheritedScope)) {
397             effectiveScope = Artifact.SCOPE_TEST;
398         } else if (Artifact.SCOPE_PROVIDED.equals(inheritedScope)) {
399             effectiveScope = Artifact.SCOPE_PROVIDED;
400         }
401 
402         return effectiveScope;
403     }
404 
405     private static ArtifactFilter createDependencyFilter(Dependency dependency, ArtifactFilter inheritedFilter) {
406         ArtifactFilter effectiveFilter = inheritedFilter;
407 
408         if (!dependency.getExclusions().isEmpty()) {
409             effectiveFilter = new ExclusionArtifactFilter(dependency.getExclusions());
410 
411             if (inheritedFilter != null) {
412                 effectiveFilter = new AndArtifactFilter(Arrays.asList(inheritedFilter, effectiveFilter));
413             }
414         }
415 
416         return effectiveFilter;
417     }
418 
419     @Override
420     public List<ArtifactVersion> retrieveAvailableVersions(
421             Artifact artifact, ArtifactRepository localRepository, List<ArtifactRepository> remoteRepositories)
422             throws ArtifactMetadataRetrievalException {
423         MetadataResolutionRequest request = new DefaultMetadataResolutionRequest();
424         injectSession(request);
425         request.setArtifact(artifact);
426         request.setLocalRepository(localRepository);
427         request.setRemoteRepositories(remoteRepositories);
428         return retrieveAvailableVersions(request);
429     }
430 
431     @Override
432     public List<ArtifactVersion> retrieveAvailableVersions(MetadataResolutionRequest request)
433             throws ArtifactMetadataRetrievalException {
434         RepositoryMetadata metadata = new ArtifactRepositoryMetadata(request.getArtifact());
435 
436         try {
437             repositoryMetadataManager.resolve(metadata, request);
438         } catch (RepositoryMetadataResolutionException e) {
439             throw new ArtifactMetadataRetrievalException(e.getMessage(), e, request.getArtifact());
440         }
441 
442         List<String> availableVersions = request.getLocalRepository().findVersions(request.getArtifact());
443 
444         return retrieveAvailableVersionsFromMetadata(metadata.getMetadata(), availableVersions);
445     }
446 
447     @Override
448     public List<ArtifactVersion> retrieveAvailableVersionsFromDeploymentRepository(
449             Artifact artifact, ArtifactRepository localRepository, ArtifactRepository deploymentRepository)
450             throws ArtifactMetadataRetrievalException {
451         RepositoryMetadata metadata = new ArtifactRepositoryMetadata(artifact);
452 
453         try {
454             repositoryMetadataManager.resolveAlways(metadata, localRepository, deploymentRepository);
455         } catch (RepositoryMetadataResolutionException e) {
456             throw new ArtifactMetadataRetrievalException(e.getMessage(), e, artifact);
457         }
458 
459         List<String> availableVersions = localRepository.findVersions(artifact);
460 
461         return retrieveAvailableVersionsFromMetadata(metadata.getMetadata(), availableVersions);
462     }
463 
464     private List<ArtifactVersion> retrieveAvailableVersionsFromMetadata(
465             Metadata repoMetadata, List<String> availableVersions) {
466         Collection<String> versions = new LinkedHashSet<>();
467 
468         if ((repoMetadata != null) && (repoMetadata.getVersioning() != null)) {
469             versions.addAll(repoMetadata.getVersioning().getVersions());
470         }
471 
472         versions.addAll(availableVersions);
473 
474         List<ArtifactVersion> artifactVersions = new ArrayList<>(versions.size());
475 
476         for (String version : versions) {
477             artifactVersions.add(new DefaultArtifactVersion(version));
478         }
479 
480         return artifactVersions;
481     }
482 
483     // USED BY MAVEN ASSEMBLY PLUGIN
484     @Deprecated
485     public static Set<Artifact> createArtifacts(
486             ArtifactFactory artifactFactory,
487             List<Dependency> dependencies,
488             String inheritedScope,
489             ArtifactFilter dependencyFilter,
490             MavenProject project)
491             throws InvalidDependencyVersionException {
492         Set<Artifact> artifacts = new LinkedHashSet<>();
493 
494         for (Dependency d : dependencies) {
495             Artifact dependencyArtifact;
496             try {
497                 dependencyArtifact = createDependencyArtifact(artifactFactory, d, inheritedScope, dependencyFilter);
498             } catch (InvalidVersionSpecificationException e) {
499                 throw new InvalidDependencyVersionException(project.getId(), d, project.getFile(), e);
500             }
501 
502             if (dependencyArtifact != null) {
503                 artifacts.add(dependencyArtifact);
504             }
505         }
506 
507         return artifacts;
508     }
509 
510     @SuppressWarnings("checkstyle:methodlength")
511     private ProjectRelocation retrieveRelocatedProject(Artifact artifact, MetadataResolutionRequest repositoryRequest)
512             throws ArtifactMetadataRetrievalException {
513         MavenProject project;
514 
515         Artifact pomArtifact;
516         Artifact relocatedArtifact = null;
517         boolean done = false;
518         do {
519             project = null;
520 
521             pomArtifact = artifactFactory.createProjectArtifact(
522                     artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), artifact.getScope());
523 
524             if ("pom".equals(artifact.getType())) {
525                 pomArtifact.setFile(artifact.getFile());
526             }
527 
528             if (Artifact.SCOPE_SYSTEM.equals(artifact.getScope())) {
529                 done = true;
530             } else {
531                 try {
532                     ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
533                     configuration.setLocalRepository(repositoryRequest.getLocalRepository());
534                     configuration.setRemoteRepositories(repositoryRequest.getRemoteRepositories());
535                     configuration.setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL);
536                     configuration.setProcessPlugins(false);
537                     configuration.setRepositoryMerging(ProjectBuildingRequest.RepositoryMerging.REQUEST_DOMINANT);
538                     MavenSession session = legacySupport.getSession();
539                     if (session != null) {
540                         configuration.setSystemProperties(session.getSystemProperties());
541                         configuration.setUserProperties(session.getUserProperties());
542                     } else {
543                         configuration.setSystemProperties(getSystemProperties());
544                         configuration.setUserProperties(new Properties());
545                     }
546                     configuration.setRepositorySession(legacySupport.getRepositorySession());
547 
548                     project = projectBuilder.build(pomArtifact, configuration).getProject();
549                 } catch (ProjectBuildingException e) {
550                     ModelProblem missingParentPom = hasMissingParentPom(e);
551                     if (missingParentPom != null) {
552                         throw new ArtifactMetadataRetrievalException(
553                                 "Failed to process POM for " + artifact.getId() + ": " + missingParentPom.getMessage(),
554                                 missingParentPom.getException(),
555                                 artifact);
556                     }
557 
558                     String message;
559 
560                     if (isMissingPom(e)) {
561                         message = "Missing POM for " + artifact.getId();
562                     } else if (isNonTransferablePom(e)) {
563                         throw new ArtifactMetadataRetrievalException(
564                                 "Failed to retrieve POM for " + artifact.getId() + ": "
565                                         + e.getCause().getMessage(),
566                                 e.getCause(),
567                                 artifact);
568                     } else {
569                         message = "Invalid POM for " + artifact.getId()
570                                 + ", transitive dependencies (if any) will not be available"
571                                 + ", enable verbose output (-X) for more details";
572                     }
573 
574                     if (logger.isDebugEnabled()) {
575                         message += ": " + e.getMessage();
576                     }
577 
578                     logger.warn(message);
579                 }
580 
581                 if (project != null) {
582                     Relocation relocation = null;
583 
584                     DistributionManagement distMgmt = project.getModel().getDistributionManagement();
585                     if (distMgmt != null) {
586                         relocation = distMgmt.getRelocation();
587 
588                         artifact.setDownloadUrl(distMgmt.getDownloadUrl());
589                         pomArtifact.setDownloadUrl(distMgmt.getDownloadUrl());
590                     }
591 
592                     if (relocation != null) {
593                         if (relocation.getGroupId() != null) {
594                             artifact.setGroupId(relocation.getGroupId());
595                             relocatedArtifact = artifact;
596                             project.setGroupId(relocation.getGroupId());
597                         }
598                         if (relocation.getArtifactId() != null) {
599                             artifact.setArtifactId(relocation.getArtifactId());
600                             relocatedArtifact = artifact;
601                             project.setArtifactId(relocation.getArtifactId());
602                         }
603                         if (relocation.getVersion() != null) {
604                             // note: see MNG-3454. This causes a problem, but fixing it may break more.
605                             artifact.setVersionRange(VersionRange.createFromVersion(relocation.getVersion()));
606                             relocatedArtifact = artifact;
607                             project.setVersion(relocation.getVersion());
608                         }
609 
610                         if (artifact.getDependencyFilter() != null
611                                 && !artifact.getDependencyFilter().include(artifact)) {
612                             return null;
613                         }
614 
615                         // MNG-2861: the artifact data has changed. If the available versions where previously
616                         // retrieved, we need to update it.
617                         // TODO shouldn't the versions be merged across relocations?
618                         List<ArtifactVersion> available = artifact.getAvailableVersions();
619                         if (available != null && !available.isEmpty()) {
620                             MetadataResolutionRequest metadataRequest =
621                                     new DefaultMetadataResolutionRequest(repositoryRequest);
622                             metadataRequest.setArtifact(artifact);
623                             available = retrieveAvailableVersions(metadataRequest);
624                             artifact.setAvailableVersions(available);
625                         }
626 
627                         String message = "  this artifact has been relocated to " + artifact.getGroupId() + ":"
628                                 + artifact.getArtifactId() + ":" + artifact.getVersion() + ".";
629 
630                         if (relocation.getMessage() != null) {
631                             message += "  " + relocation.getMessage();
632                         }
633 
634                         if (artifact.getDependencyTrail() != null
635                                 && artifact.getDependencyTrail().size() == 1) {
636                             logger.warn("While downloading " + pomArtifact.getGroupId() + ":"
637                                     + pomArtifact.getArtifactId() + ":" + pomArtifact.getVersion() + message);
638                         } else {
639                             logger.debug("While downloading " + pomArtifact.getGroupId() + ":"
640                                     + pomArtifact.getArtifactId() + ":" + pomArtifact.getVersion() + message);
641                         }
642                     } else {
643                         done = true;
644                     }
645                 } else {
646                     done = true;
647                 }
648             }
649         } while (!done);
650 
651         ProjectRelocation rel = new ProjectRelocation();
652         rel.project = project;
653         rel.pomArtifact = pomArtifact;
654         rel.relocatedArtifact = relocatedArtifact;
655 
656         return rel;
657     }
658 
659     private ModelProblem hasMissingParentPom(ProjectBuildingException e) {
660         if (e.getCause() instanceof ModelBuildingException) {
661             ModelBuildingException mbe = (ModelBuildingException) e.getCause();
662             for (ModelProblem problem : mbe.getProblems()) {
663                 if (problem.getException() instanceof UnresolvableModelException) {
664                     return problem;
665                 }
666             }
667         }
668         return null;
669     }
670 
671     private boolean isMissingPom(Exception e) {
672         if (e.getCause() instanceof MultipleArtifactsNotFoundException) {
673             return true;
674         }
675         return e.getCause() instanceof org.eclipse.aether.resolution.ArtifactResolutionException
676                 && e.getCause().getCause() instanceof ArtifactNotFoundException;
677     }
678 
679     private boolean isNonTransferablePom(Exception e) {
680         if (e.getCause() instanceof ArtifactResolutionException) {
681             return true;
682         }
683         return e.getCause() instanceof org.eclipse.aether.resolution.ArtifactResolutionException
684                 && !(e.getCause().getCause() instanceof ArtifactNotFoundException);
685     }
686 
687     private Properties getSystemProperties() {
688         Properties props = new Properties();
689 
690         EnvironmentUtils.addEnvVars(props);
691 
692         SystemProperties.addSystemProperties(props);
693 
694         return props;
695     }
696 
697     private static final class ProjectRelocation {
698         private MavenProject project;
699 
700         private Artifact pomArtifact;
701 
702         private Artifact relocatedArtifact;
703     }
704 }