Author: rfscholte
Date: Sat Jun 13 20:51:12 2015
New Revision: 1685329

URL: http://svn.apache.org/r1685329
Log:
[MSHARED-425] Change construct-method signatures of ArtifactTransitivityFilter; 
don't use deprecated M2 code 

Added:
    
maven/shared/trunk/maven-common-artifact-filters/src/main/java/org/apache/maven/shared/artifact/filter/collection/Invoker.java
Modified:
    
maven/shared/trunk/maven-common-artifact-filters/src/main/java/org/apache/maven/shared/artifact/filter/collection/ArtifactTransitivityFilter.java

Modified: 
maven/shared/trunk/maven-common-artifact-filters/src/main/java/org/apache/maven/shared/artifact/filter/collection/ArtifactTransitivityFilter.java
URL: 
http://svn.apache.org/viewvc/maven/shared/trunk/maven-common-artifact-filters/src/main/java/org/apache/maven/shared/artifact/filter/collection/ArtifactTransitivityFilter.java?rev=1685329&r1=1685328&r2=1685329&view=diff
==============================================================================
--- 
maven/shared/trunk/maven-common-artifact-filters/src/main/java/org/apache/maven/shared/artifact/filter/collection/ArtifactTransitivityFilter.java
 (original)
+++ 
maven/shared/trunk/maven-common-artifact-filters/src/main/java/org/apache/maven/shared/artifact/filter/collection/ArtifactTransitivityFilter.java
 Sat Jun 13 20:51:12 2015
@@ -19,19 +19,19 @@ package org.apache.maven.shared.artifact
  * under the License.    
  */
 
-import java.util.Collection;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
 
+import org.apache.maven.RepositoryUtils;
 import org.apache.maven.artifact.Artifact;
-import org.apache.maven.artifact.factory.ArtifactFactory;
-import org.apache.maven.artifact.repository.ArtifactRepository;
-import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter;
-import org.apache.maven.project.MavenProject;
-import org.apache.maven.project.MavenProjectBuilder;
+import org.apache.maven.execution.MavenSession;
+import org.apache.maven.project.DefaultProjectBuildingRequest;
+import org.apache.maven.project.DependencyResolutionResult;
+import org.apache.maven.project.ProjectBuilder;
 import org.apache.maven.project.ProjectBuildingException;
-import org.apache.maven.project.artifact.InvalidDependencyVersionException;
+import org.apache.maven.project.ProjectBuildingRequest;
+import org.apache.maven.project.ProjectBuildingResult;
 
 /**
  * This filter will exclude everything that is not a dependency of the 
selected artifact.
@@ -42,34 +42,93 @@ import org.apache.maven.project.artifact
 public class ArtifactTransitivityFilter
     extends AbstractArtifactsFilter
 {
+    /**
+     * List of dependencyConflictIds of transitiveArtifacts
+     */
+    private Set<String> transitiveArtifacts;
 
-    Collection<Artifact> transitiveArtifacts;
-
-    ArtifactFactory factory;
-
-    ArtifactRepository local;
-
-    List<ArtifactRepository> remote;
-
-    @SuppressWarnings( "unchecked" )
-    public ArtifactTransitivityFilter( Artifact artifact, ArtifactFactory 
factory, ArtifactRepository local,
-                                       List<ArtifactRepository> remote, 
MavenProjectBuilder builder )
-        throws ProjectBuildingException, InvalidDependencyVersionException
+    /**
+     * Use {@link MavenSession#getProjectBuildingRequest()} to get the 
buildingRequest. 
+     * The projectBuilder should be resolved with CDI.
+     * 
+     * <pre>
+     *   // For Mojo
+     *   &#64;Component
+     *   private ProjectBuilder projectBuilder;
+     *
+     *   // For Components
+     *   &#64;Requirement // or &#64;Inject  
+     *   private ProjectBuilder projectBuilder;
+     * </pre>
+     * 
+     * @param artifact the artifact to resolve the dependencies from
+     * @param buildingRequest the buildingRequest
+     * @param projectBuilder the projectBuilder
+     * @throws ProjectBuildingException if the project descriptor could not be 
successfully built
+     */
+    public ArtifactTransitivityFilter( Artifact artifact, 
ProjectBuildingRequest buildingRequest,
+                                       ProjectBuilder projectBuilder )
+        throws ProjectBuildingException
     {
-        this.factory = factory;
-        this.local = local;
-        this.remote = remote;
-
-        Artifact rootArtifactPom =
-            factory.createArtifact( artifact.getGroupId(), 
artifact.getArtifactId(), artifact.getVersion(), "", "pom" );
-
-        MavenProject rootArtifactProject = builder.buildFromRepository( 
rootArtifactPom, remote, local );
-
-        // load all the artifacts.
-        transitiveArtifacts =
-            rootArtifactProject.createArtifacts( this.factory, 
Artifact.SCOPE_TEST,
-                                                 new ScopeArtifactFilter( 
Artifact.SCOPE_TEST ) );
+        ProjectBuildingRequest request = new DefaultProjectBuildingRequest( 
buildingRequest );
+        
+        request.setResolveDependencies( true );
+        
+        ProjectBuildingResult buildingResult = projectBuilder.build( artifact, 
request );
 
+        DependencyResolutionResult resolutionResult = 
buildingResult.getDependencyResolutionResult();
+        if ( resolutionResult != null )
+        {
+            if ( isMaven31() )
+            {
+                try
+                {
+                    @SuppressWarnings( "unchecked" )
+                    List<org.eclipse.aether.graph.Dependency> dependencies =
+                        (List<org.eclipse.aether.graph.Dependency>) 
Invoker.invoke( resolutionResult, "getDependencies" );
+
+                    for ( org.eclipse.aether.graph.Dependency dependency : 
dependencies )
+                    {
+                        Artifact mavenArtifact =
+                            (Artifact) Invoker.invoke( RepositoryUtils.class, 
"toArtifact",
+                                                       
org.eclipse.aether.artifact.Artifact.class,
+                                                       
dependency.getArtifact() );
+
+                        transitiveArtifacts.add( 
mavenArtifact.getDependencyConflictId() );
+                    }
+                }
+                catch ( ReflectiveOperationException e )
+                {
+                    // don't want to pollute method signature with 
ReflectionExceptions
+                    throw new RuntimeException( e.getMessage(), e );
+                }
+            }
+            else
+            {
+                try
+                {
+                    @SuppressWarnings( "unchecked" )
+                    List<org.sonatype.aether.graph.Dependency> dependencies =
+                        (List<org.sonatype.aether.graph.Dependency>) 
Invoker.invoke( resolutionResult,
+                                                                               
      "getDependencies" );
+
+                    for ( org.sonatype.aether.graph.Dependency dependency : 
dependencies )
+                    {
+                        Artifact mavenArtifact =
+                            (Artifact) Invoker.invoke( RepositoryUtils.class, 
"toArtifact",
+                                                       
org.sonatype.aether.artifact.Artifact.class,
+                                                       
dependency.getArtifact() );
+
+                        transitiveArtifacts.add( 
mavenArtifact.getDependencyConflictId() );
+                    }
+                }
+                catch ( ReflectiveOperationException e )
+                {
+                    // don't want to pollute method signature with 
ReflectionExceptions
+                    throw new RuntimeException( e.getMessage(), e );
+                }
+            }
+        }
     }
 
     public Set<Artifact> filter( Set<Artifact> artifacts )
@@ -94,16 +153,28 @@ public class ArtifactTransitivityFilter
      */
     public boolean artifactIsATransitiveDependency( Artifact artifact )
     {
-        boolean result = false;
-        for ( Artifact trans : transitiveArtifacts )
+        return transitiveArtifacts.contains( 
artifact.getDependencyConflictId() );
+    }
+
+    /**
+     * @return true if the current Maven version is Maven 3.1.
+     */
+    protected static boolean isMaven31()
+    {
+        return canFindCoreClass( "org.eclipse.aether.artifact.Artifact" ); // 
Maven 3.1 specific
+    }
+
+    private static boolean canFindCoreClass( String className )
+    {
+        try
         {
-            if ( trans.getGroupId().equals( artifact.getGroupId() )
-                && trans.getArtifactId().equals( artifact.getArtifactId() ) )
-            {
-                result = true;
-                break;
-            }
+            Thread.currentThread().getContextClassLoader().loadClass( 
className );
+
+            return true;
         }
-        return result;
-    }
+        catch ( ClassNotFoundException e )
+        {
+            return false;
+        }
+    }    
 }

Added: 
maven/shared/trunk/maven-common-artifact-filters/src/main/java/org/apache/maven/shared/artifact/filter/collection/Invoker.java
URL: 
http://svn.apache.org/viewvc/maven/shared/trunk/maven-common-artifact-filters/src/main/java/org/apache/maven/shared/artifact/filter/collection/Invoker.java?rev=1685329&view=auto
==============================================================================
--- 
maven/shared/trunk/maven-common-artifact-filters/src/main/java/org/apache/maven/shared/artifact/filter/collection/Invoker.java
 (added)
+++ 
maven/shared/trunk/maven-common-artifact-filters/src/main/java/org/apache/maven/shared/artifact/filter/collection/Invoker.java
 Sat Jun 13 20:51:12 2015
@@ -0,0 +1,51 @@
+package org.apache.maven.shared.artifact.filter.collection;
+
+/*
+ * 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.
+ */
+
+
+/**
+ * Invokes method on objects using reflection.
+ */
+final class Invoker
+{
+    private Invoker()
+    {
+        // do not instantiate
+    }
+
+    public static Object invoke( Object object, String method )
+        throws ReflectiveOperationException
+    {
+        return invoke( object.getClass(), object, method );
+    }
+
+    public static Object invoke( Class<?> objectClazz, Object object, String 
method )
+                    throws ReflectiveOperationException
+    {
+            return objectClazz.getMethod( method ).invoke( object );
+    }
+
+    public static Object invoke( Object object, String method, Class<?> clazz, 
Object arg )
+                    throws ReflectiveOperationException
+    {
+            final Class<?> objectClazz = object.getClass();
+            return objectClazz.getMethod( method, clazz ).invoke( object, arg 
);
+    }
+}


Reply via email to