[SUREFIRE] preparation for 3.0
Project: http://git-wip-us.apache.org/repos/asf/maven-surefire/repo Commit: http://git-wip-us.apache.org/repos/asf/maven-surefire/commit/d6dab3bf Tree: http://git-wip-us.apache.org/repos/asf/maven-surefire/tree/d6dab3bf Diff: http://git-wip-us.apache.org/repos/asf/maven-surefire/diff/d6dab3bf Branch: refs/heads/3.0-rc1 Commit: d6dab3bfa5e633c1b24572067aaacf8d5a91350b Parents: 21a5602 Author: Tibor17 <tibo...@lycos.com> Authored: Sun Jan 17 02:37:15 2016 +0100 Committer: Tibor17 <tibo...@lycos.com> Committed: Sun Jan 17 02:37:15 2016 +0100 ---------------------------------------------------------------------- maven-failsafe-plugin/pom.xml | 4 + .../plugin/surefire/AbstractSurefireMojo.java | 100 ++++++------------- .../maven/plugin/surefire/ProviderInfo.java | 3 +- .../surefire/SurefireDependencyResolver.java | 86 ++++++++-------- maven-surefire-plugin/pom.xml | 4 + 5 files changed, 82 insertions(+), 115 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/d6dab3bf/maven-failsafe-plugin/pom.xml ---------------------------------------------------------------------- diff --git a/maven-failsafe-plugin/pom.xml b/maven-failsafe-plugin/pom.xml index 8c56890..d4a74f5 100644 --- a/maven-failsafe-plugin/pom.xml +++ b/maven-failsafe-plugin/pom.xml @@ -73,6 +73,10 @@ <type>zip</type> <classifier>site-source</classifier> </dependency> + <dependency> + <groupId>org.apache.maven.shared</groupId> + <artifactId>maven-artifact-transfer</artifactId> + </dependency> </dependencies> <build> http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/d6dab3bf/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java ---------------------------------------------------------------------- diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java index 1c41490..be0db9f 100644 --- a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java +++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java @@ -22,7 +22,6 @@ package org.apache.maven.plugin.surefire; import java.io.File; import java.io.IOException; -import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -44,12 +43,13 @@ import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.artifact.resolver.AbstractArtifactResolutionException; 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.project.DefaultProjectBuildingRequest; import org.apache.maven.project.ProjectBuildingRequest; +import org.apache.maven.repository.RepositorySystem; import org.apache.maven.shared.artifact.resolve.ArtifactResolver; import org.apache.maven.artifact.resolver.filter.ArtifactFilter; -import org.apache.maven.artifact.resolver.filter.ExcludesArtifactFilter; import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter; import org.apache.maven.artifact.versioning.ArtifactVersion; import org.apache.maven.artifact.versioning.DefaultArtifactVersion; @@ -667,6 +667,9 @@ public abstract class AbstractSurefireMojo @Component private ToolchainManager toolchainManager; + @Component + private RepositorySystem repositorySystem; + private Artifact surefireBooterArtifact; private Toolchain toolchain; @@ -901,8 +904,8 @@ public abstract class AbstractSurefireMojo private void createDependencyResolver() { dependencyResolver = - new SurefireDependencyResolver( getArtifactResolver(), getArtifactFactory(), getLog(), getLocalRepository(), - getRemoteRepositories(), getMetadataSource(), getPluginName() ); + new SurefireDependencyResolver( repositorySystem, getArtifactFactory(), getLog(), getLocalRepository(), + getRemoteRepositories(), getPluginName() ); } protected List<ProviderInfo> createProviders() @@ -2215,28 +2218,13 @@ public abstract class AbstractSurefireMojo } } - - private ArtifactResolutionResult resolveArtifact( Artifact filteredArtifact, Artifact providerArtifact ) + private ArtifactResolutionResult resolveArtifact( Artifact providerArtifact ) { - ArtifactFilter filter = null; - if ( filteredArtifact != null ) - { - filter = new ExcludesArtifactFilter( - Collections.singletonList( filteredArtifact.getGroupId() + ":" + filteredArtifact.getArtifactId() ) ); - } - - Artifact originatingArtifact = getArtifactFactory().createBuildArtifact( "dummy", "dummy", "1.0", "jar" ); - - try - { - return getArtifactResolver().resolveTransitively( Collections.singleton( providerArtifact ), - originatingArtifact, getLocalRepository(), - getRemoteRepositories(), getMetadataSource(), filter ); - } - catch ( AbstractArtifactResolutionException e ) - { - throw new RuntimeException( e ); - } + ArtifactResolutionRequest request = new ArtifactResolutionRequest() + .setArtifact( providerArtifact ) + .setRemoteRepositories( remoteRepositories ) + .setLocalRepository( localRepository ); + return repositorySystem.resolve( request ); } private Classpath getArtifactClasspath( Artifact surefireArtifact ) @@ -2244,18 +2232,13 @@ public abstract class AbstractSurefireMojo Classpath existing = ClasspathCache.getCachedClassPath( surefireArtifact.getArtifactId() ); if ( existing == null ) { - ArtifactResolutionResult result = resolveArtifact( null, surefireArtifact ); - List<String> items = new ArrayList<String>(); - for ( Object o : result.getArtifacts() ) + for ( Artifact artifact : resolveArtifact( surefireArtifact ).getArtifacts() ) { - Artifact artifact = (Artifact) o; - - getLog().debug( - "Adding to " + getPluginName() + " booter test classpath: " + artifact.getFile().getAbsolutePath() + String artifactPath = artifact.getFile().getAbsolutePath(); + getLog().debug( "Adding to " + getPluginName() + " booter test classpath: " + artifactPath + " Scope: " + artifact.getScope() ); - - items.add( artifact.getFile().getAbsolutePath() ); + items.add( artifactPath ); } existing = new Classpath( items ); ClasspathCache.setCachedClasspath( surefireArtifact.getArtifactId(), existing ); @@ -2265,34 +2248,9 @@ public abstract class AbstractSurefireMojo private Properties getUserProperties() { - Properties props = null; - try - { - // try calling MavenSession.getUserProperties() from Maven 2.1.0-M1+ - Method getUserProperties = getSession().getClass().getMethod( "getUserProperties" ); - props = (Properties) getUserProperties.invoke( getSession() ); - } - catch ( Exception e ) - { - String msg = "Build uses Maven 2.0.x, cannot propagate system properties" - + " from command line to tests (cf. SUREFIRE-121)"; - if ( getLog().isDebugEnabled() ) - { - getLog().warn( msg, e ); - } - else - { - getLog().warn( msg ); - } - } - if ( props == null ) - { - props = new Properties(); - } - return props; + return getSession().getUserProperties(); } - private void ensureWorkingDirectoryExists() throws MojoFailureException { @@ -2486,7 +2444,7 @@ public abstract class AbstractSurefireMojo } public Classpath getProviderClasspath() - throws ArtifactResolutionException, ArtifactNotFoundException + throws ArtifactResolutionException, ArtifactNotFoundException, ArtifactResolverException { Artifact surefireArtifact = getPluginArtifactMap().get( "org.apache.maven.surefire:surefire-booter" ); return dependencyResolver.getProviderClasspath( "surefire-testng", surefireArtifact.getBaseVersion(), @@ -2512,12 +2470,12 @@ public abstract class AbstractSurefireMojo } public Classpath getProviderClasspath() - throws ArtifactResolutionException, ArtifactNotFoundException + throws ArtifactResolutionException, ArtifactNotFoundException, ArtifactResolverException { // add the JUnit provider as default - it doesn't require JUnit to be present, // since it supports POJO tests. - return dependencyResolver.getProviderClasspath( "surefire-junit3", surefireBooterArtifact.getBaseVersion(), - null ); + return dependencyResolver.getProviderClasspath( "surefire-junit3", + surefireBooterArtifact.getBaseVersion() ); } } @@ -2550,10 +2508,10 @@ public abstract class AbstractSurefireMojo } public Classpath getProviderClasspath() - throws ArtifactResolutionException, ArtifactNotFoundException + throws ArtifactResolutionException, ArtifactNotFoundException, ArtifactResolverException { - return dependencyResolver.getProviderClasspath( "surefire-junit4", surefireBooterArtifact.getBaseVersion(), - null ); + return dependencyResolver.getProviderClasspath( "surefire-junit4", + surefireBooterArtifact.getBaseVersion() ); } } @@ -2595,10 +2553,10 @@ public abstract class AbstractSurefireMojo } public Classpath getProviderClasspath() - throws ArtifactResolutionException, ArtifactNotFoundException + throws ArtifactResolutionException, ArtifactNotFoundException, ArtifactResolverException { - return dependencyResolver.getProviderClasspath( "surefire-junit47", surefireBooterArtifact.getBaseVersion(), - null ); + return dependencyResolver.getProviderClasspath( "surefire-junit47", + surefireBooterArtifact.getBaseVersion() ); } } @@ -2639,7 +2597,7 @@ public abstract class AbstractSurefireMojo } public Classpath getProviderClasspath() - throws ArtifactResolutionException, ArtifactNotFoundException + throws ArtifactResolutionException, ArtifactNotFoundException, ArtifactResolverException { final Map<String, Artifact> pluginArtifactMap = getPluginArtifactMap(); Artifact plugin = pluginArtifactMap.get( "org.apache.maven.plugins:maven-surefire-plugin" ); http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/d6dab3bf/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/ProviderInfo.java ---------------------------------------------------------------------- diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/ProviderInfo.java b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/ProviderInfo.java index 50de7ea..23c3d43 100644 --- a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/ProviderInfo.java +++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/ProviderInfo.java @@ -22,6 +22,7 @@ package org.apache.maven.plugin.surefire; import org.apache.maven.artifact.resolver.ArtifactNotFoundException; import org.apache.maven.artifact.resolver.ArtifactResolutionException; import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.shared.artifact.resolve.ArtifactResolverException; import org.apache.maven.surefire.booter.Classpath; import javax.annotation.Nonnull; @@ -37,7 +38,7 @@ public interface ProviderInfo boolean isApplicable(); Classpath getProviderClasspath() - throws ArtifactResolutionException, ArtifactNotFoundException; + throws ArtifactResolutionException, ArtifactNotFoundException, ArtifactResolverException; void addProviderProperties() throws MojoExecutionException; } http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/d6dab3bf/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/SurefireDependencyResolver.java ---------------------------------------------------------------------- diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/SurefireDependencyResolver.java b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/SurefireDependencyResolver.java index 7f91a5e..bd32478 100644 --- a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/SurefireDependencyResolver.java +++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/SurefireDependencyResolver.java @@ -20,29 +20,32 @@ package org.apache.maven.plugin.surefire; */ import java.util.ArrayList; -import java.util.Collections; import java.util.List; import java.util.Map; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.factory.ArtifactFactory; -import org.apache.maven.artifact.metadata.ArtifactMetadataSource; import org.apache.maven.artifact.repository.ArtifactRepository; 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.filter.ArtifactFilter; import org.apache.maven.artifact.resolver.filter.ExcludesArtifactFilter; import org.apache.maven.artifact.versioning.DefaultArtifactVersion; import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException; import org.apache.maven.artifact.versioning.OverConstrainedVersionException; import org.apache.maven.artifact.versioning.VersionRange; import org.apache.maven.plugin.logging.Log; -import org.apache.maven.shared.artifact.resolve.ArtifactResolver; +import org.apache.maven.repository.RepositorySystem; +import org.apache.maven.shared.artifact.resolve.ArtifactResolverException; import org.apache.maven.surefire.booter.Classpath; import javax.annotation.Nonnull; import javax.annotation.Nullable; +import static java.util.Collections.singletonList; +import static org.apache.maven.artifact.Artifact.SCOPE_TEST; +import static org.apache.maven.artifact.versioning.VersionRange.createFromVersion; + /** * Does dependency resolution and artifact handling for the surefire plugin. * @@ -51,36 +54,30 @@ import javax.annotation.Nullable; */ public class SurefireDependencyResolver { - - private final ArtifactResolver artifactResolver; + private final RepositorySystem repositorySystem; private final ArtifactFactory artifactFactory; - private final org.apache.maven.plugin.logging.Log log; + private final Log log; private final ArtifactRepository localRepository; private final List<ArtifactRepository> remoteRepositories; - private final ArtifactMetadataSource artifactMetadataSource; - private final String pluginName; - protected SurefireDependencyResolver( ArtifactResolver artifactResolver, ArtifactFactory artifactFactory, Log log, + protected SurefireDependencyResolver( RepositorySystem repositorySystem, ArtifactFactory artifactFactory, Log log, ArtifactRepository localRepository, - List<ArtifactRepository> remoteRepositories, - ArtifactMetadataSource artifactMetadataSource, String pluginName ) + List<ArtifactRepository> remoteRepositories, String pluginName ) { - this.artifactResolver = artifactResolver; + this.repositorySystem = repositorySystem; this.artifactFactory = artifactFactory; this.log = log; this.localRepository = localRepository; this.remoteRepositories = remoteRepositories; - this.artifactMetadataSource = artifactMetadataSource; this.pluginName = pluginName; } - public boolean isWithinVersionSpec( @Nullable Artifact artifact, @Nonnull String versionSpec ) { if ( artifact == null ) @@ -109,46 +106,49 @@ public class SurefireDependencyResolver } } + public ArtifactResolutionResult resolveArtifact( Artifact providerArtifact ) + throws ArtifactNotFoundException, ArtifactResolverException, ArtifactResolutionException + { + return resolveArtifact( providerArtifact, null ); + } - public ArtifactResolutionResult resolveArtifact( @Nullable Artifact filteredArtifact, Artifact providerArtifact ) - throws ArtifactResolutionException, ArtifactNotFoundException + public ArtifactResolutionResult resolveArtifact( Artifact providerArtifact, Artifact excludeArtifact ) + throws ArtifactResolutionException, ArtifactNotFoundException, ArtifactResolverException { - ArtifactFilter filter = null; - if ( filteredArtifact != null ) + ArtifactResolutionRequest request = new ArtifactResolutionRequest() + .setArtifact( providerArtifact ) + .setRemoteRepositories( remoteRepositories ) + .setLocalRepository( localRepository ); + if ( excludeArtifact != null ) { - filter = new ExcludesArtifactFilter( - Collections.singletonList( filteredArtifact.getGroupId() + ":" + filteredArtifact.getArtifactId() ) ); + String pattern = excludeArtifact.getGroupId() + ":" + excludeArtifact.getArtifactId(); + request.setCollectionFilter( new ExcludesArtifactFilter( singletonList( pattern ) ) ); } + return repositorySystem.resolve( request ); + } - Artifact originatingArtifact = artifactFactory.createBuildArtifact( "dummy", "dummy", "1.0", "jar" ); - - return artifactResolver.resolveTransitively( Collections.singleton( providerArtifact ), originatingArtifact, - localRepository, remoteRepositories, artifactMetadataSource, - filter ); + public Classpath getProviderClasspath( String provider, String version ) + throws ArtifactNotFoundException, ArtifactResolutionException, ArtifactResolverException + { + return getProviderClasspath( provider, version, null ); } - public Classpath getProviderClasspath( String provider, String version, Artifact filteredArtifact ) - throws ArtifactNotFoundException, ArtifactResolutionException + public Classpath getProviderClasspath( String provider, String version, Artifact excludeArtifact ) + throws ArtifactNotFoundException, ArtifactResolutionException, ArtifactResolverException { Classpath classPath = ClasspathCache.getCachedClassPath( provider ); if ( classPath == null ) { - Artifact providerArtifact = artifactFactory.createDependencyArtifact( "org.apache.maven.surefire", provider, - VersionRange.createFromVersion( - version ), "jar", null, - Artifact.SCOPE_TEST ); - ArtifactResolutionResult result = resolveArtifact( filteredArtifact, providerArtifact ); + Artifact providerArtifact = + artifactFactory.createDependencyArtifact( "org.apache.maven.surefire", provider, + createFromVersion( version ), "jar", null, SCOPE_TEST ); List<String> files = new ArrayList<String>(); - - for ( Object o : result.getArtifacts() ) + for ( Artifact artifact : resolveArtifact( providerArtifact ).getArtifacts() ) { - Artifact artifact = (Artifact) o; - - log.debug( - "Adding to " + pluginName + " test classpath: " + artifact.getFile().getAbsolutePath() + " Scope: " + String artifactPath = artifact.getFile().getAbsolutePath(); + log.debug( "Adding to " + pluginName + " test classpath: " + artifactPath + " Scope: " + artifact.getScope() ); - - files.add( artifact.getFile().getAbsolutePath() ); + files.add( artifactPath ); } classPath = new Classpath( files ); ClasspathCache.setCachedClasspath( provider, classPath ); @@ -157,12 +157,12 @@ public class SurefireDependencyResolver } public Classpath addProviderToClasspath( Map<String, Artifact> pluginArtifactMap, Artifact surefireArtifact ) - throws ArtifactResolutionException, ArtifactNotFoundException + throws ArtifactResolutionException, ArtifactNotFoundException, ArtifactResolverException { List<String> files = new ArrayList<String>(); if ( surefireArtifact != null ) { - final ArtifactResolutionResult artifactResolutionResult = resolveArtifact( null, surefireArtifact ); + final ArtifactResolutionResult artifactResolutionResult = resolveArtifact( surefireArtifact ); for ( Artifact artifact : pluginArtifactMap.values() ) { if ( !artifactResolutionResult.getArtifacts().contains( artifact ) ) http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/d6dab3bf/maven-surefire-plugin/pom.xml ---------------------------------------------------------------------- diff --git a/maven-surefire-plugin/pom.xml b/maven-surefire-plugin/pom.xml index 978ad1a..d87f17d 100644 --- a/maven-surefire-plugin/pom.xml +++ b/maven-surefire-plugin/pom.xml @@ -65,6 +65,10 @@ <artifactId>maven-plugin-annotations</artifactId> <scope>compile</scope> </dependency> + <dependency> + <groupId>org.apache.maven.shared</groupId> + <artifactId>maven-artifact-transfer</artifactId> + </dependency> </dependencies> <build>