diff --git a/maven-compat/src/main/java/org/apache/maven/repository/LegacyRepositorySystem.java b/maven-compat/src/main/java/org/apache/maven/repository/LegacyRepositorySystem.java index 9884cd52e8..3dab3e2ce6 100644 --- a/maven-compat/src/main/java/org/apache/maven/repository/LegacyRepositorySystem.java +++ b/maven-compat/src/main/java/org/apache/maven/repository/LegacyRepositorySystem.java @@ -85,9 +85,6 @@ public class LegacyRepositorySystem @Requirement private PlexusContainer plexus; - // TODO: move this out, the component needs to be stateless for safe reuse - private Map proxies = new HashMap(); - public Artifact createArtifact( String groupId, String artifactId, String version, String scope, String type ) { return artifactFactory.createArtifact( groupId, artifactId, version, scope, type ); @@ -379,6 +376,8 @@ public class LegacyRepositorySystem effectiveRepository.setAuthentication( aliasedRepo.getAuthentication() ); + effectiveRepository.setProxy( aliasedRepo.getProxy() ); + effectiveRepositories.add( effectiveRepository ); } @@ -531,6 +530,51 @@ public class LegacyRepositorySystem } } + private org.apache.maven.settings.Proxy getProxy( ArtifactRepository repository, + List proxies ) + { + if ( proxies != null && repository.getProtocol() != null ) + { + for ( org.apache.maven.settings.Proxy proxy : proxies ) + { + if ( proxy.isActive() && repository.getProtocol().equalsIgnoreCase( proxy.getProtocol() ) ) + { + return proxy; + } + } + } + + return null; + } + + public void injectProxy( List repositories, List proxies ) + { + if ( repositories != null ) + { + for ( ArtifactRepository repository : repositories ) + { + org.apache.maven.settings.Proxy proxy = getProxy( repository, proxies ); + + if ( proxy != null ) + { + Proxy p = new Proxy(); + p.setHost( proxy.getHost() ); + p.setProtocol( proxy.getProtocol() ); + p.setPort( proxy.getPort() ); + p.setNonProxyHosts( proxy.getNonProxyHosts() ); + p.setUserName( proxy.getUsername() ); + p.setPassword( proxy.getPassword() ); + + repository.setProxy( p ); + } + else + { + repository.setProxy( null ); + } + } + } + } + public MetadataResolutionResult resolveMetadata( MetadataResolutionRequest request ) { @@ -612,25 +656,7 @@ public class LegacyRepositorySystem ArtifactRepository artifactRepository = artifactRepositoryFactory.createArtifactRepository( repositoryId, url, repositoryLayout, snapshots, releases ); - Proxy proxy = proxies.get( artifactRepository.getProtocol() ); - - if ( proxy != null ) - { - artifactRepository.setProxy( proxy ); - } - return artifactRepository; } - public void addProxy( String protocol, String host, int port, String username, String password, String nonProxyHosts ) - { - Proxy proxy = new Proxy(); - proxy.setHost( host ); - proxy.setProtocol( protocol ); - proxy.setPort( port ); - proxy.setNonProxyHosts( nonProxyHosts ); - proxy.setUserName( username ); - proxy.setPassword( password ); - proxies.put( protocol, proxy ); - } } diff --git a/maven-compat/src/main/java/org/apache/maven/repository/RepositorySystem.java b/maven-compat/src/main/java/org/apache/maven/repository/RepositorySystem.java index b34b4360fe..36cce326b1 100644 --- a/maven-compat/src/main/java/org/apache/maven/repository/RepositorySystem.java +++ b/maven-compat/src/main/java/org/apache/maven/repository/RepositorySystem.java @@ -103,11 +103,22 @@ public interface RepositorySystem */ void injectMirror( List repositories, List mirrors ); + /** + * Injects the proxy information into the specified repositories. For each repository that is matched by a proxy, + * its proxy data will be set accordingly. Repositories without a matching proxy will have their proxy cleared. + * Note: This method must be called after {@link #injectMirror(List, List)} or the repositories will end up + * with the wrong proxies. + * + * @param repositories The repositories into which to inject the proxy information, may be {@code null}. + * @param proxies The available proxies, may be {@code null}. + */ + void injectProxy( List repositories, List proxies ); + /** * Injects the authentication information into the specified repositories. For each repository that is matched by a * server, its credentials will be updated to match the values from the server specification. Repositories without a - * matching server will have their credentials cleared. Note: This method must be called before - * {@link #injectAuthentication(List, List)} or the repositories will end up with the wrong credentials. + * matching server will have their credentials cleared. Note: This method must be called after + * {@link #injectMirror(List, List)} or the repositories will end up with the wrong credentials. * * @param repositories The repositories into which to inject the authentication information, may be {@code null}. * @param servers The available servers, may be {@code null}. @@ -133,5 +144,4 @@ public interface RepositorySystem void retrieve( ArtifactRepository repository, File destination, String remotePath, TransferListener downloadMonitor ) throws TransferFailedException, ResourceDoesNotExistException; - void addProxy( String protocol, String host, int port, String username, String password, String nonProxyHosts ); } diff --git a/maven-core/src/main/java/org/apache/maven/execution/DefaultMavenExecutionRequest.java b/maven-core/src/main/java/org/apache/maven/execution/DefaultMavenExecutionRequest.java index dda29ce142..d238def9d9 100644 --- a/maven-core/src/main/java/org/apache/maven/execution/DefaultMavenExecutionRequest.java +++ b/maven-core/src/main/java/org/apache/maven/execution/DefaultMavenExecutionRequest.java @@ -967,6 +967,7 @@ public class DefaultMavenExecutionRequest projectBuildingRequest.setOffline( isOffline() ); projectBuildingRequest.setServers( getServers() ); projectBuildingRequest.setMirrors( getMirrors() ); + projectBuildingRequest.setProxies( getProxies() ); projectBuildingRequest.setActiveProfileIds( getActiveProfiles() ); projectBuildingRequest.setInactiveProfileIds( getInactiveProfiles() ); projectBuildingRequest.setProfiles( getProfiles() ); diff --git a/maven-core/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java b/maven-core/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java index 93622e9ebe..e2c4917326 100644 --- a/maven-core/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java +++ b/maven-core/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java @@ -86,6 +86,7 @@ public class DefaultMavenProjectBuilder { ArtifactRepository repo = repositorySystem.buildArtifactRepository( (Repository) repository ); repositorySystem.injectMirror( Arrays.asList( repo ), configuration.getMirrors() ); + repositorySystem.injectProxy( Arrays.asList( repo ), configuration.getProxies() ); repositorySystem.injectAuthentication( Arrays.asList( repo ), configuration.getServers() ); repos.add( repo ); } 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 b1f7f74ef0..f74fd64412 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 @@ -100,6 +100,8 @@ public class DefaultProjectBuildingHelper repositorySystem.injectMirror( artifactRepositories, request.getMirrors() ); + repositorySystem.injectProxy( artifactRepositories, request.getProxies() ); + repositorySystem.injectAuthentication( artifactRepositories, request.getServers() ); if ( externalRepositories != null ) 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 bf33132f0f..6693a1bd60 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 @@ -30,6 +30,7 @@ import org.apache.maven.model.Profile; import org.apache.maven.model.building.ModelBuildingRequest; import org.apache.maven.model.building.ModelEventListener; import org.apache.maven.settings.Mirror; +import org.apache.maven.settings.Proxy; import org.apache.maven.settings.Server; public class DefaultProjectBuildingRequest @@ -50,6 +51,8 @@ public class DefaultProjectBuildingRequest private List mirrors; + private List proxies; + private List listeners; private MavenProject project; @@ -87,6 +90,7 @@ public class DefaultProjectBuildingRequest pluginArtifactRepositories = new ArrayList(); servers = new ArrayList(); mirrors = new ArrayList(); + proxies = new ArrayList(); } public MavenProject getProject() @@ -210,6 +214,25 @@ public class DefaultProjectBuildingRequest return mirrors; } + public ProjectBuildingRequest setProxies( List proxies ) + { + if ( proxies != null ) + { + this.proxies = new ArrayList( proxies ); + } + else + { + this.proxies.clear(); + } + + return this; + } + + public List getProxies() + { + return proxies; + } + public Properties getSystemProperties() { return systemProperties; 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 be54bdbde5..aca73c28d3 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 @@ -28,6 +28,7 @@ import org.apache.maven.artifact.repository.RepositoryCache; import org.apache.maven.model.Profile; import org.apache.maven.model.building.ModelBuildingRequest; import org.apache.maven.settings.Mirror; +import org.apache.maven.settings.Proxy; import org.apache.maven.settings.Server; public interface ProjectBuildingRequest @@ -61,6 +62,10 @@ public interface ProjectBuildingRequest List getMirrors(); + ProjectBuildingRequest setProxies( List proxies ); + + List getProxies(); + /** * Sets the system properties to use for interpolation and profile activation. The system properties are collected * from the runtime environment like {@link System#getProperties()} and environment variables. diff --git a/maven-core/src/main/java/org/apache/maven/project/RepositoryModelResolver.java b/maven-core/src/main/java/org/apache/maven/project/RepositoryModelResolver.java index bc9b563a7e..0412ad92d6 100644 --- a/maven-core/src/main/java/org/apache/maven/project/RepositoryModelResolver.java +++ b/maven-core/src/main/java/org/apache/maven/project/RepositoryModelResolver.java @@ -102,6 +102,8 @@ class RepositoryModelResolver repositorySystem.injectMirror( Arrays.asList( repo ), projectBuildingRequest.getMirrors() ); + repositorySystem.injectProxy( Arrays.asList( repo ), projectBuildingRequest.getProxies() ); + repositorySystem.injectAuthentication( Arrays.asList( repo ), projectBuildingRequest.getServers() ); remoteRepositories.add( 0, repo ); diff --git a/maven-embedder/src/main/java/org/apache/maven/embedder/execution/DefaultMavenExecutionRequestPopulator.java b/maven-embedder/src/main/java/org/apache/maven/embedder/execution/DefaultMavenExecutionRequestPopulator.java index be58d550c6..269380fb77 100644 --- a/maven-embedder/src/main/java/org/apache/maven/embedder/execution/DefaultMavenExecutionRequestPopulator.java +++ b/maven-embedder/src/main/java/org/apache/maven/embedder/execution/DefaultMavenExecutionRequestPopulator.java @@ -194,23 +194,13 @@ public class DefaultMavenExecutionRequestPopulator // // - Proxy activeProxy = settings.getActiveProxy(); - - if ( activeProxy != null ) - { - if ( activeProxy.getHost() == null ) - { - throw new MavenEmbedderException( "Proxy in settings.xml has no host" ); - } - - String password = decrypt( activeProxy.getPassword(), "password for proxy " + activeProxy.getId() ); - - repositorySystem.addProxy( activeProxy.getProtocol(), activeProxy.getHost(), activeProxy.getPort(), - activeProxy.getUsername(), password, activeProxy.getNonProxyHosts() ); - } - for ( Proxy proxy : settings.getProxies() ) { + if ( !proxy.isActive() ) + { + continue; + } + proxy = proxy.clone(); String password = decrypt( proxy.getPassword(), "password for proxy " + proxy.getId() ); @@ -247,11 +237,13 @@ public class DefaultMavenExecutionRequestPopulator } repositorySystem.injectMirror( request.getRemoteRepositories(), request.getMirrors() ); + repositorySystem.injectProxy( request.getRemoteRepositories(), request.getProxies() ); repositorySystem.injectAuthentication( request.getRemoteRepositories(), request.getServers() ); request.setRemoteRepositories( repositorySystem.getEffectiveRepositories( request.getRemoteRepositories() ) ); repositorySystem.injectMirror( request.getPluginArtifactRepositories(), request.getMirrors() ); + repositorySystem.injectProxy( request.getPluginArtifactRepositories(), request.getProxies() ); repositorySystem.injectAuthentication( request.getPluginArtifactRepositories(), request.getServers() ); request.setPluginArtifactRepositories( repositorySystem.getEffectiveRepositories( request.getPluginArtifactRepositories() ) );