maven-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rfscho...@apache.org
Subject svn commit: r1684764 - /maven/shared/branches/m3-common-artifact-filters/src/main/java/org/apache/maven/shared/artifact/filter/collection/ArtifactTransitivityFilter.java
Date Wed, 10 Jun 2015 20:58:47 GMT
Author: rfscholte
Date: Wed Jun 10 20:58:47 2015
New Revision: 1684764

URL: http://svn.apache.org/r1684764
Log:
Don't use org.apache.maven.shared.dependency.graph.DependencyNode, that will cause a cyclic
dependency.
ProjectBuildingRequest + ProjectBuilder seems to result the most efficient method-signature

Modified:
    maven/shared/branches/m3-common-artifact-filters/src/main/java/org/apache/maven/shared/artifact/filter/collection/ArtifactTransitivityFilter.java

Modified: maven/shared/branches/m3-common-artifact-filters/src/main/java/org/apache/maven/shared/artifact/filter/collection/ArtifactTransitivityFilter.java
URL: http://svn.apache.org/viewvc/maven/shared/branches/m3-common-artifact-filters/src/main/java/org/apache/maven/shared/artifact/filter/collection/ArtifactTransitivityFilter.java?rev=1684764&r1=1684763&r2=1684764&view=diff
==============================================================================
--- maven/shared/branches/m3-common-artifact-filters/src/main/java/org/apache/maven/shared/artifact/filter/collection/ArtifactTransitivityFilter.java
(original)
+++ maven/shared/branches/m3-common-artifact-filters/src/main/java/org/apache/maven/shared/artifact/filter/collection/ArtifactTransitivityFilter.java
Wed Jun 10 20:58:47 2015
@@ -20,18 +20,17 @@ package org.apache.maven.shared.artifact
  */
 
 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.resolver.filter.ScopeArtifactFilter;
+import org.apache.maven.project.DependencyResolutionResult;
+import org.apache.maven.project.ProjectBuilder;
 import org.apache.maven.project.ProjectBuildingException;
+import org.apache.maven.project.ProjectBuildingRequest;
+import org.apache.maven.project.ProjectBuildingResult;
 import org.apache.maven.project.artifact.InvalidDependencyVersionException;
-import org.apache.maven.shared.dependency.graph.DependencyNode;
-import org.apache.maven.shared.dependency.graph.filter.ArtifactDependencyNodeFilter;
-import org.apache.maven.shared.dependency.graph.filter.DependencyNodeFilter;
-import org.apache.maven.shared.dependency.graph.traversal.BuildingDependencyNodeVisitor;
-import org.apache.maven.shared.dependency.graph.traversal.CollectingDependencyNodeVisitor;
-import org.apache.maven.shared.dependency.graph.traversal.FilteringDependencyNodeVisitor;
 
 /**
  * This filter will exclude everything that is not a dependency of the selected dependencyNode.
@@ -42,35 +41,71 @@ import org.apache.maven.shared.dependenc
 public class ArtifactTransitivityFilter
     extends AbstractArtifactsFilter
 {
-
     /**
      * List of dependencyConflictIds of transitiveArtifacts
      */
     private Set<String> transitiveArtifacts;
 
-    /**
-     * @TODO describe for to get a DependencyNode based on Artifact or Dependency
-     * 
-     */
-    public ArtifactTransitivityFilter( DependencyNode node )
+    public ArtifactTransitivityFilter( Artifact artifact, ProjectBuildingRequest buildingRequest,
+                                       ProjectBuilder projectBuilder )
         throws ProjectBuildingException, InvalidDependencyVersionException
     {
-        CollectingDependencyNodeVisitor collectingVisitor = new CollectingDependencyNodeVisitor();
-     
-        DependencyNodeFilter dependencyFilter = new ArtifactDependencyNodeFilter( new ScopeArtifactFilter(
Artifact.SCOPE_TEST ) );
-        
-        FilteringDependencyNodeVisitor filteringVisitor = new FilteringDependencyNodeVisitor(
collectingVisitor, dependencyFilter );
-        
-        BuildingDependencyNodeVisitor buildingVisitor = new BuildingDependencyNodeVisitor(
filteringVisitor );
-
-        buildingVisitor.visit( node );
-        
-        for( DependencyNode collectedNode : collectingVisitor.getNodes() )
+        ProjectBuildingResult buildingResult = projectBuilder.build( artifact, buildingRequest
);
+
+        DependencyResolutionResult resolutionResult = buildingResult.getDependencyResolutionResult();
+        if ( resolutionResult != null )
         {
-            transitiveArtifacts.add( collectedNode.getArtifact().getDependencyConflictId()
);
+            if ( isMaven31() )
+            {
+                try
+                {
+                    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
+                {
+                    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 )
     {
 
@@ -95,4 +130,26 @@ public class ArtifactTransitivityFilter
     {
         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
+        {
+            Thread.currentThread().getContextClassLoader().loadClass( className );
+
+            return true;
+        }
+        catch ( ClassNotFoundException e )
+        {
+            return false;
+        }
+    }
 }



Mime
View raw message