diff --git a/maven-project/src/main/java/org/apache/maven/project/artifact/MavenMetadataSource.java b/maven-project/src/main/java/org/apache/maven/project/artifact/MavenMetadataSource.java index aed494b92c..cc2e0dc050 100644 --- a/maven-project/src/main/java/org/apache/maven/project/artifact/MavenMetadataSource.java +++ b/maven-project/src/main/java/org/apache/maven/project/artifact/MavenMetadataSource.java @@ -89,16 +89,59 @@ public class MavenMetadataSource private boolean strictlyEnforceThePresenceOfAValidMavenPOM = false; /** - * Retrieve the metadata for the project from the repository. - * Uses the ProjectBuilder, to enable post-processing and inheritance calculation before retrieving the - * associated artifacts. + * Resolve all relocations in the POM for this artifact, and return the new artifact coordinate. */ - public ResolutionGroup retrieve( Artifact artifact, ArtifactRepository localRepository, List remoteRepositories ) + public Artifact retrieveRelocatedArtifact( Artifact artifact, + ArtifactRepository localRepository, + List remoteRepositories ) + throws ArtifactMetadataRetrievalException + { + if ( artifact instanceof ActiveProjectArtifact ) + { + return artifact; + } + + ProjectRelocation res = retrieveRelocatedProject( artifact, localRepository, remoteRepositories ); + MavenProject project = res.project; + + if ( project == null || getRelocationKey( artifact ).equals( getRelocationKey( project.getArtifact() ) ) ) + { + return artifact; + } + + Artifact result = null; + if ( artifact.getClassifier() != null ) + { + result = artifactFactory.createArtifactWithClassifier( project.getGroupId(), project.getArtifactId(), project.getVersion(), artifact.getType(), artifact.getClassifier() ); + } + else + { + result = artifactFactory.createArtifact( project.getGroupId(), project.getArtifactId(), project.getVersion(), artifact.getScope(), artifact.getType() ); + } + + result.setScope( artifact.getScope() ); + result.setArtifactHandler( artifact.getArtifactHandler() ); + result.setDependencyFilter( artifact.getDependencyFilter() ); + result.setDependencyTrail( artifact.getDependencyTrail() ); + result.setOptional( artifact.isOptional() ); + result.setRelease( artifact.isRelease() ); + + return result; + } + + private String getRelocationKey( Artifact artifact ) + { + return artifact.getGroupId() + ":" + artifact.getArtifactId() + ":" + artifact.getVersion(); + } + + private ProjectRelocation retrieveRelocatedProject( Artifact artifact, + ArtifactRepository localRepository, + List remoteRepositories ) throws ArtifactMetadataRetrievalException { if ( remoteRepositories == null ) { - remoteRepositories = Collections.EMPTY_LIST; + remoteRepositories = Collections.emptyList(); } try @@ -111,7 +154,6 @@ public class MavenMetadataSource } MavenProject project = null; - Artifact pomArtifact; boolean done = false; @@ -181,14 +223,17 @@ public class MavenMetadataSource if ( relocation.getGroupId() != null ) { artifact.setGroupId( relocation.getGroupId() ); + project.setGroupId( relocation.getGroupId() ); } if ( relocation.getArtifactId() != null ) { artifact.setArtifactId( relocation.getArtifactId() ); + project.setArtifactId( relocation.getArtifactId() ); } if ( relocation.getVersion() != null ) { artifact.setVersionRange( VersionRange.createFromVersion( relocation.getVersion() ) ); + project.setVersion( relocation.getVersion() ); } if ( ( artifact.getDependencyFilter() != null ) && @@ -239,6 +284,25 @@ public class MavenMetadataSource } while ( !done ); + ProjectRelocation res = new ProjectRelocation(); + res.project = project; + res.pomArtifact = pomArtifact; + + return res; + } + + /** + * Retrieve the metadata for the project from the repository. + * Uses the ProjectBuilder, to enable post-processing and inheritance calculation before retrieving the + * associated artifacts. + */ + public ResolutionGroup retrieve( Artifact artifact, ArtifactRepository localRepository, List remoteRepositories ) + throws ArtifactMetadataRetrievalException + { + ProjectRelocation res = retrieveRelocatedProject( artifact, localRepository, remoteRepositories ); + MavenProject project = res.project; + Artifact pomArtifact = res.pomArtifact; + // last ditch effort to try to get this set... if ( artifact.getDownloadUrl() == null ) { @@ -511,4 +575,11 @@ public class MavenMetadataSource { container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY ); } + + private static final class ProjectRelocation + { + private MavenProject project; + private Artifact pomArtifact; + } + } diff --git a/maven-project/src/test/java/org/apache/maven/project/TestArtifactResolver.java b/maven-project/src/test/java/org/apache/maven/project/TestArtifactResolver.java index 3e5226c5b7..14ba85ed01 100644 --- a/maven-project/src/test/java/org/apache/maven/project/TestArtifactResolver.java +++ b/maven-project/src/test/java/org/apache/maven/project/TestArtifactResolver.java @@ -189,6 +189,14 @@ public class TestArtifactResolver return projectArtifacts; } + + public Artifact retrieveRelocatedArtifact( Artifact artifact, + ArtifactRepository localRepository, + List remoteRepositories ) + throws ArtifactMetadataRetrievalException + { + return artifact; + } } public Source source() @@ -199,12 +207,14 @@ public class TestArtifactResolver /** * @noinspection RefusedBequest */ + @Override public void resolve( Artifact artifact, List remoteRepositories, ArtifactRepository localRepository ) throws ArtifactResolutionException { artifact.setFile( new File( "dummy" ) ); } + @Override public ArtifactResolutionResult resolveTransitively( Set artifacts, Artifact originatingArtifact, ArtifactRepository localRepository, List remoteRepositories, ArtifactMetadataSource source, ArtifactFilter filter ) @@ -214,6 +224,7 @@ public class TestArtifactResolver new Source( artifactFactory, repositoryFactory, container ), filter ); } + @Override public ArtifactResolutionResult resolveTransitively( Set artifacts, Artifact originatingArtifact, List remoteRepositories, ArtifactRepository localRepository, ArtifactMetadataSource source ) @@ -226,7 +237,7 @@ public class TestArtifactResolver public void contextualize( Context context ) throws ContextException { - this.container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY ); + container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY ); } } \ No newline at end of file