o Synced ProjectDependenciesResolver with MSHARED-126

git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@809365 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Benjamin Bentmann 2009-08-30 16:09:32 +00:00
parent 0234dc7e0e
commit 6a41005dd2
8 changed files with 399 additions and 188 deletions

View File

@ -0,0 +1,108 @@
package org.apache.maven.artifact.resolver.filter;
/*
* 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 org.apache.maven.artifact.Artifact;
/**
* Filter to only retain objects in the given artifactScope or better.
*
* @author <a href="mailto:brett@apache.org">Brett Porter</a>
* @version $Id$
*/
abstract class AbstractScopeArtifactFilter
implements ArtifactFilter
{
private boolean compileScope;
private boolean runtimeScope;
private boolean testScope;
private boolean providedScope;
private boolean systemScope;
void addScope( String scope )
{
if ( Artifact.SCOPE_COMPILE.equals( scope ) )
{
systemScope = true;
providedScope = true;
compileScope = true;
}
else if ( Artifact.SCOPE_RUNTIME.equals( scope ) )
{
compileScope = true;
runtimeScope = true;
}
else if ( Artifact.SCOPE_COMPILE_PLUS_RUNTIME.equals( scope ) )
{
systemScope = true;
providedScope = true;
compileScope = true;
runtimeScope = true;
}
else if ( Artifact.SCOPE_RUNTIME_PLUS_SYSTEM.equals( scope ) )
{
systemScope = true;
compileScope = true;
runtimeScope = true;
}
else if ( Artifact.SCOPE_TEST.equals( scope ) )
{
systemScope = true;
providedScope = true;
compileScope = true;
runtimeScope = true;
testScope = true;
}
}
public boolean include( Artifact artifact )
{
if ( Artifact.SCOPE_COMPILE.equals( artifact.getScope() ) )
{
return compileScope;
}
else if ( Artifact.SCOPE_RUNTIME.equals( artifact.getScope() ) )
{
return runtimeScope;
}
else if ( Artifact.SCOPE_TEST.equals( artifact.getScope() ) )
{
return testScope;
}
else if ( Artifact.SCOPE_PROVIDED.equals( artifact.getScope() ) )
{
return providedScope;
}
else if ( Artifact.SCOPE_SYSTEM.equals( artifact.getScope() ) )
{
return systemScope;
}
else
{
return true;
}
}
}

View File

@ -0,0 +1,95 @@
package org.apache.maven.artifact.resolver.filter;
/*
* 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.util.Collection;
import java.util.HashSet;
import java.util.Set;
/**
* Filter to only retain objects in the given scope or better. This implementation allows the accumulation of multiple
* scopes and their associated implied scopes, so that the user can filter apply a series of implication rules in a
* single step. This should be a more efficient implementation of multiple standard {@link ScopeArtifactFilter}
* instances ORed together.
*
* @author <a href="mailto:brett@apache.org">Brett Porter</a>
* @author jdcasey
* @version $Id$
*/
public class CumulativeScopeArtifactFilter
extends AbstractScopeArtifactFilter
{
private Set<String> scopes;
/**
* Create a new filter with the specified scopes and their implied scopes enabled.
*
* @param scopes The scopes to enable, along with all implied scopes, may be {@code null}.
*/
public CumulativeScopeArtifactFilter( Collection<String> scopes )
{
this.scopes = new HashSet<String>();
if ( scopes != null )
{
this.scopes.addAll( scopes );
for ( String scope : scopes )
{
addScope( scope );
}
}
}
public Set<String> getScopes()
{
return scopes;
}
@Override
public int hashCode()
{
int hash = 17;
hash = hash * 31 + scopes.hashCode();
return hash;
}
@Override
public boolean equals( Object obj )
{
if ( this == obj )
{
return true;
}
if ( !( obj instanceof CumulativeScopeArtifactFilter ) )
{
return false;
}
CumulativeScopeArtifactFilter that = (CumulativeScopeArtifactFilter) obj;
return scopes.equals( that.scopes );
}
}

View File

@ -19,8 +19,6 @@ package org.apache.maven.artifact.resolver.filter;
* under the License.
*/
import org.apache.maven.artifact.Artifact;
/**
* Filter to only retain objects in the given artifactScope or better.
*
@ -28,100 +26,16 @@ import org.apache.maven.artifact.Artifact;
* @version $Id$
*/
public class ScopeArtifactFilter
implements ArtifactFilter
extends AbstractScopeArtifactFilter
{
private final boolean compileScope;
private final boolean runtimeScope;
private final boolean testScope;
private final boolean providedScope;
private final boolean systemScope;
private final String scope;
public ScopeArtifactFilter( String scope )
{
this.scope = scope;
if ( Artifact.SCOPE_COMPILE.equals( scope ) )
{
systemScope = true;
providedScope = true;
compileScope = true;
runtimeScope = false;
testScope = false;
}
else if ( Artifact.SCOPE_RUNTIME.equals( scope ) )
{
systemScope = false;
providedScope = false;
compileScope = true;
runtimeScope = true;
testScope = false;
}
else if ( Artifact.SCOPE_COMPILE_PLUS_RUNTIME.equals( scope ) )
{
systemScope = true;
providedScope = true;
compileScope = true;
runtimeScope = true;
testScope = false;
}
else if ( Artifact.SCOPE_RUNTIME_PLUS_SYSTEM.equals( scope ) )
{
systemScope = true;
providedScope = false;
compileScope = true;
runtimeScope = true;
testScope = false;
}
else if ( Artifact.SCOPE_TEST.equals( scope ) )
{
systemScope = true;
providedScope = true;
compileScope = true;
runtimeScope = true;
testScope = true;
}
else
{
systemScope = false;
providedScope = false;
compileScope = false;
runtimeScope = false;
testScope = false;
}
}
public boolean include( Artifact artifact )
{
if ( Artifact.SCOPE_COMPILE.equals( artifact.getScope() ) )
{
return compileScope;
}
else if ( Artifact.SCOPE_RUNTIME.equals( artifact.getScope() ) )
{
return runtimeScope;
}
else if ( Artifact.SCOPE_TEST.equals( artifact.getScope() ) )
{
return testScope;
}
else if ( Artifact.SCOPE_PROVIDED.equals( artifact.getScope() ) )
{
return providedScope;
}
else if ( Artifact.SCOPE_SYSTEM.equals( artifact.getScope() ) )
{
return systemScope;
}
else
{
return true;
}
addScope( scope );
}
public String getScope()
@ -146,19 +60,20 @@ public class ScopeArtifactFilter
{
return true;
}
if ( !( obj instanceof ScopeArtifactFilter ) )
{
return false;
}
ScopeArtifactFilter other = (ScopeArtifactFilter) obj;
return equals( scope, other.scope );
}
private static boolean equals( String str1, String str2 )
private static <T> boolean equals( T str1, T str2 )
{
return str1 != null ? str1.equals( str2 ) : str2 == null;
}
}

View File

@ -20,21 +20,23 @@ package org.apache.maven;
*/
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.repository.RepositoryRequest;
import org.apache.maven.artifact.ArtifactUtils;
import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
import org.apache.maven.artifact.resolver.ArtifactResolutionException;
import org.apache.maven.artifact.resolver.ArtifactResolutionRequest;
import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
import org.apache.maven.artifact.resolver.MultipleArtifactsNotFoundException;
import org.apache.maven.artifact.resolver.ResolutionErrorHandler;
import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
import org.apache.maven.artifact.resolver.filter.OrArtifactFilter;
import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter;
import org.apache.maven.model.Dependency;
import org.apache.maven.artifact.resolver.filter.CumulativeScopeArtifactFilter;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.artifact.ProjectArtifact;
import org.apache.maven.repository.RepositorySystem;
@ -45,17 +47,32 @@ import org.codehaus.plexus.component.annotations.Requirement;
public class DefaultProjectDependenciesResolver
implements ProjectDependenciesResolver
{
@Requirement
private RepositorySystem repositorySystem;
@Requirement
private ResolutionErrorHandler resolutionErrorHandler;
public Set<Artifact> resolve( MavenProject project, Collection<String> scopes, RepositoryRequest repositoryRequest )
public Set<Artifact> resolve( MavenProject project, Collection<String> scopes, MavenSession session )
throws ArtifactResolutionException, ArtifactNotFoundException
{
{
return resolve( Collections.singleton( project ), scopes, session );
}
public Set<Artifact> resolve( Collection<? extends MavenProject> projects, Collection<String> scopes,
MavenSession session )
throws ArtifactResolutionException, ArtifactNotFoundException
{
Set<Artifact> resolved = new LinkedHashSet<Artifact>();
if ( projects == null || projects.isEmpty() )
{
return resolved;
}
/*
Logic for transitve global exclusions
List<String> exclusions = new ArrayList<String>();
@ -85,46 +102,67 @@ public class DefaultProjectDependenciesResolver
}
*/
OrArtifactFilter scopeFilter = new OrArtifactFilter();
for ( String scope : scopes )
{
scopeFilter.add( new ScopeArtifactFilter( scope ) );
}
ArtifactFilter scopeFilter = new CumulativeScopeArtifactFilter( scopes );
ArtifactFilter filter = scopeFilter;
ArtifactResolutionRequest request = new ArtifactResolutionRequest( repositoryRequest )
.setArtifact( new ProjectArtifact( project ) )
ArtifactResolutionRequest request = new ArtifactResolutionRequest()
.setResolveRoot( false )
.setResolveTransitively( true )
.setManagedVersionMap( project.getManagedVersionMap() )
.setFilter( filter );
.setFilter( filter )
.setLocalRepository( session.getLocalRepository() )
.setOffline( session.isOffline() )
.setCache( session.getRepositoryCache() );
// FIXME setTransferListener
ArtifactResolutionResult result = repositorySystem.resolve( request );
project.setArtifacts( result.getArtifacts() );
Set<String> projectIds = null;
Set<String> directDependencies = new HashSet<String>( project.getDependencies().size() * 2 );
for ( Dependency dependency : project.getDependencies() )
for ( MavenProject project : projects )
{
directDependencies.add( dependency.getManagementKey() );
}
request.setArtifact( new ProjectArtifact( project ) );
request.setManagedVersionMap( project.getManagedVersionMap() );
request.setRemoteRepositories( project.getRemoteArtifactRepositories() );
Set<Artifact> dependencyArtifacts = new LinkedHashSet<Artifact>( project.getDependencies().size() * 2 );
for ( Artifact artifact : result.getArtifacts() )
{
if ( directDependencies.contains( artifact.getDependencyConflictId() ) )
ArtifactResolutionResult result = repositorySystem.resolve( request );
try
{
dependencyArtifacts.add( artifact );
resolutionErrorHandler.throwErrors( request, result );
}
catch ( MultipleArtifactsNotFoundException e )
{
if ( projectIds == null )
{
projectIds = new HashSet<String>( projects.size() * 2 );
for ( MavenProject p : projects )
{
String key = ArtifactUtils.key( p.getGroupId(), p.getArtifactId(), p.getVersion() );
projectIds.add( key );
}
}
Collection<Artifact> missing = new HashSet<Artifact>( e.getMissingArtifacts() );
for ( Iterator<Artifact> it = missing.iterator(); it.hasNext(); )
{
String key = ArtifactUtils.key( it.next() );
if ( projectIds.contains( key ) )
{
it.remove();
}
}
if ( !missing.isEmpty() )
{
throw e;
}
}
resolved.addAll( result.getArtifacts() );
}
project.setDependencyArtifacts( dependencyArtifacts );
resolutionErrorHandler.throwErrors( request, result );
return result.getArtifacts();
return resolved;
}
}

View File

@ -23,13 +23,39 @@ import java.util.Collection;
import java.util.Set;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.repository.RepositoryRequest;
import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
import org.apache.maven.artifact.resolver.ArtifactResolutionException;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.project.MavenProject;
public interface ProjectDependenciesResolver
{
public Set<Artifact> resolve( MavenProject project, Collection<String> scopes, RepositoryRequest repositoryRequest )
/**
* Resolves the transitive dependencies of the specified project.
*
* @param project The project whose dependencies should be resolved, must not be {@code null}.
* @param scopes The dependency scopes that should be resolved, may be {@code null}.
* @param session The current build session, must not be {@code null}.
* @return The transitive dependencies of the specified project that match the requested scopes, never {@code null}.
*/
public Set<Artifact> resolve( MavenProject project, Collection<String> scopes, MavenSession session )
throws ArtifactResolutionException, ArtifactNotFoundException;
/**
* Resolves the transitive dependencies of the specified projects. Note that dependencies which can't be resolved
* from any repository but are present among the set of specified projects will not cause an exception. Instead,
* those unresolved artifacts will be returned in the result set, allowing the caller to take special care of
* artifacts that haven't been build yet.
*
* @param projects The projects whose dependencies should be resolved, may be {@code null}.
* @param scopes The dependency scopes that should be resolved, may be {@code null}.
* @param session The current build session, must not be {@code null}.
* @return The transitive dependencies of the specified projects that match the requested scopes, never {@code null}
* .
*/
public Set<Artifact> resolve( Collection<? extends MavenProject> projects, Collection<String> scopes,
MavenSession session )
throws ArtifactResolutionException, ArtifactNotFoundException;
}

View File

@ -24,6 +24,7 @@ import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
@ -35,6 +36,8 @@ import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.ArtifactUtils;
import org.apache.maven.artifact.repository.DefaultRepositoryRequest;
import org.apache.maven.artifact.repository.RepositoryRequest;
import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
import org.apache.maven.artifact.resolver.ArtifactResolutionException;
import org.apache.maven.artifact.resolver.MultipleArtifactsNotFoundException;
import org.apache.maven.execution.BuildFailure;
import org.apache.maven.execution.BuildSuccess;
@ -306,37 +309,7 @@ public class DefaultLifecycleExecutor
for ( MavenProject project : projectsToResolve )
{
repositoryRequest.setRemoteRepositories( project.getRemoteArtifactRepositories() );
try
{
projectDependenciesResolver.resolve( project, executionPlan.getRequiredResolutionScopes(),
repositoryRequest );
}
catch ( MultipleArtifactsNotFoundException e )
{
/*
* MNG-2277, the check below compensates for our bad plugin support where we ended up with
* aggregator plugins that require dependency resolution although they usually run in phases of
* the build where project artifacts haven't been assembled yet. The prime example of this is
* "mvn release:prepare".
*/
if ( projectBuild.taskSegment.aggregating
&& areAllArtifactsInReactor( session.getProjects(), e.getMissingArtifacts() ) )
{
logger.warn( "The following artifacts could not be resolved at this point of the build"
+ " but seem to be part of the reactor:" );
for ( Artifact artifact : e.getMissingArtifacts() )
{
logger.warn( "o " + artifact.getId() );
}
logger.warn( "Try running the build up to the lifecycle phase \"package\"" );
}
else
{
throw e;
}
}
resolveProjectDependencies( project, executionPlan, session, projectBuild.taskSegment.aggregating );
}
for ( MojoExecution mojoExecution : executionPlan.getExecutions() )
@ -391,6 +364,64 @@ public class DefaultLifecycleExecutor
fireEvent( session, null, LifecycleEventCatapult.SESSION_ENDED );
}
private void resolveProjectDependencies( MavenProject project, MavenExecutionPlan executionPlan,
MavenSession session, boolean aggregating )
throws ArtifactResolutionException, ArtifactNotFoundException
{
Set<Artifact> artifacts;
try
{
Collection<String> scopesToResolve = executionPlan.getRequiredResolutionScopes();
artifacts = projectDependenciesResolver.resolve( project, scopesToResolve, session );
}
catch ( MultipleArtifactsNotFoundException e )
{
/*
* MNG-2277, the check below compensates for our bad plugin support where we ended up with aggregator
* plugins that require dependency resolution although they usually run in phases of the build where project
* artifacts haven't been assembled yet. The prime example of this is "mvn release:prepare".
*/
if ( aggregating && areAllArtifactsInReactor( session.getProjects(), e.getMissingArtifacts() ) )
{
logger.warn( "The following artifacts could not be resolved at this point of the build"
+ " but seem to be part of the reactor:" );
for ( Artifact artifact : e.getMissingArtifacts() )
{
logger.warn( "o " + artifact.getId() );
}
logger.warn( "Try running the build up to the lifecycle phase \"package\"" );
artifacts = new LinkedHashSet<Artifact>( e.getResolvedArtifacts() );
}
else
{
throw e;
}
}
project.setArtifacts( artifacts );
Set<String> directDependencies = new HashSet<String>( project.getDependencies().size() * 2 );
for ( Dependency dependency : project.getDependencies() )
{
directDependencies.add( dependency.getManagementKey() );
}
Set<Artifact> dependencyArtifacts = new LinkedHashSet<Artifact>( project.getDependencies().size() * 2 );
for ( Artifact artifact : artifacts )
{
if ( directDependencies.contains( artifact.getDependencyConflictId() ) )
{
dependencyArtifacts.add( artifact );
}
}
project.setDependencyArtifacts( dependencyArtifacts );
}
private boolean areAllArtifactsInReactor( Collection<MavenProject> projects, Collection<Artifact> artifacts )
{
Set<String> projectKeys = new HashSet<String>( projects.size() * 2 );

View File

@ -126,6 +126,8 @@ public abstract class AbstractCoreMavenComponentTestCase
else
{
project = createStubMavenProject();
project.setRemoteArtifactRepositories( request.getRemoteRepositories() );
project.setPluginArtifactRepositories( request.getPluginArtifactRepositories() );
}
MavenSession session = new MavenSession( getContainer(), request, new DefaultMavenExecutionResult(), project );
@ -166,7 +168,12 @@ public abstract class AbstractCoreMavenComponentTestCase
protected class ProjectBuilder
{
private MavenProject project;
public ProjectBuilder( MavenProject project )
{
this.project = project;
}
public ProjectBuilder( String groupId, String artifactId, String version )
{
Model model = new Model();

View File

@ -7,9 +7,6 @@ import java.util.Properties;
import java.util.Set;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.InvalidRepositoryException;
import org.apache.maven.artifact.repository.DefaultRepositoryRequest;
import org.apache.maven.artifact.repository.RepositoryRequest;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Exclusion;
import org.apache.maven.project.MavenProject;
@ -41,46 +38,40 @@ public class ProjectDependenciesResolverTest
return "src/test/projects/project-dependencies-resolver";
}
protected RepositoryRequest getRepositoryRequest()
throws InvalidRepositoryException
{
RepositoryRequest request = new DefaultRepositoryRequest();
request.setLocalRepository( getLocalRepository() );
request.setRemoteRepositories( getRemoteRepositories() );
return request;
}
public void testExclusionsInDependencies()
throws Exception
{
MavenSession session = createMavenSession( null );
MavenProject project = session.getCurrentProject();
Exclusion exclusion = new Exclusion();
exclusion.setGroupId( "commons-lang" );
exclusion.setArtifactId( "commons-lang" );
MavenProject project = new ProjectBuilder( "org.apache.maven", "project-test", "1.0" )
.addDependency( "org.apache.maven.its", "maven-core-it-support", "1.3", Artifact.SCOPE_RUNTIME, exclusion )
.get();
new ProjectBuilder( project ).addDependency( "org.apache.maven.its", "maven-core-it-support", "1.3",
Artifact.SCOPE_RUNTIME, exclusion );
Set<Artifact> artifactDependencies =
resolver.resolve( project, Collections.singleton( Artifact.SCOPE_COMPILE ), getRepositoryRequest() );
resolver.resolve( project, Collections.singleton( Artifact.SCOPE_COMPILE ), session );
assertEquals( 0, artifactDependencies.size() );
artifactDependencies =
resolver.resolve( project, Collections.singleton( Artifact.SCOPE_RUNTIME ), getRepositoryRequest() );
artifactDependencies = resolver.resolve( project, Collections.singleton( Artifact.SCOPE_RUNTIME ), session );
assertEquals( 1, artifactDependencies.size() );
assertEquals( "maven-core-it-support" , artifactDependencies.iterator().next().getArtifactId() );
assertEquals( "maven-core-it-support", artifactDependencies.iterator().next().getArtifactId() );
}
public void testSystemScopeDependencies()
throws Exception
{
MavenProject project = new ProjectBuilder( "org.apache.maven", "project-test", "1.0" )
.addDependency( "com.mycompany", "system-dependency", "1.0", Artifact.SCOPE_SYSTEM, new File( getBasedir(), "pom.xml" ).getAbsolutePath() )
.get();
MavenSession session = createMavenSession( null );
MavenProject project = session.getCurrentProject();
new ProjectBuilder( project )
.addDependency( "com.mycompany", "system-dependency", "1.0", Artifact.SCOPE_SYSTEM, new File( getBasedir(), "pom.xml" ).getAbsolutePath() );
Set<Artifact> artifactDependencies =
resolver.resolve( project, Collections.singleton( Artifact.SCOPE_COMPILE ), getRepositoryRequest() );
assertEquals( 1, artifactDependencies.size() );
resolver.resolve( project, Collections.singleton( Artifact.SCOPE_COMPILE ), session );
assertEquals( 1, artifactDependencies.size() );
}
public void testSystemScopeDependencyIsPresentInTheCompileClasspathElements()
@ -94,7 +85,7 @@ public class ProjectDependenciesResolverTest
MavenSession session = createMavenSession( pom, eps );
MavenProject project = session.getCurrentProject();
resolver.resolve( project, Collections.singleton( Artifact.SCOPE_COMPILE ), getRepositoryRequest() );
project.setArtifacts( resolver.resolve( project, Collections.singleton( Artifact.SCOPE_COMPILE ), session ) );
List<String> elements = project.getCompileClasspathElements();
assertEquals( 2, elements.size() );