From a5fc1cfb1481705deaf4baca5c99128d6dd0a13e Mon Sep 17 00:00:00 2001 From: Jason van Zyl Date: Tue, 3 Mar 2009 21:56:42 +0000 Subject: [PATCH] o fix the mirroring code o restore ArtifactResolver.resolve( artifact, remotes, local ) for the rr plugin git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@749784 13f79535-47bb-0310-9956-ffa450edef68 --- .../artifact/resolver/ArtifactResolver.java | 6 +- .../DefaultMavenExecutionRequest.java | 4 +- .../execution/MavenExecutionRequest.java | 9 + ...DefaultMavenExecutionRequestPopulator.java | 227 +++++++++--------- .../extensions/META-INF/plexus/components.xml | 1 + .../maven/embedder/TestComponentOverride.java | 23 +- .../repository/DefaultMirrorBuilder.java | 11 +- .../LegacyMavenRepositorySystem.java | 5 + .../repository/MavenRepositorySystem.java | 2 + .../maven/repository/MirrorBuilder.java | 2 +- 10 files changed, 161 insertions(+), 129 deletions(-) diff --git a/maven-compat/src/main/java/org/apache/maven/artifact/resolver/ArtifactResolver.java b/maven-compat/src/main/java/org/apache/maven/artifact/resolver/ArtifactResolver.java index 07c40c0151..20ad8bd99b 100644 --- a/maven-compat/src/main/java/org/apache/maven/artifact/resolver/ArtifactResolver.java +++ b/maven-compat/src/main/java/org/apache/maven/artifact/resolver/ArtifactResolver.java @@ -34,8 +34,12 @@ public interface ArtifactResolver { ArtifactResolutionResult resolve( ArtifactResolutionRequest request ); - // Used directory by Surefire ... + // USED BY SUREFIRE ArtifactResolutionResult resolveTransitively( Set artifacts, Artifact originatingArtifact, ArtifactRepository localRepository, List remoteRepositories, ArtifactMetadataSource source, ArtifactFilter filter ) throws ArtifactResolutionException, ArtifactNotFoundException; + + // USED BY REMOTE RESOURCES PLUGIN + void resolve( Artifact artifact, List remoteRepositories, ArtifactRepository localRepositor ) + throws ArtifactResolutionException, ArtifactNotFoundException; } \ No newline at end of file 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 ae0ee393b9..ada7793037 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 @@ -247,9 +247,11 @@ public class DefaultMavenExecutionRequest this.inactiveProfiles = inactiveProfiles; } - public void setRemoteRepositories( List remoteRepositories ) + public MavenExecutionRequest setRemoteRepositories( List remoteRepositories ) { this.remoteRepositories = remoteRepositories; + + return this; } public void setProjectBuildingConfiguration( ProjectBuilderConfiguration projectBuildingConfiguration ) diff --git a/maven-core/src/main/java/org/apache/maven/execution/MavenExecutionRequest.java b/maven-core/src/main/java/org/apache/maven/execution/MavenExecutionRequest.java index aef563fe5b..70ad23a633 100644 --- a/maven-core/src/main/java/org/apache/maven/execution/MavenExecutionRequest.java +++ b/maven-core/src/main/java/org/apache/maven/execution/MavenExecutionRequest.java @@ -211,6 +211,15 @@ public interface MavenExecutionRequest MavenExecutionRequest setGlobalSettingsFile( File globalSettingsFile ); MavenExecutionRequest addRemoteRepository( ArtifactRepository repository ); + /** + * Set a new list of remote repositories to use the execution request. This is necessary if you perform + * transformations on the remote repositories being used. For example if you replace existing repositories with + * mirrors then it's easier to just replace the whole list with a new list of transformed repositories. + * + * @param repositories + * @return + */ + MavenExecutionRequest setRemoteRepositories( List repositories ); List getRemoteRepositories(); MavenExecutionRequest setRealmManager( MavenRealmManager realmManager ); 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 e0cecc9687..ed9cea1261 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 @@ -1,29 +1,28 @@ package org.apache.maven.embedder.execution; /* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license + * agreements. See the NOTICE file distributed with this work for additional information regarding + * copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. You may obtain a + * copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. */ import java.io.File; import java.io.IOException; +import java.util.ArrayList; import java.util.Iterator; +import java.util.LinkedHashSet; import java.util.List; import java.util.Properties; +import java.util.Set; import org.apache.maven.Maven; import org.apache.maven.artifact.InvalidRepositoryException; @@ -39,8 +38,8 @@ import org.apache.maven.model.Repository; import org.apache.maven.monitor.event.DefaultEventMonitor; import org.apache.maven.monitor.event.EventMonitor; import org.apache.maven.project.DefaultProfileManager; -import org.apache.maven.project.ProfileManager; import org.apache.maven.project.ProfileActivationContext; +import org.apache.maven.project.ProfileManager; import org.apache.maven.realm.DefaultMavenRealmManager; import org.apache.maven.repository.MavenRepositorySystem; import org.apache.maven.settings.MavenSettingsBuilder; @@ -48,30 +47,22 @@ import org.apache.maven.settings.Mirror; import org.apache.maven.settings.Proxy; import org.apache.maven.settings.Server; import org.apache.maven.settings.Settings; -import org.apache.maven.settings.SettingsConfigurationException; import org.apache.maven.settings.SettingsUtils; -import org.apache.maven.wagon.repository.RepositoryPermissions; import org.codehaus.plexus.PlexusContainer; import org.codehaus.plexus.component.annotations.Component; import org.codehaus.plexus.component.annotations.Requirement; -import org.codehaus.plexus.component.repository.exception.ComponentLifecycleException; -import org.codehaus.plexus.component.repository.exception.ComponentLookupException; import org.codehaus.plexus.logging.AbstractLogEnabled; import org.codehaus.plexus.util.StringUtils; import org.sonatype.plexus.components.sec.dispatcher.SecDispatcher; import org.sonatype.plexus.components.sec.dispatcher.SecDispatcherException; /** - * Things that we deal with in this populator to ensure that we have a valid {@MavenExecutionRequest} + * Things that we deal with in this populator to ensure that we have a valid + * {@MavenExecutionRequest} *

- * - POM - * - Settings - * - Local Repository - * - Snapshot update policies - * - Repository checksum policies - * - Artifact transfer mechanism configuration - * - Eventing/Logging configuration - * - Profile manager configuration + * - POM - Settings - Local Repository - Snapshot update policies - Repository checksum policies - + * Artifact transfer mechanism configuration - Eventing/Logging configuration - Profile manager + * configuration */ @Component(role = MavenExecutionRequestPopulator.class) public class DefaultMavenExecutionRequestPopulator @@ -89,11 +80,10 @@ public class DefaultMavenExecutionRequestPopulator // 2009-02-12 Oleg: this component is defined in maven-core components.xml // because it already has another declared (not generated) component - @Requirement( hint = "maven" ) + @Requirement(hint = "maven") private SecDispatcher securityDispatcher; - public MavenExecutionRequest populateDefaults( MavenExecutionRequest request, - Configuration configuration ) + public MavenExecutionRequest populateDefaults( MavenExecutionRequest request, Configuration configuration ) throws MavenEmbedderException { eventing( request, configuration ); @@ -119,12 +109,11 @@ public class DefaultMavenExecutionRequestPopulator profileManager( request, configuration ); processSettings( request, configuration ); - + return request; } - private void reporter( MavenExecutionRequest request, - Configuration configuration ) + private void reporter( MavenExecutionRequest request, Configuration configuration ) { if ( request.getErrorReporter() == null ) { @@ -139,8 +128,7 @@ public class DefaultMavenExecutionRequestPopulator } } - private void executionProperties( MavenExecutionRequest request, - Configuration configuration ) + private void executionProperties( MavenExecutionRequest request, Configuration configuration ) { Properties requestProperties = request.getProperties(); @@ -216,7 +204,7 @@ public class DefaultMavenExecutionRequestPopulator profileManager.addProfile( profile ); // We need to convert profile repositories to artifact repositories - + for ( Iterator j = profile.getRepositories().iterator(); j.hasNext(); ) { Repository r = j.next(); @@ -235,6 +223,86 @@ public class DefaultMavenExecutionRequestPopulator } } } + + processRepositoriesInSettings( request, configuration ); + } + + private void processRepositoriesInSettings( MavenExecutionRequest request, Configuration configuration ) + throws MavenEmbedderException + { + Settings settings = request.getSettings(); + + Proxy proxy = settings.getActiveProxy(); + + if ( proxy != null ) + { + if ( proxy.getHost() == null ) + { + throw new MavenEmbedderException( "Proxy in settings.xml has no host" ); + } + + repositorySystem.addProxy( proxy.getProtocol(), proxy.getHost(), proxy.getPort(), proxy.getUsername(), proxy.getPassword(), proxy.getNonProxyHosts() ); + } + + for ( Iterator i = settings.getServers().iterator(); i.hasNext(); ) + { + Server server = (Server) i.next(); + + String pass; + String phrase; + try + { + pass = securityDispatcher.decrypt( server.getPassword() ); + phrase = securityDispatcher.decrypt( server.getPassphrase() ); + } + catch ( SecDispatcherException e ) + { + throw new MavenEmbedderException( "Error decrypting server password/passphrase.", e ); + } + + repositorySystem.addAuthenticationInfo( server.getId(), server.getUsername(), pass, server.getPrivateKey(), phrase ); + + repositorySystem.addPermissionInfo( server.getId(), server.getFilePermissions(), server.getDirectoryPermissions() ); + } + + for ( Iterator i = settings.getMirrors().iterator(); i.hasNext(); ) + { + Mirror mirror = i.next(); + + repositorySystem.addMirror( mirror.getId(), mirror.getMirrorOf(), mirror.getUrl() ); + } + + // + // + // nexus + // * + // http://repository.sonatype.org/content/groups/public + // + // + + if ( request.getRemoteRepositories() != null ) + { + Set remoteRepositoriesWithMirrors = new LinkedHashSet(); + + for ( ArtifactRepository repository : request.getRemoteRepositories() ) + { + // Check to see if we have a valid mirror for this repository + ArtifactRepository mirror = repositorySystem.getMirror( repository ); + + if ( mirror != null ) + { + // If there is a valid mirror for this repository then we'll enter the mirror as a replacement for this repository. + remoteRepositoriesWithMirrors.add( mirror ); + } + else + { + // If we have no valid mirrors for this repository we will keep this repository in the list. + remoteRepositoriesWithMirrors.add( repository ); + } + } + + request.setRemoteRepositories( new ArrayList( remoteRepositoriesWithMirrors ) ); + } } // ------------------------------------------------------------------------ @@ -304,7 +372,7 @@ public class DefaultMavenExecutionRequestPopulator try { Settings settings = settingsBuilder.buildSettings( request ); - + request.setSettings( new SettingsAdapter( request, settings ) ); } catch ( Exception e ) @@ -314,8 +382,7 @@ public class DefaultMavenExecutionRequestPopulator } } - private void localRepository( MavenExecutionRequest request, - Configuration configuration ) + private void localRepository( MavenExecutionRequest request, Configuration configuration ) throws MavenEmbedderException { // ------------------------------------------------------------------------ @@ -336,13 +403,12 @@ public class DefaultMavenExecutionRequestPopulator request.setLocalRepositoryPath( new File( request.getLocalRepository().getBasedir() ).getAbsoluteFile() ); } } - + // ------------------------------------------------------------------------ // Snapshot Policy // ------------------------------------------------------------------------ - private void snapshotPolicy( MavenExecutionRequest request, - Configuration configuration ) + private void snapshotPolicy( MavenExecutionRequest request, Configuration configuration ) { // ------------------------------------------------------------------------ // Snapshot Repository Update Policies @@ -375,8 +441,7 @@ public class DefaultMavenExecutionRequestPopulator // Checksum Policy // ------------------------------------------------------------------------ - private void checksumPolicy( MavenExecutionRequest request, - Configuration configuration ) + private void checksumPolicy( MavenExecutionRequest request, Configuration configuration ) { repositorySystem.setGlobalChecksumPolicy( request.getGlobalChecksumPolicy() ); } @@ -405,75 +470,20 @@ public class DefaultMavenExecutionRequestPopulator repositorySystem.setDownloadMonitor( request.getTransferListener() ); repositorySystem.setOnline( true ); - } - - try - { - resolveParameters( request.getSettings() ); } - catch ( Exception e ) - { - throw new MavenEmbedderException( "Unable to configure Maven for execution", e ); - } - } - - private void resolveParameters( Settings settings ) - throws ComponentLookupException, ComponentLifecycleException, SettingsConfigurationException - { - Proxy proxy = settings.getActiveProxy(); - - if ( proxy != null ) - { - if ( proxy.getHost() == null ) - { - throw new SettingsConfigurationException( "Proxy in settings.xml has no host" ); - } - - repositorySystem.addProxy( proxy.getProtocol(), proxy.getHost(), proxy.getPort(), proxy.getUsername(), proxy.getPassword(), proxy.getNonProxyHosts() ); - } - - for ( Iterator i = settings.getServers().iterator(); i.hasNext(); ) - { - Server server = i.next(); - - String pass; - String phrase; - try - { - pass = securityDispatcher.decrypt( server.getPassword() ); - phrase = securityDispatcher.decrypt( server.getPassphrase() ); - } - catch (SecDispatcherException e) - { - throw new SettingsConfigurationException( "Error decrypting server password/passphrase.", e ); - } - - repositorySystem.addAuthenticationInfo( server.getId(), server.getUsername(), pass, server.getPrivateKey(), phrase ); - - repositorySystem.addPermissionInfo( server.getId(), server.getFilePermissions(), server.getDirectoryPermissions() ); - } - - RepositoryPermissions defaultPermissions = new RepositoryPermissions(); - - for ( Iterator i = settings.getMirrors().iterator(); i.hasNext(); ) - { - Mirror mirror = i.next(); - - repositorySystem.addMirror( mirror.getId(), mirror.getMirrorOf(), mirror.getUrl() ); - } } /** * decrypt settings passwords and passphrases * * @param settings settings to process - * @throws IOException + * @throws IOException */ private void decrypt( Settings settings ) - throws IOException + throws IOException { List servers = settings.getServers(); - + if ( servers != null && !servers.isEmpty() ) { try @@ -495,7 +505,7 @@ public class DefaultMavenExecutionRequestPopulator } } - public ArtifactRepository createLocalRepository( MavenExecutionRequest request, Settings settings, Configuration configuration ) + public ArtifactRepository createLocalRepository( MavenExecutionRequest request, Settings settings, Configuration configuration ) throws MavenEmbedderException { String localRepositoryPath = null; @@ -529,13 +539,12 @@ public class DefaultMavenExecutionRequestPopulator throw new MavenEmbedderException( "Cannot create local repository.", e ); } } - + // ------------------------------------------------------------------------ // Eventing // ------------------------------------------------------------------------ - private void eventing( MavenExecutionRequest request, - Configuration configuration ) + private void eventing( MavenExecutionRequest request, Configuration configuration ) { // ------------------------------------------------------------------------ // Event Monitor/Logging diff --git a/maven-embedder/src/test/extensions/META-INF/plexus/components.xml b/maven-embedder/src/test/extensions/META-INF/plexus/components.xml index f2c7900857..c71f2a21b7 100644 --- a/maven-embedder/src/test/extensions/META-INF/plexus/components.xml +++ b/maven-embedder/src/test/extensions/META-INF/plexus/components.xml @@ -21,6 +21,7 @@ under the License. org.apache.maven.artifact.factory.ArtifactFactory + default org.apache.maven.embedder.CustomArtifactFactory diff --git a/maven-embedder/src/test/java/org/apache/maven/embedder/TestComponentOverride.java b/maven-embedder/src/test/java/org/apache/maven/embedder/TestComponentOverride.java index 886af0eada..5b0f9db52e 100644 --- a/maven-embedder/src/test/java/org/apache/maven/embedder/TestComponentOverride.java +++ b/maven-embedder/src/test/java/org/apache/maven/embedder/TestComponentOverride.java @@ -19,19 +19,17 @@ package org.apache.maven.embedder; * under the License. */ -import junit.framework.TestCase; +import java.io.File; + import org.apache.maven.artifact.factory.ArtifactFactory; import org.codehaus.plexus.PlexusContainer; +import org.codehaus.plexus.PlexusTestCase; import org.codehaus.plexus.component.repository.exception.ComponentLookupException; -import java.io.File; - /** @author Kenney Westerhof */ public class TestComponentOverride - extends TestCase + extends PlexusTestCase { - private String basedir; - private MavenEmbedder maven; protected PlexusContainer container; @@ -39,8 +37,6 @@ public class TestComponentOverride protected void setUp() throws Exception { - basedir = System.getProperty( "basedir" ); - ClassLoader loader = Thread.currentThread().getContextClassLoader(); Configuration request = new DefaultConfiguration(); @@ -49,9 +45,15 @@ public class TestComponentOverride request.setMavenEmbedderLogger( new MavenEmbedderConsoleLogger() ); - request.addExtension( new File( basedir, "src/test/extensions" ).toURI().toURL() ); + File extensions = new File( getBasedir(), "src/test/extensions" ); + + assertTrue( extensions.exists() ); + + request.addExtension( extensions.toURI().toURL() ); maven = new MavenEmbedder( request ); + + container = maven.getPlexusContainer(); } public void testComponentOverride() @@ -61,8 +63,7 @@ public class TestComponentOverride assertNotNull( factory ); - assertTrue( "Expecting " + CustomArtifactFactory.class.getName() + " but was " + factory.getClass().getName(), - CustomArtifactFactory.class.isAssignableFrom( factory.getClass() ) ); + assertTrue( "Expecting " + CustomArtifactFactory.class.getName() + " but was " + factory.getClass().getName(), CustomArtifactFactory.class.isAssignableFrom( factory.getClass() ) ); // test wheter the requirement is injected - if not, it nullpointers factory.createArtifact( "testGroupId", "testArtifactId", "testVersion", "compile", "jar" ); diff --git a/maven-repository/src/main/java/org/apache/maven/repository/DefaultMirrorBuilder.java b/maven-repository/src/main/java/org/apache/maven/repository/DefaultMirrorBuilder.java index 0af538ca53..b18405b8e1 100644 --- a/maven-repository/src/main/java/org/apache/maven/repository/DefaultMirrorBuilder.java +++ b/maven-repository/src/main/java/org/apache/maven/repository/DefaultMirrorBuilder.java @@ -42,15 +42,12 @@ public class DefaultMirrorBuilder ArtifactRepository mirror = new DefaultArtifactRepository( id, url, null ); - //System.out.println( mirror + " --> " + mirrorOf ); - - //first one must win so don't insert more. - if (!mirrors.containsKey( mirrorOf )) + if ( !mirrors.containsKey( mirrorOf ) ) { - mirrors.put( mirrorOf, mirror ); + mirrors.put( mirrorOf, mirror ); } } - + /** * This method finds a matching mirror for the selected repository. If there is an exact match, * this will be used. If there is no exact match, then the list of mirrors is examined to see if @@ -189,4 +186,6 @@ public class DefaultMirrorBuilder return false; } } + + } diff --git a/maven-repository/src/main/java/org/apache/maven/repository/LegacyMavenRepositorySystem.java b/maven-repository/src/main/java/org/apache/maven/repository/LegacyMavenRepositorySystem.java index 6236884e60..e06b2928c2 100644 --- a/maven-repository/src/main/java/org/apache/maven/repository/LegacyMavenRepositorySystem.java +++ b/maven-repository/src/main/java/org/apache/maven/repository/LegacyMavenRepositorySystem.java @@ -616,4 +616,9 @@ public class LegacyMavenRepositorySystem { mirrorBuilder.addMirror( id, mirrorOf, url ); } + + public ArtifactRepository getMirror( ArtifactRepository repository ) + { + return mirrorBuilder.getMirror( repository ); + } } diff --git a/maven-repository/src/main/java/org/apache/maven/repository/MavenRepositorySystem.java b/maven-repository/src/main/java/org/apache/maven/repository/MavenRepositorySystem.java index fb961696c8..386991d9db 100644 --- a/maven-repository/src/main/java/org/apache/maven/repository/MavenRepositorySystem.java +++ b/maven-repository/src/main/java/org/apache/maven/repository/MavenRepositorySystem.java @@ -127,4 +127,6 @@ public interface MavenRepositorySystem // Mirrors void addMirror( String id, String mirrorOf, String url ); + + ArtifactRepository getMirror( ArtifactRepository repository ); } diff --git a/maven-repository/src/main/java/org/apache/maven/repository/MirrorBuilder.java b/maven-repository/src/main/java/org/apache/maven/repository/MirrorBuilder.java index 8838812975..96069006e1 100644 --- a/maven-repository/src/main/java/org/apache/maven/repository/MirrorBuilder.java +++ b/maven-repository/src/main/java/org/apache/maven/repository/MirrorBuilder.java @@ -5,7 +5,7 @@ import org.apache.maven.artifact.repository.ArtifactRepository; public interface MirrorBuilder { ArtifactRepository getMirror( ArtifactRepository repository ); - + void addMirror( String id, String mirrorOf, String url ); void clearMirrors();