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
This commit is contained in:
Jason van Zyl 2009-03-03 21:56:42 +00:00
parent c5938b5d12
commit a5fc1cfb14
10 changed files with 161 additions and 129 deletions

View File

@ -34,8 +34,12 @@ public interface ArtifactResolver
{
ArtifactResolutionResult resolve( ArtifactResolutionRequest request );
// Used directory by Surefire ...
// USED BY SUREFIRE
ArtifactResolutionResult resolveTransitively( Set<Artifact> artifacts, Artifact originatingArtifact, ArtifactRepository localRepository, List<ArtifactRepository> remoteRepositories,
ArtifactMetadataSource source, ArtifactFilter filter )
throws ArtifactResolutionException, ArtifactNotFoundException;
// USED BY REMOTE RESOURCES PLUGIN
void resolve( Artifact artifact, List<ArtifactRepository> remoteRepositories, ArtifactRepository localRepositor )
throws ArtifactResolutionException, ArtifactNotFoundException;
}

View File

@ -247,9 +247,11 @@ public class DefaultMavenExecutionRequest
this.inactiveProfiles = inactiveProfiles;
}
public void setRemoteRepositories( List<ArtifactRepository> remoteRepositories )
public MavenExecutionRequest setRemoteRepositories( List<ArtifactRepository> remoteRepositories )
{
this.remoteRepositories = remoteRepositories;
return this;
}
public void setProjectBuildingConfiguration( ProjectBuilderConfiguration projectBuildingConfiguration )

View File

@ -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<ArtifactRepository> repositories );
List<ArtifactRepository> getRemoteRepositories();
MavenExecutionRequest setRealmManager( MavenRealmManager realmManager );

View File

@ -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}
* <p/>
* - 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<Repository> 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<Mirror> i = settings.getMirrors().iterator(); i.hasNext(); )
{
Mirror mirror = i.next();
repositorySystem.addMirror( mirror.getId(), mirror.getMirrorOf(), mirror.getUrl() );
}
// <mirrors>
// <mirror>
// <id>nexus</id>
// <mirrorOf>*</mirrorOf>
// <url>http://repository.sonatype.org/content/groups/public</url>
// </mirror>
// </mirrors>
if ( request.getRemoteRepositories() != null )
{
Set<ArtifactRepository> remoteRepositoriesWithMirrors = new LinkedHashSet<ArtifactRepository>();
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<ArtifactRepository>( 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<Server> 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<Mirror> 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<Server> 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

View File

@ -21,6 +21,7 @@ under the License.
<components>
<component>
<role>org.apache.maven.artifact.factory.ArtifactFactory</role>
<role-hint>default</role-hint>
<implementation>org.apache.maven.embedder.CustomArtifactFactory</implementation>
</component>
</components>

View File

@ -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 <a href="mailto:kenney@apache.org">Kenney Westerhof</a> */
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" );

View File

@ -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;
}
}
}

View File

@ -616,4 +616,9 @@ public class LegacyMavenRepositorySystem
{
mirrorBuilder.addMirror( id, mirrorOf, url );
}
public ArtifactRepository getMirror( ArtifactRepository repository )
{
return mirrorBuilder.getMirror( repository );
}
}

View File

@ -127,4 +127,6 @@ public interface MavenRepositorySystem
// Mirrors
void addMirror( String id, String mirrorOf, String url );
ArtifactRepository getMirror( ArtifactRepository repository );
}

View File

@ -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();