This is an automated email from the ASF dual-hosted git repository.

tibordigana pushed a commit to branch resolver
in repository https://gitbox.apache.org/repos/asf/maven-surefire.git

commit 041fe0056f8b9619b02143c5fdc7186fe53bafd1
Author: tibordigana <tibordig...@apache.org>
AuthorDate: Sun Jan 19 21:57:29 2020 +0100

    dependencies for dynamic provider contain Maven artifacts from the MOJO 
plugin
---
 .../plugin/surefire/AbstractSurefireMojo.java      |  3 +-
 .../surefire/SurefireDependencyResolver.java       | 92 ++++++++++++++--------
 pom.xml                                            | 12 +++
 3 files changed, 73 insertions(+), 34 deletions(-)

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 7604c03..ed751cf 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
@@ -3222,8 +3222,7 @@ public abstract class AbstractSurefireMojo
         @Nonnull
         public Set<Artifact> getProviderClasspath()
         {
-            return surefireDependencyResolver.addProviderToClasspath( 
getPluginArtifactMap(), getMojoArtifact(),
-                    getApiArtifact(), getLoggerApiArtifact() );
+            return surefireDependencyResolver.resolveProvider( 
getPluginDescriptor() );
         }
     }
 
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 16cb6c8..de5ee47 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
@@ -37,6 +37,7 @@ import 
org.apache.maven.artifact.versioning.OverConstrainedVersionException;
 import org.apache.maven.artifact.versioning.VersionRange;
 import org.apache.maven.model.Dependency;
 import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugin.descriptor.PluginDescriptor;
 import org.apache.maven.plugin.surefire.log.api.ConsoleLogger;
 import org.apache.maven.project.ProjectBuildingRequest;
 import org.apache.maven.repository.RepositorySystem;
@@ -156,26 +157,64 @@ final class SurefireDependencyResolver
 
     ArtifactResolutionResult resolvePluginArtifact( Artifact artifact )
     {
-        return resolveArtifact( artifact, pluginRemoteRepositories );
+        return resolvePluginArtifact( artifact, new RuntimeArtifactFilter() );
     }
 
     ArtifactResolutionResult resolveProjectArtifact( Artifact artifact )
     {
-        return resolveArtifact( artifact, projectRemoteRepositories );
+        return resolveProjectArtifact( artifact, new RuntimeArtifactFilter() );
     }
 
-    private ArtifactResolutionResult resolveArtifact( Artifact artifact, 
List<ArtifactRepository> repositories )
+    private ArtifactResolutionResult resolvePluginArtifact( Artifact artifact, 
ArtifactFilter filter )
+    {
+        return resolveArtifact( artifact, pluginRemoteRepositories, filter );
+    }
+
+    private ArtifactResolutionResult resolveProjectArtifact( Artifact 
artifact, ArtifactFilter filter )
+    {
+        return resolveArtifact( artifact, projectRemoteRepositories, filter );
+    }
+
+    private ArtifactResolutionResult resolveArtifact( Artifact artifact, 
List<ArtifactRepository> repositories,
+                                                      ArtifactFilter filter )
     {
         ArtifactResolutionRequest request = new ArtifactResolutionRequest()
+                //.setOffline(  )
+                // .setForceUpdate(  )
+                // set mirrors, proxies, servers - see MavenSession.Settings
                 .setArtifact( artifact )
                 .setLocalRepository( localRepository )
                 .setResolveTransitively( true )
-                .setCollectionFilter( new RuntimeArtifactFilter() )
+                .setCollectionFilter( filter )
                 .setRemoteRepositories( repositories );
 
         return repositorySystem.resolve( request );
     }
 
+    private Set<Artifact> toPluginDependenciesAsArtifacts( 
Collection<Dependency> dependencies )
+    {
+        Set<Artifact> artifacts = new LinkedHashSet<>();
+        for ( Dependency dependency : dependencies )
+        {
+            Artifact artifact = repositorySystem.createDependencyArtifact( 
dependency );
+            // LegacyRepositorySystem do not resolve the artifact file
+            if ( artifact.getFile() == null )
+            {
+                ArtifactFilter filter = new RuntimeArtifactFilter( artifact );
+                Iterator<Artifact> it = resolvePluginArtifact( artifact, 
filter ).getArtifacts().iterator();
+                if ( it.hasNext() )
+                {
+                    artifacts.add( it.next() );
+                }
+            }
+            else
+            {
+                artifacts.add( artifact );
+            }
+        }
+        return artifacts;
+    }
+
     @Nonnull
     Set<Artifact> getProviderClasspath( String providerArtifactId, String 
providerVersion )
     {
@@ -204,34 +243,10 @@ final class SurefireDependencyResolver
         return artifactMapByVersionlessId( getProviderClasspath( 
providerArtifactId, providerVersion ) );
     }
 
-    Set<Artifact> addProviderToClasspath( Map<String, Artifact> 
pluginArtifactMap, Artifact mojoPluginArtifact,
-                                          Artifact surefireApi, Artifact 
surefireLoggerApi )
+    Set<Artifact> resolveProvider( PluginDescriptor pluginDescriptor )
     {
-        Set<Artifact> providerArtifacts = new LinkedHashSet<>();
-        ArtifactResolutionResult artifactResolutionResult = 
resolvePluginArtifact( mojoPluginArtifact );
-        for ( Artifact artifact : pluginArtifactMap.values() )
-        {
-            if ( !artifactResolutionResult.getArtifacts().contains( artifact ) 
)
-            {
-                providerArtifacts.add( artifact );
-                for ( Artifact dependency : resolvePluginArtifact( artifact 
).getArtifacts() )
-                {
-                    String groupId = dependency.getGroupId();
-                    String artifactId = dependency.getArtifactId();
-                    if ( groupId.equals( surefireApi.getGroupId() )
-                            && artifactId.equals( surefireApi.getArtifactId() 
) )
-                    {
-                        providerArtifacts.add( surefireApi );
-                    }
-                    else if ( groupId.equals( surefireLoggerApi.getGroupId() )
-                            && artifactId.equals( 
surefireLoggerApi.getArtifactId() ) )
-                    {
-                        providerArtifacts.add( surefireLoggerApi );
-                    }
-                }
-            }
-        }
-        return orderProviderArtifacts( providerArtifacts );
+        Collection<Dependency> dependencies = 
pluginDescriptor.getPlugin().getDependencies();
+        return toPluginDependenciesAsArtifacts( dependencies );
     }
 
     private static Set<Artifact> orderProviderArtifacts( Set<Artifact> 
providerArtifacts )
@@ -269,11 +284,24 @@ final class SurefireDependencyResolver
         private static final Collection<String> SCOPES =
                 asList( SCOPE_COMPILE, SCOPE_COMPILE_PLUS_RUNTIME, 
SCOPE_RUNTIME );
 
+        private final Artifact filter;
+
+        RuntimeArtifactFilter()
+        {
+            this( null );
+        }
+
+        RuntimeArtifactFilter( Artifact filter )
+        {
+            this.filter = filter;
+        }
+
         @Override
         public boolean include( Artifact artifact )
         {
             String scope = artifact.getScope();
-            return !artifact.isOptional() && ( scope == null || 
SCOPES.contains( scope ) );
+            return ( filter == null || artifact.equals( filter ) )
+                && !artifact.isOptional() && ( scope == null || 
SCOPES.contains( scope ) );
         }
     }
 }
diff --git a/pom.xml b/pom.xml
index 0f6d440..e6a2838 100644
--- a/pom.xml
+++ b/pom.xml
@@ -225,6 +225,18 @@
             <groupId>org.apache.maven.shared</groupId>
             <artifactId>maven-shared-utils</artifactId>
           </exclusion>
+          <exclusion>
+            <groupId>org.apache.maven</groupId>
+            <artifactId>maven-plugin-api</artifactId>
+          </exclusion>
+          <exclusion>
+            <groupId>org.apache.maven</groupId>
+             <artifactId>maven-model</artifactId>
+          </exclusion>
+          <exclusion>
+             <groupId>org.sonatype.sisu</groupId>
+             <artifactId>sisu-inject-plexus</artifactId>
+          </exclusion>
         </exclusions>
       </dependency>
       <dependency>

Reply via email to