diff --git a/maven-compat/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java b/maven-compat/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java index 6c2ef2d428..f40aa89359 100644 --- a/maven-compat/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java +++ b/maven-compat/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java @@ -146,9 +146,9 @@ public class DefaultMavenProjectBuilder try { ArtifactRepository repo = repositorySystem.buildArtifactRepository( (Repository) repository ); - repositorySystem.injectMirror( Arrays.asList( repo ), request.getMirrors() ); - repositorySystem.injectProxy( Arrays.asList( repo ), request.getProxies() ); - repositorySystem.injectAuthentication( Arrays.asList( repo ), request.getServers() ); + repositorySystem.injectMirror( request.getRepositorySession(), Arrays.asList( repo ) ); + repositorySystem.injectProxy( request.getRepositorySession(), Arrays.asList( repo ) ); + repositorySystem.injectAuthentication( request.getRepositorySession(), Arrays.asList( repo ) ); repos.add( repo ); } catch ( InvalidRepositoryException e ) diff --git a/maven-compat/src/main/java/org/apache/maven/repository/legacy/LegacyRepositorySystem.java b/maven-compat/src/main/java/org/apache/maven/repository/legacy/LegacyRepositorySystem.java index c2fd990cd8..91dc3b6c76 100644 --- a/maven-compat/src/main/java/org/apache/maven/repository/legacy/LegacyRepositorySystem.java +++ b/maven-compat/src/main/java/org/apache/maven/repository/legacy/LegacyRepositorySystem.java @@ -28,9 +28,11 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import org.apache.maven.RepositoryUtils; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.InvalidRepositoryException; import org.apache.maven.artifact.factory.ArtifactFactory; +import org.apache.maven.artifact.metadata.ArtifactMetadata; import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.repository.legacy.repository.ArtifactRepositoryFactory; import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy; @@ -50,8 +52,6 @@ import org.apache.maven.model.RepositoryPolicy; import org.apache.maven.repository.DelegatingLocalArtifactRepository; import org.apache.maven.repository.LocalArtifactRepository; import org.apache.maven.repository.ArtifactTransferListener; -import org.apache.maven.repository.MetadataResolutionRequest; -import org.apache.maven.repository.MetadataResolutionResult; import org.apache.maven.repository.MirrorSelector; import org.apache.maven.repository.Proxy; import org.apache.maven.repository.RepositorySystem; @@ -70,6 +70,10 @@ import org.codehaus.plexus.component.annotations.Requirement; import org.codehaus.plexus.component.repository.exception.ComponentLookupException; import org.codehaus.plexus.logging.Logger; import org.codehaus.plexus.util.StringUtils; +import org.sonatype.aether.RepositorySystemSession; +import org.sonatype.aether.repository.AuthenticationSelector; +import org.sonatype.aether.repository.ProxySelector; +import org.sonatype.aether.repository.RemoteRepository; /** * @author Jason van Zyl @@ -458,10 +462,52 @@ public class LegacyRepositorySystem repository.setId( mirror.getId() ); repository.setUrl( mirror.getUrl() ); - ArtifactRepositoryLayout layout = layouts.get( mirror.getLayout() ); - if ( layout != null ) + if ( StringUtils.isNotEmpty( mirror.getLayout() ) ) { - repository.setLayout( layout ); + repository.setLayout( getLayout( mirror.getLayout() ) ); + } + } + } + } + } + + private Mirror getMirror( RepositorySystemSession session, ArtifactRepository repository ) + { + if ( session != null ) + { + org.sonatype.aether.repository.MirrorSelector selector = session.getMirrorSelector(); + if ( selector != null ) + { + RemoteRepository repo = selector.getMirror( RepositoryUtils.toRepo( repository ) ); + if ( repo != null ) + { + Mirror mirror = new Mirror(); + mirror.setId( repo.getId() ); + mirror.setUrl( repo.getUrl() ); + mirror.setLayout( repo.getContentType() ); + return mirror; + } + } + } + return null; + } + + public void injectMirror( RepositorySystemSession session, List repositories ) + { + if ( repositories != null && session != null ) + { + for ( ArtifactRepository repository : repositories ) + { + Mirror mirror = getMirror( session, repository ); + + if ( mirror != null ) + { + repository.setId( mirror.getId() ); + repository.setUrl( mirror.getUrl() ); + + if ( StringUtils.isNotEmpty( mirror.getLayout() ) ) + { + repository.setLayout( getLayout( mirror.getLayout() ) ); } } } @@ -515,6 +561,35 @@ public class LegacyRepositorySystem } } + private Authentication getAuthentication( RepositorySystemSession session, ArtifactRepository repository ) + { + if ( session != null ) + { + AuthenticationSelector selector = session.getAuthenticationSelector(); + if ( selector != null ) + { + org.sonatype.aether.repository.Authentication auth = + selector.getAuthentication( RepositoryUtils.toRepo( repository ) ); + if ( auth != null ) + { + return new Authentication( auth.getUsername(), auth.getPassword() ); + } + } + } + return null; + } + + public void injectAuthentication( RepositorySystemSession session, List repositories ) + { + if ( repositories != null && session != null ) + { + for ( ArtifactRepository repository : repositories ) + { + repository.setAuthentication( getAuthentication( session, repository ) ); + } + } + } + private org.apache.maven.settings.Proxy getProxy( ArtifactRepository repository, List proxies ) { @@ -572,23 +647,43 @@ public class LegacyRepositorySystem } } - public MetadataResolutionResult resolveMetadata( MetadataResolutionRequest request ) + private Proxy getProxy( RepositorySystemSession session, ArtifactRepository repository ) { - - // ArtifactResolutionResult collect( Set artifacts, - // Artifact originatingArtifact, - // Map managedVersions, - // ArtifactRepository localRepository, - // List remoteRepositories, - // ArtifactMetadataSource source, - // ArtifactFilter filter, - // List listeners, - // List conflictResolvers ) - // ArtifactResolutionResult result = artifactCollector. - + if ( session != null ) + { + ProxySelector selector = session.getProxySelector(); + if ( selector != null ) + { + org.sonatype.aether.repository.Proxy proxy = selector.getProxy( RepositoryUtils.toRepo( repository ) ); + if ( proxy != null ) + { + Proxy p = new Proxy(); + p.setHost( proxy.getHost() ); + p.setProtocol( proxy.getType() ); + p.setPort( proxy.getPort() ); + if ( proxy.getAuthentication() != null ) + { + p.setUserName( proxy.getAuthentication().getUsername() ); + p.setPassword( proxy.getAuthentication().getPassword() ); + } + return p; + } + } + } return null; } + public void injectProxy( RepositorySystemSession session, List repositories ) + { + if ( repositories != null && session != null ) + { + for ( ArtifactRepository repository : repositories ) + { + repository.setProxy( getProxy( session, repository ) ); + } + } + } + public void retrieve( ArtifactRepository repository, File destination, String remotePath, ArtifactTransferListener transferListener ) throws ArtifactTransferFailedException, ArtifactDoesNotExistException @@ -650,7 +745,7 @@ public class LegacyRepositorySystem ArtifactRepositoryPolicy releases = buildArtifactRepositoryPolicy( repo.getReleases() ); - return createArtifactRepository( id, url, layouts.get( repo.getLayout() ), snapshots, releases ); + return createArtifactRepository( id, url, getLayout( repo.getLayout() ), snapshots, releases ); } else { @@ -702,4 +797,58 @@ public class LegacyRepositorySystem return getMessage( error.getCause(), def ); } + private ArtifactRepositoryLayout getLayout( String id ) + { + ArtifactRepositoryLayout layout = layouts.get( id ); + + if ( layout == null ) + { + layout = new UnknownRepositoryLayout( id, layouts.get( "default" ) ); + } + + return layout; + } + + /** + * In the future, the legacy system might encounter repository types for which no layout components exists because + * the actual communication with the repository happens via a repository connector. As a minimum, the legacy system + * needs to retain the id of this layout so that the content type of the remote repository can still be accurately + * described. + */ + static class UnknownRepositoryLayout + implements ArtifactRepositoryLayout + { + + private final String id; + + private final ArtifactRepositoryLayout fallback; + + public UnknownRepositoryLayout( String id, ArtifactRepositoryLayout fallback ) + { + this.id = id; + this.fallback = fallback; + } + + public String getId() + { + return id; + } + + public String pathOf( Artifact artifact ) + { + return fallback.pathOf( artifact ); + } + + public String pathOfLocalRepositoryMetadata( ArtifactMetadata metadata, ArtifactRepository repository ) + { + return fallback.pathOfLocalRepositoryMetadata( metadata, repository ); + } + + public String pathOfRemoteRepositoryMetadata( ArtifactMetadata metadata ) + { + return fallback.pathOfRemoteRepositoryMetadata( metadata ); + } + + } + } 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 c81e213e28..69c4d3d9a1 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 @@ -114,11 +114,11 @@ public class DefaultProjectBuildingHelper repoIds.add( repository.getId() ); } - repositorySystem.injectMirror( artifactRepositories, request.getMirrors() ); + repositorySystem.injectMirror( request.getRepositorySession(), artifactRepositories ); - repositorySystem.injectProxy( artifactRepositories, request.getProxies() ); + repositorySystem.injectProxy( request.getRepositorySession(), artifactRepositories ); - repositorySystem.injectAuthentication( artifactRepositories, request.getServers() ); + repositorySystem.injectAuthentication( request.getRepositorySession(), artifactRepositories ); if ( externalRepositories != null ) { diff --git a/maven-core/src/main/java/org/apache/maven/project/MavenProject.java b/maven-core/src/main/java/org/apache/maven/project/MavenProject.java index f36aefdefc..488580ba5c 100644 --- a/maven-core/src/main/java/org/apache/maven/project/MavenProject.java +++ b/maven-core/src/main/java/org/apache/maven/project/MavenProject.java @@ -1844,8 +1844,9 @@ public class MavenProject { ArtifactRepository repo = repositorySystem.buildArtifactRepository( getDistributionManagement().getRepository() ); - repositorySystem.injectProxy( Arrays.asList( repo ), projectBuilderConfiguration.getProxies() ); - repositorySystem.injectAuthentication( Arrays.asList( repo ), projectBuilderConfiguration.getServers() ); + repositorySystem.injectProxy( projectBuilderConfiguration.getRepositorySession(), Arrays.asList( repo ) ); + repositorySystem.injectAuthentication( projectBuilderConfiguration.getRepositorySession(), + Arrays.asList( repo ) ); setReleaseArtifactRepository( repo ); } catch ( InvalidRepositoryException e ) @@ -1864,8 +1865,9 @@ public class MavenProject { ArtifactRepository repo = repositorySystem.buildArtifactRepository( getDistributionManagement().getSnapshotRepository() ); - repositorySystem.injectProxy( Arrays.asList( repo ), projectBuilderConfiguration.getProxies() ); - repositorySystem.injectAuthentication( Arrays.asList( repo ), projectBuilderConfiguration.getServers() ); + repositorySystem.injectProxy( projectBuilderConfiguration.getRepositorySession(), Arrays.asList( repo ) ); + repositorySystem.injectAuthentication( projectBuilderConfiguration.getRepositorySession(), + Arrays.asList( repo ) ); setSnapshotArtifactRepository( repo ); } catch ( InvalidRepositoryException e ) diff --git a/maven-core/src/main/java/org/apache/maven/repository/RepositorySystem.java b/maven-core/src/main/java/org/apache/maven/repository/RepositorySystem.java index 69a4edf326..eb8230fd52 100644 --- a/maven-core/src/main/java/org/apache/maven/repository/RepositorySystem.java +++ b/maven-core/src/main/java/org/apache/maven/repository/RepositorySystem.java @@ -34,6 +34,7 @@ import org.apache.maven.model.Plugin; import org.apache.maven.model.Repository; import org.apache.maven.settings.Mirror; import org.apache.maven.settings.Server; +import org.sonatype.aether.RepositorySystemSession; /** * @author Jason van Zyl @@ -133,6 +134,12 @@ public interface RepositorySystem */ void injectAuthentication( List repositories, List servers ); + void injectMirror( RepositorySystemSession session, List repositories ); + + void injectProxy( RepositorySystemSession session, List repositories ); + + void injectAuthentication( RepositorySystemSession session, List repositories ); + ArtifactResolutionResult resolve( ArtifactResolutionRequest request ); // Install diff --git a/maven-core/src/test/java/org/apache/maven/repository/TestRepositorySystem.java b/maven-core/src/test/java/org/apache/maven/repository/TestRepositorySystem.java index 186c2697b6..c90f08aff8 100644 --- a/maven-core/src/test/java/org/apache/maven/repository/TestRepositorySystem.java +++ b/maven-core/src/test/java/org/apache/maven/repository/TestRepositorySystem.java @@ -48,6 +48,7 @@ import org.apache.maven.settings.Server; import org.codehaus.plexus.component.annotations.Component; import org.codehaus.plexus.component.annotations.Requirement; import org.codehaus.plexus.util.FileUtils; +import org.sonatype.aether.RepositorySystemSession; /** * @author Benjamin Bentmann @@ -292,4 +293,16 @@ public class TestRepositorySystem } + public void injectMirror( RepositorySystemSession session, List repositories ) + { + } + + public void injectProxy( RepositorySystemSession session, List repositories ) + { + } + + public void injectAuthentication( RepositorySystemSession session, List repositories ) + { + } + }