[MNG-4331] Add new mojo annotation @requiresDependencyCollection to grab dependency tree without files

git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@809431 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Benjamin Bentmann 2009-08-30 22:28:16 +00:00
parent a0d6f81a20
commit 6d604c17c7
16 changed files with 188 additions and 41 deletions

View File

@ -54,7 +54,9 @@ public class ArtifactResolutionRequest
private RepositoryCache cache;
private ArtifactFilter filter;
private ArtifactFilter collectionFilter;
private ArtifactFilter resolutionFilter;
// Needs to go away
private List<ResolutionListener> listeners = new ArrayList<ResolutionListener>();
@ -131,14 +133,39 @@ public ArtifactResolutionRequest setRemoteRepositories( List<ArtifactRepository>
return this;
}
public ArtifactFilter getFilter()
/**
* Gets the artifact filter that controls traversal of the dependency graph.
*
* @return The filter used to determine which of the artifacts in the dependency graph should be traversed or
* {@code null} to collect all transitive dependencies.
*/
public ArtifactFilter getCollectionFilter()
{
return filter;
return collectionFilter;
}
public ArtifactResolutionRequest setFilter( ArtifactFilter filter )
public ArtifactResolutionRequest setCollectionFilter( ArtifactFilter filter )
{
this.filter = filter;
this.collectionFilter = filter;
return this;
}
/**
* Gets the artifact filter that controls downloading of artifact files. This filter operates on those artifacts
* that have been included by the {@link #getCollectionFilter()}.
*
* @return The filter used to determine which of the artifacts should have their files resolved or {@code null} to
* resolve the files for all collected artifacts.
*/
public ArtifactFilter getResolutionFilter()
{
return resolutionFilter;
}
public ArtifactResolutionRequest setResolutionFilter( ArtifactFilter filter )
{
this.resolutionFilter = filter;
return this;
}

View File

@ -339,7 +339,7 @@ public ArtifactResolutionResult resolveTransitively( Set<Artifact> artifacts, Ar
.setManagedVersionMap( managedVersions )
.setLocalRepository( localRepository )
.setRemoteRepositories( remoteRepositories )
.setFilter( filter )
.setCollectionFilter( filter )
.setListeners( listeners );
return resolveWithExceptions( request );
@ -379,7 +379,8 @@ private ArtifactResolutionResult resolve( ArtifactResolutionRequest request, boo
Set<Artifact> artifacts = request.getArtifactDependencies();
Map managedVersions = request.getManagedVersionMap();
List<ResolutionListener> listeners = request.getListeners();
ArtifactFilter filter = request.getFilter();
ArtifactFilter collectionFilter = request.getCollectionFilter();
ArtifactFilter resolutionFilter = request.getResolutionFilter();
//TODO: hack because metadata isn't generated in m2e correctly and i want to run the maven i have in the workspace
if ( source == null )
@ -489,7 +490,9 @@ private ArtifactResolutionResult resolve( ArtifactResolutionRequest request, boo
}
// After the collection we will have the artifact object in the result but they will not be resolved yet.
result = artifactCollector.collect( artifacts, rootArtifact, managedVersions, request, source, filter, listeners, null );
result =
artifactCollector.collect( artifacts, rootArtifact, managedVersions, request, source, collectionFilter,
listeners, null );
// We have metadata retrieval problems, or there are cycles that have been detected
// so we give this back to the calling code and let them deal with this information
@ -505,9 +508,12 @@ private ArtifactResolutionResult resolve( ArtifactResolutionRequest request, boo
for ( Artifact artifact : result.getArtifacts() )
{
try
{
if ( resolutionFilter == null || resolutionFilter.include( artifact ) )
{
resolve( artifact, request, request.getTransferListener(), false );
}
}
catch ( ArtifactNotFoundException anfe )
{
// These are cases where the artifact just isn't present in any of the remote repositories

View File

@ -41,7 +41,7 @@ abstract class AbstractScopeArtifactFilter
private boolean systemScope;
void addScope( String scope )
void addScopeInternal( String scope )
{
if ( Artifact.SCOPE_COMPILE.equals( scope ) )
{

View File

@ -48,10 +48,31 @@ public CumulativeScopeArtifactFilter( Collection<String> scopes )
{
this.scopes = new HashSet<String>();
addScopes( scopes );
}
/**
* Creates a new filter that combines the specified filters.
*
* @param filters The filters to combine, may be {@code null}.
*/
public CumulativeScopeArtifactFilter( CumulativeScopeArtifactFilter... filters )
{
this.scopes = new HashSet<String>();
if ( filters != null )
{
for ( CumulativeScopeArtifactFilter filter : filters )
{
addScopes( filter.getScopes() );
}
}
}
private void addScopes( Collection<String> scopes )
{
if ( scopes != null )
{
this.scopes.addAll( scopes );
for ( String scope : scopes )
{
addScope( scope );
@ -59,6 +80,13 @@ public CumulativeScopeArtifactFilter( Collection<String> scopes )
}
}
private void addScope( String scope )
{
this.scopes.add( scope );
addScopeInternal( scope );
}
public Set<String> getScopes()
{
return scopes;

View File

@ -35,7 +35,7 @@ public ScopeArtifactFilter( String scope )
{
this.scope = scope;
addScope( scope );
addScopeInternal( scope );
}
public String getScope()

View File

@ -34,7 +34,6 @@
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.CumulativeScopeArtifactFilter;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.project.MavenProject;
@ -54,15 +53,29 @@ public class DefaultProjectDependenciesResolver
@Requirement
private ResolutionErrorHandler resolutionErrorHandler;
public Set<Artifact> resolve( MavenProject project, Collection<String> scopes, MavenSession session )
public Set<Artifact> resolve( MavenProject project, Collection<String> scopesToResolve, MavenSession session )
throws ArtifactResolutionException, ArtifactNotFoundException
{
return resolve( Collections.singleton( project ), scopes, session );
return resolve( Collections.singleton( project ), scopesToResolve, session );
}
public Set<Artifact> resolve( Collection<? extends MavenProject> projects, Collection<String> scopes,
public Set<Artifact> resolve( MavenProject project, Collection<String> scopesToCollect,
Collection<String> scopesToResolve, MavenSession session )
throws ArtifactResolutionException, ArtifactNotFoundException
{
return resolve( Collections.singleton( project ), scopesToCollect, scopesToResolve, session );
}
public Set<Artifact> resolve( Collection<? extends MavenProject> projects, Collection<String> scopesToResolve,
MavenSession session )
throws ArtifactResolutionException, ArtifactNotFoundException
{
return resolve( projects, null, scopesToResolve, session );
}
public Set<Artifact> resolve( Collection<? extends MavenProject> projects, Collection<String> scopesToCollect,
Collection<String> scopesToResolve, MavenSession session )
throws ArtifactResolutionException, ArtifactNotFoundException
{
Set<Artifact> resolved = new LinkedHashSet<Artifact>();
@ -102,14 +115,16 @@ public Set<Artifact> resolve( Collection<? extends MavenProject> projects, Colle
}
*/
ArtifactFilter scopeFilter = new CumulativeScopeArtifactFilter( scopes );
CumulativeScopeArtifactFilter resolutionScopeFilter = new CumulativeScopeArtifactFilter( scopesToResolve );
ArtifactFilter filter = scopeFilter;
CumulativeScopeArtifactFilter collectionScopeFilter = new CumulativeScopeArtifactFilter( scopesToCollect );
collectionScopeFilter = new CumulativeScopeArtifactFilter( collectionScopeFilter, resolutionScopeFilter );
ArtifactResolutionRequest request = new ArtifactResolutionRequest()
.setResolveRoot( false )
.setResolveTransitively( true )
.setFilter( filter )
.setCollectionFilter( collectionScopeFilter )
.setResolutionFilter( resolutionScopeFilter )
.setLocalRepository( session.getLocalRepository() )
.setOffline( session.isOffline() )
.setCache( session.getRepositoryCache() );

View File

@ -35,11 +35,24 @@ public interface ProjectDependenciesResolver
* 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 scopesToResolve 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 )
public Set<Artifact> resolve( MavenProject project, Collection<String> scopesToResolve, MavenSession session )
throws ArtifactResolutionException, ArtifactNotFoundException;
/**
* Resolves the transitive dependencies of the specified project.
*
* @param project The project whose dependencies should be resolved, must not be {@code null}.
* @param scopesToCollect The dependency scopes that should be collected, may be {@code null}.
* @param scopesToResolve The dependency scopes that should be collected and also 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> scopesToCollect,
Collection<String> scopesToResolve, MavenSession session )
throws ArtifactResolutionException, ArtifactNotFoundException;
/**

View File

@ -371,7 +371,9 @@ private void resolveProjectDependencies( MavenProject project, MavenExecutionPla
{
Collection<String> scopesToResolve = executionPlan.getRequiredResolutionScopes();
artifacts = projectDependenciesResolver.resolve( project, scopesToResolve, session );
Collection<String> scopesToCollect = executionPlan.getRequiredCollectionScopes();
artifacts = projectDependenciesResolver.resolve( project, scopesToCollect, scopesToResolve, session );
}
catch ( MultipleArtifactsNotFoundException e )
{
@ -672,6 +674,8 @@ private MavenExecutionPlan calculateExecutionPlan( MavenSession session, MavenPr
Set<String> requiredDependencyResolutionScopes = new TreeSet<String>();
Set<String> requiredDependencyCollectionScopes = new TreeSet<String>();
for ( Object task : taskSegment.tasks )
{
if ( task instanceof GoalTask )
@ -721,10 +725,12 @@ else if ( task instanceof LifecycleTask )
calculateForkedExecutions( mojoExecution, session, project, new HashSet<MojoDescriptor>() );
collectDependencyResolutionScopes( requiredDependencyResolutionScopes, mojoExecution );
collectDependencyRequirements( requiredDependencyResolutionScopes, requiredDependencyCollectionScopes,
mojoExecution );
}
return new MavenExecutionPlan( mojoExecutions, requiredDependencyResolutionScopes );
return new MavenExecutionPlan( mojoExecutions, requiredDependencyResolutionScopes,
requiredDependencyCollectionScopes );
}
private List<TaskSegment> calculateTaskSegments( MavenSession session, List<String> tasks )
@ -887,21 +893,32 @@ private RepositoryRequest getRepositoryRequest( MavenSession session, MavenProje
return request;
}
private void collectDependencyResolutionScopes( Collection<String> requiredDependencyResolutionScopes,
private void collectDependencyRequirements( Collection<String> requiredDependencyResolutionScopes,
Collection<String> requiredDependencyCollectionScopes,
MojoExecution mojoExecution )
{
String requiredDependencyResolutionScope = mojoExecution.getMojoDescriptor().isDependencyResolutionRequired();
MojoDescriptor mojoDescriptor = mojoExecution.getMojoDescriptor();
String requiredDependencyResolutionScope = mojoDescriptor.getDependencyResolutionRequired();
if ( StringUtils.isNotEmpty( requiredDependencyResolutionScope ) )
{
requiredDependencyResolutionScopes.add( requiredDependencyResolutionScope );
}
String requiredDependencyCollectionScope = mojoDescriptor.getDependencyCollectionRequired();
if ( StringUtils.isNotEmpty( requiredDependencyCollectionScope ) )
{
requiredDependencyCollectionScopes.add( requiredDependencyCollectionScope );
}
for ( List<MojoExecution> forkedExecutions : mojoExecution.getForkedExecutions().values() )
{
for ( MojoExecution forkedExecution : forkedExecutions )
{
collectDependencyResolutionScopes( requiredDependencyResolutionScopes, forkedExecution );
collectDependencyRequirements( requiredDependencyResolutionScopes,
requiredDependencyCollectionScopes, forkedExecution );
}
}
}

View File

@ -38,10 +38,15 @@ public class MavenExecutionPlan
/** For project dependency resolution, the scopes of resolution required if any. */
private Set<String> requiredDependencyResolutionScopes;
public MavenExecutionPlan( List<MojoExecution> executions, Set<String> requiredDependencyResolutionScopes )
/** For project dependency collection, the scopes of collection required if any. */
private Set<String> requiredDependencyCollectionScopes;
public MavenExecutionPlan( List<MojoExecution> executions, Set<String> requiredDependencyResolutionScopes,
Set<String> requiredDependencyCollectionScopes )
{
this.executions = executions;
this.requiredDependencyResolutionScopes = requiredDependencyResolutionScopes;
this.requiredDependencyCollectionScopes = requiredDependencyCollectionScopes;
}
public List<MojoExecution> getExecutions()
@ -53,4 +58,10 @@ public Set<String> getRequiredResolutionScopes()
{
return requiredDependencyResolutionScopes;
}
public Set<String> getRequiredCollectionScopes()
{
return requiredDependencyCollectionScopes;
}
}

View File

@ -397,7 +397,7 @@ protected List<Artifact> resolvePluginArtifacts( Plugin plugin, Artifact pluginA
ArtifactResolutionRequest request = new ArtifactResolutionRequest( repositoryRequest );
request.setArtifact( pluginArtifact );
request.setArtifactDependencies( overrideArtifacts );
request.setFilter( filter );
request.setCollectionFilter( filter );
request.setResolveRoot( true );
request.setResolveTransitively( true );

View File

@ -1861,14 +1861,9 @@ private void deepCopy( MavenProject project )
}
private void addArtifactPath( Artifact artifact, List<String> classpath )
throws DependencyResolutionRequiredException
{
File file = artifact.getFile();
if ( file == null )
{
throw new DependencyResolutionRequiredException( artifact );
}
else
if ( file != null )
{
classpath.add( file.getPath() );
}

View File

@ -62,7 +62,7 @@ public MavenExecutionPlan calculateExecutionPlan( MavenSession session, String..
throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException,
MojoNotFoundException
{
return new MavenExecutionPlan( Collections.<MojoExecution> emptyList(), null );
return new MavenExecutionPlan( Collections.<MojoExecution> emptyList(), null, null );
}
public void execute( MavenSession session )

View File

@ -93,6 +93,9 @@ public class MojoDescriptor
/** Specify the required dependencies in a specified scope */
private String dependencyResolutionRequired = null;
/** The scope of (transitive) dependencies that should be collected but not resolved. */
private String dependencyCollectionRequired;
/** By default, the Mojo needs a Maven project to be executed */
private boolean projectRequired = true;
@ -235,15 +238,38 @@ public void setDependencyResolutionRequired( String requiresDependencyResolution
this.dependencyResolutionRequired = requiresDependencyResolution;
}
public String getDependencyResolutionRequired()
{
return dependencyResolutionRequired;
}
/**
* @return the required dependencies in a specified scope
* @TODO the name is not intelligible
*/
@Deprecated
public String isDependencyResolutionRequired()
{
return dependencyResolutionRequired;
}
public void setDependencyCollectionRequired( String requiresDependencyCollection )
{
this.dependencyCollectionRequired = requiresDependencyCollection;
}
/**
* Gets the scope of (transitive) dependencies that should be collected. Dependency collection refers to the process
* of calculating the complete dependency tree in terms of artifact coordinates. In contrast to dependency
* resolution, this does not include the download of the files for the dependency artifacts.
*
* @return The scope of (transitive) dependencies that should be collected or {@code null} if none.
*/
public String getDependencyCollectionRequired()
{
return dependencyCollectionRequired;
}
// ----------------------------------------------------------------------
// Project requirement
// ----------------------------------------------------------------------

View File

@ -195,11 +195,18 @@ public MojoDescriptor buildComponentDescriptor( PlexusConfiguration c, PluginDes
mojo.setDescription( c.getChild( "description" ).getValue() );
String dependencyResolution = c.getChild( "requiresDependencyResolution" ).getValue();
PlexusConfiguration dependencyResolution = c.getChild( "requiresDependencyResolution", false );
if ( dependencyResolution != null )
{
mojo.setDependencyResolutionRequired( dependencyResolution );
mojo.setDependencyResolutionRequired( dependencyResolution.getValue() );
}
PlexusConfiguration dependencyCollection = c.getChild( "requiresDependencyCollection", false );
if ( dependencyCollection != null )
{
mojo.setDependencyCollectionRequired( dependencyCollection.getValue() );
}
String directInvocationOnly = c.getChild( "requiresDirectInvocation" ).getValue();

View File

@ -66,7 +66,8 @@ public void testBuildReader()
assertEquals( "jar", md.getGoal() );
assertEquals( "mojo-description", md.getDescription() );
assertEquals( "runtime", md.isDependencyResolutionRequired() );
assertEquals( "runtime", md.getDependencyResolutionRequired() );
assertEquals( "test", md.getDependencyCollectionRequired() );
assertEquals( false, md.isAggregator() );
assertEquals( false, md.isDirectInvocationOnly() );
assertEquals( true, md.isInheritedByDefault() );

View File

@ -12,6 +12,7 @@
<goal>jar</goal>
<description>mojo-description</description>
<requiresDependencyResolution>runtime</requiresDependencyResolution>
<requiresDependencyCollection>test</requiresDependencyCollection>
<requiresDirectInvocation>false</requiresDirectInvocation>
<requiresProject>true</requiresProject>
<requiresReports>false</requiresReports>