[MNG-4199] [regression] Dependency resolution for compile and runtime scope misses provided&system dependencies

git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@784480 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Benjamin Bentmann 2009-06-13 21:53:18 +00:00
parent ef1445e0d8
commit 05a782b347
7 changed files with 115 additions and 64 deletions

View File

@ -0,0 +1,73 @@
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.ArrayList;
import java.util.Collection;
import org.apache.maven.artifact.Artifact;
/**
* Apply multiple filters, accepting an artifact if at least one of the filters accepts it.
*
* @author Benjamin Bentmann
*/
public class OrArtifactFilter
implements ArtifactFilter
{
private Collection<ArtifactFilter> filters;
public OrArtifactFilter()
{
}
public OrArtifactFilter( Collection<ArtifactFilter> filters )
{
this.filters = filters;
}
public boolean include( Artifact artifact )
{
if ( filters != null )
{
for ( ArtifactFilter filter : filters )
{
if ( filter.include( artifact ) )
{
return true;
}
}
}
return false;
}
public void add( ArtifactFilter artifactFilter )
{
if ( filters == null )
{
filters = new ArrayList<ArtifactFilter>();
}
filters.add( artifactFilter );
}
}

View File

@ -1,5 +1,6 @@
package org.apache.maven;
import java.util.Collection;
import java.util.List;
import java.util.Set;
@ -11,6 +12,7 @@ import org.apache.maven.artifact.resolver.ArtifactResolutionRequest;
import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
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.project.MavenProject;
import org.apache.maven.project.artifact.ProjectArtifact;
@ -28,7 +30,7 @@ public class DefaultProjectDependenciesResolver
@Requirement
private ResolutionErrorHandler resolutionErrorHandler;
public Set<Artifact> resolve( MavenProject project, String scope, ArtifactRepository localRepository, List<ArtifactRepository> remoteRepositories )
public Set<Artifact> resolve( MavenProject project, Collection<String> scopes, ArtifactRepository localRepository, List<ArtifactRepository> remoteRepositories )
throws ArtifactResolutionException, ArtifactNotFoundException
{
/*
@ -61,8 +63,13 @@ public class DefaultProjectDependenciesResolver
filter = scopeFilter;
}
*/
ArtifactFilter scopeFilter = new ScopeArtifactFilter( scope );
OrArtifactFilter scopeFilter = new OrArtifactFilter();
for ( String scope : scopes )
{
scopeFilter.add( new ScopeArtifactFilter( scope ) );
}
ArtifactFilter filter = scopeFilter;

View File

@ -1,5 +1,6 @@
package org.apache.maven;
import java.util.Collection;
import java.util.List;
import java.util.Set;
@ -11,6 +12,6 @@ import org.apache.maven.project.MavenProject;
public interface ProjectDependenciesResolver
{
public Set<Artifact> resolve( MavenProject project, String scope, ArtifactRepository localRepository, List<ArtifactRepository> remoteRepositories )
public Set<Artifact> resolve( MavenProject project, Collection<String> scopes, ArtifactRepository localRepository, List<ArtifactRepository> remoteRepositories )
throws ArtifactResolutionException, ArtifactNotFoundException;
}

View File

@ -23,6 +23,7 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@ -30,7 +31,6 @@ import java.util.Set;
import java.util.StringTokenizer;
import org.apache.maven.ProjectDependenciesResolver;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.metadata.Metadata;
import org.apache.maven.artifact.repository.metadata.RepositoryMetadataReadException;
@ -169,7 +169,7 @@ public class DefaultLifecycleExecutor
// mojoDescriptor.isDependencyResolutionRequired() is actually the scope of the dependency resolution required, not a boolean ... yah.
try
{
projectDependenciesResolver.resolve( currentProject, executionPlan.getRequiredResolutionScope(), session.getLocalRepository(), currentProject.getRemoteArtifactRepositories() );
projectDependenciesResolver.resolve( currentProject, executionPlan.getRequiredResolutionScopes(), session.getLocalRepository(), currentProject.getRemoteArtifactRepositories() );
}
catch ( ArtifactNotFoundException e )
{
@ -227,7 +227,7 @@ public class DefaultLifecycleExecutor
List<MojoExecution> lifecyclePlan = new ArrayList<MojoExecution>();
String requiredDependencyResolutionScope = null;
Set<String> requiredDependencyResolutionScopes = new HashSet<String>();
for ( String task : tasks )
{
@ -258,14 +258,17 @@ public class DefaultLifecycleExecutor
pluginDescriptor.setClassRealm( pluginManager.getPluginRealm( session, pluginDescriptor ) );
}
requiredDependencyResolutionScope = calculateRequiredDependencyResolutionScope( requiredDependencyResolutionScope, mojoDescriptor.isDependencyResolutionRequired() );
if ( StringUtils.isNotEmpty( mojoDescriptor.isDependencyResolutionRequired() ) )
{
requiredDependencyResolutionScopes.add( mojoDescriptor.isDependencyResolutionRequired() );
}
mojoExecution.setMojoDescriptor( mojoDescriptor );
populateMojoExecutionConfiguration( project, mojoExecution, false );
}
return new MavenExecutionPlan( lifecyclePlan, requiredDependencyResolutionScope );
return new MavenExecutionPlan( lifecyclePlan, requiredDependencyResolutionScopes );
}
private void calculateExecutionForIndividualGoal( MavenSession session, List<MojoExecution> lifecyclePlan, String goal )
@ -431,37 +434,6 @@ public class DefaultLifecycleExecutor
}
}
// SCOPE_COMPILE
// SCOPE_TEST
// SCOPE_RUNTIME
//
String calculateRequiredDependencyResolutionScope( String currentRequiredDependencyResolutionScope, String inputScope )
{
if ( inputScope == null )
{
return currentRequiredDependencyResolutionScope;
}
if ( currentRequiredDependencyResolutionScope == null && inputScope != null )
{
return inputScope;
}
if ( currentRequiredDependencyResolutionScope.equals( Artifact.SCOPE_COMPILE ) && ( inputScope.equals( Artifact.SCOPE_RUNTIME ) || inputScope.equals( Artifact.SCOPE_TEST ) ) )
{
return inputScope;
}
if ( currentRequiredDependencyResolutionScope.equals( Artifact.SCOPE_RUNTIME ) && inputScope.equals( Artifact.SCOPE_TEST ) )
{
return inputScope;
}
// Nothing changed we return what we were
//
return currentRequiredDependencyResolutionScope;
}
private String executionDescription( MojoExecution me, MavenProject project )
{
PluginDescriptor pd = me.getMojoDescriptor().getPluginDescriptor();

View File

@ -1,6 +1,7 @@
package org.apache.maven.lifecycle;
import java.util.List;
import java.util.Set;
import org.apache.maven.plugin.MojoExecution;
@ -15,13 +16,13 @@ public class MavenExecutionPlan
/** Individual executions that must be performed. */
private List<MojoExecution> executions;
/** For project dependency resolution, the scope of resolution required if any. */
private String requiredDependencyResolutionScope;
/** For project dependency resolution, the scopes of resolution required if any. */
private Set<String> requiredDependencyResolutionScopes;
public MavenExecutionPlan( List<MojoExecution> executions, String requiredDependencyResolutionScope )
public MavenExecutionPlan( List<MojoExecution> executions, Set<String> requiredDependencyResolutionScopes )
{
this.executions = executions;
this.requiredDependencyResolutionScope = requiredDependencyResolutionScope;
this.requiredDependencyResolutionScopes = requiredDependencyResolutionScopes;
}
public List<MojoExecution> getExecutions()
@ -29,8 +30,8 @@ public class MavenExecutionPlan
return executions;
}
public String getRequiredResolutionScope()
public Set<String> getRequiredResolutionScopes()
{
return requiredDependencyResolutionScope;
return requiredDependencyResolutionScopes;
}
}

View File

@ -1,6 +1,7 @@
package org.apache.maven;
import java.io.File;
import java.util.Collections;
import java.util.List;
import java.util.Properties;
import java.util.Set;
@ -48,10 +49,14 @@ public class ProjectDependenciesResolverTest
.addDependency( "org.apache.maven.its", "maven-core-it-support", "1.3", Artifact.SCOPE_RUNTIME, exclusion )
.get();
Set<Artifact> artifactDependencies = resolver.resolve( project, Artifact.SCOPE_COMPILE, getLocalRepository(), getRemoteRepositories() );
Set<Artifact> artifactDependencies =
resolver.resolve( project, Collections.singleton( Artifact.SCOPE_COMPILE ), getLocalRepository(),
getRemoteRepositories() );
assertEquals( 0, artifactDependencies.size() );
artifactDependencies = resolver.resolve( project, Artifact.SCOPE_RUNTIME, getLocalRepository(), getRemoteRepositories() );
artifactDependencies =
resolver.resolve( project, Collections.singleton( Artifact.SCOPE_RUNTIME ), getLocalRepository(),
getRemoteRepositories() );
assertEquals( 1, artifactDependencies.size() );
assertEquals( "maven-core-it-support" , artifactDependencies.iterator().next().getArtifactId() );
}
@ -63,7 +68,9 @@ public class ProjectDependenciesResolverTest
.addDependency( "com.mycompany", "system-dependency", "1.0", Artifact.SCOPE_SYSTEM, new File( getBasedir(), "pom.xml" ).getAbsolutePath() )
.get();
Set<Artifact> artifactDependencies = resolver.resolve( project, Artifact.SCOPE_COMPILE, getLocalRepository(), getRemoteRepositories() );
Set<Artifact> artifactDependencies =
resolver.resolve( project, Collections.singleton( Artifact.SCOPE_COMPILE ), getLocalRepository(),
getRemoteRepositories() );
assertEquals( 1, artifactDependencies.size() );
}
@ -78,7 +85,8 @@ public class ProjectDependenciesResolverTest
MavenSession session = createMavenSession( pom, eps );
MavenProject project = session.getCurrentProject();
resolver.resolve( project, Artifact.SCOPE_COMPILE, getLocalRepository(), getRemoteRepositories() );
resolver.resolve( project, Collections.singleton( Artifact.SCOPE_COMPILE ), getLocalRepository(),
getRemoteRepositories() );
List<String> elements = project.getCompileClasspathElements();
assertEquals( 2, elements.size() );

View File

@ -127,7 +127,9 @@ public class LifecycleExecutorTest
MavenExecutionPlan plan = lifecycleExecutor.calculateExecutionPlan( session, "clean", "install" );
assertEquals( Artifact.SCOPE_TEST, plan.getRequiredResolutionScope() );
assertTrue( plan.getRequiredResolutionScopes().contains( Artifact.SCOPE_COMPILE ) );
assertTrue( plan.getRequiredResolutionScopes().contains( Artifact.SCOPE_RUNTIME ) );
assertTrue( plan.getRequiredResolutionScopes().contains( Artifact.SCOPE_TEST ) );
List<MojoExecution> executions = plan.getExecutions();
@ -259,17 +261,4 @@ public class LifecycleExecutorTest
assertNotNull( plugin );
}
//
public void testRequiredDependencyResolutionScopeCalculation()
throws Exception
{
assertEquals( Artifact.SCOPE_COMPILE, lifecycleExecutor.calculateRequiredDependencyResolutionScope( null, Artifact.SCOPE_COMPILE ) );
assertEquals( Artifact.SCOPE_COMPILE, lifecycleExecutor.calculateRequiredDependencyResolutionScope( Artifact.SCOPE_COMPILE, Artifact.SCOPE_COMPILE ) );
assertEquals( Artifact.SCOPE_RUNTIME, lifecycleExecutor.calculateRequiredDependencyResolutionScope( null, Artifact.SCOPE_RUNTIME ) );
assertEquals( Artifact.SCOPE_RUNTIME, lifecycleExecutor.calculateRequiredDependencyResolutionScope( Artifact.SCOPE_RUNTIME, Artifact.SCOPE_RUNTIME ) );
assertEquals( Artifact.SCOPE_TEST, lifecycleExecutor.calculateRequiredDependencyResolutionScope( null, Artifact.SCOPE_TEST ) );
assertEquals( Artifact.SCOPE_TEST, lifecycleExecutor.calculateRequiredDependencyResolutionScope( Artifact.SCOPE_TEST, Artifact.SCOPE_TEST ) );
assertEquals( Artifact.SCOPE_RUNTIME, lifecycleExecutor.calculateRequiredDependencyResolutionScope( Artifact.SCOPE_COMPILE, Artifact.SCOPE_RUNTIME) );
}
}