2222import javax .inject .Named ;
2323import javax .inject .Singleton ;
2424
25+ import java .util .LinkedHashMap ;
2526import java .util .LinkedHashSet ;
2627import java .util .List ;
2728import java .util .Map ;
2829import java .util .Objects ;
2930import java .util .Properties ;
3031import java .util .Set ;
3132
32- import org .apache .maven .model .DistributionManagement ;
3333import org .apache .maven .model .Model ;
34- import org .apache .maven .model .Relocation ;
3534import org .apache .maven .model .building .DefaultModelBuilderFactory ;
3635import org .apache .maven .model .building .DefaultModelBuildingRequest ;
3736import org .apache .maven .model .building .FileModelSource ;
4241import org .apache .maven .model .building .ModelProblem ;
4342import org .apache .maven .model .building .ModelProblemUtils ;
4443import org .apache .maven .model .resolution .UnresolvableModelException ;
44+ import org .apache .maven .repository .internal .relocation .DistributionManagementArtifactRelocationSource ;
45+ import org .apache .maven .repository .internal .relocation .UserPropertiesArtifactRelocationSource ;
4546import org .codehaus .plexus .util .StringUtils ;
4647import org .eclipse .aether .RepositoryEvent ;
4748import org .eclipse .aether .RepositoryEvent .EventType ;
@@ -94,6 +95,8 @@ public class DefaultArtifactDescriptorReader implements ArtifactDescriptorReader
9495
9596 private ModelCacheFactory modelCacheFactory ;
9697
98+ private Map <String , MavenArtifactRelocationSource > artifactRelocationSources ;
99+
97100 private final ArtifactDescriptorReaderDelegate artifactDescriptorReaderDelegate =
98101 new ArtifactDescriptorReaderDelegate ();
99102
@@ -104,7 +107,10 @@ public DefaultArtifactDescriptorReader() {
104107 // enable no-arg constructor
105108 }
106109
107- @ Inject
110+ /**
111+ * Just here for "static" resolver providers.
112+ */
113+ @ Deprecated
108114 public DefaultArtifactDescriptorReader (
109115 RemoteRepositoryManager remoteRepositoryManager ,
110116 VersionResolver versionResolver ,
@@ -120,6 +126,34 @@ public DefaultArtifactDescriptorReader(
120126 setModelBuilder (modelBuilder );
121127 setRepositoryEventDispatcher (repositoryEventDispatcher );
122128 setModelCacheFactory (modelCacheFactory );
129+ Map <String , MavenArtifactRelocationSource > artifactRelocationSources = new LinkedHashMap <>(); // order!
130+ artifactRelocationSources .put (
131+ UserPropertiesArtifactRelocationSource .NAME , new UserPropertiesArtifactRelocationSource ());
132+ artifactRelocationSources .put (
133+ DistributionManagementArtifactRelocationSource .NAME ,
134+ new DistributionManagementArtifactRelocationSource ());
135+ setArtifactRelocationSources (artifactRelocationSources );
136+ }
137+
138+ @ SuppressWarnings ("checkstyle:parameternumber" )
139+ @ Inject
140+ public DefaultArtifactDescriptorReader (
141+ RemoteRepositoryManager remoteRepositoryManager ,
142+ VersionResolver versionResolver ,
143+ VersionRangeResolver versionRangeResolver ,
144+ ArtifactResolver artifactResolver ,
145+ ModelBuilder modelBuilder ,
146+ RepositoryEventDispatcher repositoryEventDispatcher ,
147+ ModelCacheFactory modelCacheFactory ,
148+ Map <String , MavenArtifactRelocationSource > artifactRelocationSources ) {
149+ setRemoteRepositoryManager (remoteRepositoryManager );
150+ setVersionResolver (versionResolver );
151+ setVersionRangeResolver (versionRangeResolver );
152+ setArtifactResolver (artifactResolver );
153+ setModelBuilder (modelBuilder );
154+ setRepositoryEventDispatcher (repositoryEventDispatcher );
155+ setModelCacheFactory (modelCacheFactory );
156+ setArtifactRelocationSources (artifactRelocationSources );
123157 }
124158
125159 @ Deprecated
@@ -175,6 +209,13 @@ public DefaultArtifactDescriptorReader setModelCacheFactory(ModelCacheFactory mo
175209 return this ;
176210 }
177211
212+ public DefaultArtifactDescriptorReader setArtifactRelocationSources (
213+ Map <String , MavenArtifactRelocationSource > artifactRelocationSources ) {
214+ this .artifactRelocationSources =
215+ Objects .requireNonNull (artifactRelocationSources , "artifactRelocationSources cannot be null" );
216+ return this ;
217+ }
218+
178219 @ Override
179220 public ArtifactDescriptorResult readArtifactDescriptor (
180221 RepositorySystemSession session , ArtifactDescriptorRequest request ) throws ArtifactDescriptorException {
@@ -336,23 +377,28 @@ private Model loadPom(
336377 throw new ArtifactDescriptorException (result );
337378 }
338379
339- Relocation relocation = getRelocation (model );
340-
341- if (relocation != null ) {
342- result .addRelocation (a );
343- a = new RelocatedArtifact (
344- a ,
345- relocation .getGroupId (),
346- relocation .getArtifactId (),
347- relocation .getVersion (),
348- relocation .getMessage ());
349- result .setArtifact (a );
380+ Artifact relocatedArtifact = getRelocation (session , result , model );
381+ if (relocatedArtifact != null ) {
382+ if (withinSameGav (relocatedArtifact , a )) {
383+ result .setArtifact (relocatedArtifact );
384+ return model ; // they share same model
385+ } else {
386+ result .addRelocation (a );
387+ a = relocatedArtifact ;
388+ result .setArtifact (a );
389+ }
350390 } else {
351391 return model ;
352392 }
353393 }
354394 }
355395
396+ private boolean withinSameGav (Artifact a1 , Artifact a2 ) {
397+ return Objects .equals (a1 .getGroupId (), a2 .getGroupId ())
398+ && Objects .equals (a1 .getArtifactId (), a2 .getArtifactId ())
399+ && Objects .equals (a1 .getVersion (), a2 .getVersion ());
400+ }
401+
356402 private Properties toProperties (Map <String , String > dominant , Map <String , String > recessive ) {
357403 Properties props = new Properties ();
358404 if (recessive != null ) {
@@ -364,13 +410,17 @@ private Properties toProperties(Map<String, String> dominant, Map<String, String
364410 return props ;
365411 }
366412
367- private Relocation getRelocation (Model model ) {
368- Relocation relocation = null ;
369- DistributionManagement distMgmt = model .getDistributionManagement ();
370- if (distMgmt != null ) {
371- relocation = distMgmt .getRelocation ();
413+ private Artifact getRelocation (
414+ RepositorySystemSession session , ArtifactDescriptorResult artifactDescriptorResult , Model model )
415+ throws ArtifactDescriptorException {
416+ Artifact result = null ;
417+ for (MavenArtifactRelocationSource source : artifactRelocationSources .values ()) {
418+ result = source .relocatedTarget (session , artifactDescriptorResult , model );
419+ if (result != null ) {
420+ break ;
421+ }
372422 }
373- return relocation ;
423+ return result ;
374424 }
375425
376426 private void missingDescriptor (
0 commit comments