diff --git a/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java b/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java index 6b1ef7750d..ddbdf97c9f 100644 --- a/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java +++ b/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java @@ -208,9 +208,9 @@ public class DefaultProjectBuilder ReactorModelPool modelPool ) { ModelResolver resolver = - new ReactorModelResolver( configuration.getRepositorySession(), artifactResolver, - repositoryManager, - RepositoryUtils.toRepos( configuration.getRemoteRepositories() ), modelPool ); + new ProjectModelResolver( configuration.getRepositorySession(), artifactResolver, repositoryManager, + RepositoryUtils.toRepos( configuration.getRemoteRepositories() ), + configuration.getRepositoryMerging(), modelPool ); ModelBuildingRequest request = new DefaultModelBuildingRequest(); diff --git a/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuildingHelper.java b/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuildingHelper.java index 69c4d3d9a1..1465ab0969 100644 --- a/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuildingHelper.java +++ b/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuildingHelper.java @@ -105,24 +105,48 @@ public class DefaultProjectBuildingHelper ProjectBuildingRequest request ) throws InvalidRepositoryException { - List artifactRepositories = new ArrayList(); - Collection repoIds = new HashSet(); + List internalRepositories = new ArrayList(); for ( Repository repository : pomRepositories ) { - artifactRepositories.add( repositorySystem.buildArtifactRepository( repository ) ); - repoIds.add( repository.getId() ); + internalRepositories.add( repositorySystem.buildArtifactRepository( repository ) ); } - repositorySystem.injectMirror( request.getRepositorySession(), artifactRepositories ); + repositorySystem.injectMirror( request.getRepositorySession(), internalRepositories ); - repositorySystem.injectProxy( request.getRepositorySession(), artifactRepositories ); + repositorySystem.injectProxy( request.getRepositorySession(), internalRepositories ); - repositorySystem.injectAuthentication( request.getRepositorySession(), artifactRepositories ); + repositorySystem.injectAuthentication( request.getRepositorySession(), internalRepositories ); - if ( externalRepositories != null ) + List dominantRepositories; + List recessiveRepositories; + + if ( ProjectBuildingRequest.RepositoryMerging.REQUEST_DOMINANT.equals( request.getRepositoryMerging() ) ) { - for ( ArtifactRepository repository : externalRepositories ) + dominantRepositories = externalRepositories; + recessiveRepositories = internalRepositories; + } + else + { + dominantRepositories = internalRepositories; + recessiveRepositories = externalRepositories; + } + + List artifactRepositories = new ArrayList(); + Collection repoIds = new HashSet(); + + if ( dominantRepositories != null ) + { + for ( ArtifactRepository repository : dominantRepositories ) + { + repoIds.add( repository.getId() ); + artifactRepositories.add( repository ); + } + } + + if ( recessiveRepositories != null ) + { + for ( ArtifactRepository repository : recessiveRepositories ) { if ( repoIds.add( repository.getId() ) ) { diff --git a/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuildingRequest.java b/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuildingRequest.java index c53ece8ce2..f32426d0e3 100644 --- a/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuildingRequest.java +++ b/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuildingRequest.java @@ -61,6 +61,8 @@ public class DefaultProjectBuildingRequest private boolean resolveDependencies; + private RepositoryMerging repositoryMerging = RepositoryMerging.POM_DOMINANT; + public DefaultProjectBuildingRequest() { processPlugins = true; @@ -301,4 +303,19 @@ public class DefaultProjectBuildingRequest return this; } + public DefaultProjectBuildingRequest setRepositoryMerging( RepositoryMerging repositoryMerging ) + { + if ( repositoryMerging == null ) + { + throw new IllegalArgumentException( "repository merge mode not specified" ); + } + this.repositoryMerging = repositoryMerging; + return this; + } + + public RepositoryMerging getRepositoryMerging() + { + return repositoryMerging; + } + } diff --git a/maven-core/src/main/java/org/apache/maven/project/ProjectBuildingRequest.java b/maven-core/src/main/java/org/apache/maven/project/ProjectBuildingRequest.java index e119b58b7a..39522f6843 100644 --- a/maven-core/src/main/java/org/apache/maven/project/ProjectBuildingRequest.java +++ b/maven-core/src/main/java/org/apache/maven/project/ProjectBuildingRequest.java @@ -151,4 +151,39 @@ public interface ProjectBuildingRequest ProjectBuildingRequest setRepositorySession( RepositorySystemSession repositorySession ); + /** + * Sets the merge mode used to combine repositories declared in the POM with the repositories specified in this + * request. + * + * @param mode The repository merge mode, must not be {@code null}. + * @return This request for chaining, never {@code null}. + * @see #setRemoteRepositories(List) + */ + ProjectBuildingRequest setRepositoryMerging( RepositoryMerging mode ); + + /** + * Gets the merge mode used to combine repositories declared in the POM with the repositories specified in this + * request + * + * @return The merge mode, never {@code null}. + */ + RepositoryMerging getRepositoryMerging(); + + /** + * The possible merge modes for combining remote repositories. + */ + enum RepositoryMerging + { + + /** + * The repositories declared in the POM have precedence over the repositories specified in the request. + */ + POM_DOMINANT, + + /** + * The repositories specified in the request have precedence over the repositories declared in the POM. + */ + REQUEST_DOMINANT, + } + } diff --git a/maven-core/src/main/java/org/apache/maven/project/ReactorModelResolver.java b/maven-core/src/main/java/org/apache/maven/project/ProjectModelResolver.java similarity index 81% rename from maven-core/src/main/java/org/apache/maven/project/ReactorModelResolver.java rename to maven-core/src/main/java/org/apache/maven/project/ProjectModelResolver.java index 9066fce5aa..36490710ba 100644 --- a/maven-core/src/main/java/org/apache/maven/project/ReactorModelResolver.java +++ b/maven-core/src/main/java/org/apache/maven/project/ProjectModelResolver.java @@ -48,7 +48,7 @@ import org.sonatype.aether.util.artifact.DefaultArtifact; * * @author Benjamin Bentmann */ -class ReactorModelResolver +class ProjectModelResolver implements ModelResolver { @@ -60,7 +60,7 @@ class ReactorModelResolver private List pomRepositories; - private final List defaultRepositories; + private final List externalRepositories; private final ArtifactResolver resolver; @@ -70,28 +70,32 @@ class ReactorModelResolver private final ReactorModelPool modelPool; - public ReactorModelResolver( RepositorySystemSession session, ArtifactResolver resolver, + private final ProjectBuildingRequest.RepositoryMerging repositoryMerging; + + public ProjectModelResolver( RepositorySystemSession session, ArtifactResolver resolver, RemoteRepositoryManager remoteRepositoryManager, List repositories, - ReactorModelPool modelPool ) + ProjectBuildingRequest.RepositoryMerging repositoryMerging, ReactorModelPool modelPool ) { this.session = session; this.resolver = resolver; this.remoteRepositoryManager = remoteRepositoryManager; this.pomRepositories = new ArrayList(); - this.defaultRepositories = repositories; + this.externalRepositories = repositories; this.repositories = repositories; + this.repositoryMerging = repositoryMerging; this.repositoryIds = new HashSet(); this.modelPool = modelPool; } - private ReactorModelResolver( ReactorModelResolver original ) + private ProjectModelResolver( ProjectModelResolver original ) { this.session = original.session; this.resolver = original.resolver; this.remoteRepositoryManager = original.remoteRepositoryManager; this.pomRepositories = original.pomRepositories; - this.defaultRepositories = original.defaultRepositories; + this.externalRepositories = original.externalRepositories; this.repositories = original.repositories; + this.repositoryMerging = original.repositoryMerging; this.repositoryIds = new HashSet( original.repositoryIds ); this.modelPool = original.modelPool; } @@ -106,10 +110,17 @@ class ReactorModelResolver List newRepositories = Collections.singletonList( convert( repository ) ); - pomRepositories = - remoteRepositoryManager.aggregateRepositories( session, pomRepositories, newRepositories, true ); - repositories = - remoteRepositoryManager.aggregateRepositories( session, pomRepositories, defaultRepositories, false ); + if ( ProjectBuildingRequest.RepositoryMerging.REQUEST_DOMINANT.equals( repositoryMerging ) ) + { + repositories = remoteRepositoryManager.aggregateRepositories( session, repositories, newRepositories, true ); + } + else + { + pomRepositories = + remoteRepositoryManager.aggregateRepositories( session, pomRepositories, newRepositories, true ); + repositories = + remoteRepositoryManager.aggregateRepositories( session, pomRepositories, externalRepositories, false ); + } } private static RemoteRepository convert( Repository repository ) @@ -145,7 +156,7 @@ class ReactorModelResolver public ModelResolver newCopy() { - return new ReactorModelResolver( this ); + return new ProjectModelResolver( this ); } public ModelSource resolveModel( String groupId, String artifactId, String version ) diff --git a/maven-core/src/main/java/org/apache/maven/project/artifact/MavenMetadataSource.java b/maven-core/src/main/java/org/apache/maven/project/artifact/MavenMetadataSource.java index cc93fe712c..a29b2ff43e 100644 --- a/maven-core/src/main/java/org/apache/maven/project/artifact/MavenMetadataSource.java +++ b/maven-core/src/main/java/org/apache/maven/project/artifact/MavenMetadataSource.java @@ -573,6 +573,7 @@ public class MavenMetadataSource configuration.setRemoteRepositories( repositoryRequest.getRemoteRepositories() ); configuration.setValidationLevel( ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL ); configuration.setProcessPlugins( false ); + configuration.setRepositoryMerging( ProjectBuildingRequest.RepositoryMerging.REQUEST_DOMINANT ); configuration.setSystemProperties( getSystemProperties() ); configuration.setRepositorySession( legacySupport.getRepositorySession() );