]> source.dussan.org Git - archiva.git/commitdiff
use directly aether api to get dependency tree rather than passing tru maven resoluti...
authorOlivier Lamy <olamy@apache.org>
Thu, 2 Aug 2012 16:22:37 +0000 (16:22 +0000)
committerOlivier Lamy <olamy@apache.org>
Thu, 2 Aug 2012 16:22:37 +0000 (16:22 +0000)
git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1368579 13f79535-47bb-0310-9956-ffa450edef68

archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultBrowseService.java
archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/utils/TreeDependencyNodeVisitor.java
archiva-modules/archiva-web/archiva-webapp-js/pom.xml
archiva-modules/plugins/maven2-repository/pom.xml
archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/dependency/tree/maven2/Maven3DependencyTreeBuilder.java
archiva-modules/plugins/maven2-repository/src/test/java/org/apache/archiva/dependency/tree/maven2/DependencyTreeBuilderTestMaven3.java
pom.xml

index 7896ec50c75628139a5d2a64753951068735452e..67397616feacadd984b9b898a13f5f3bbea70e2b 100644 (file)
@@ -21,6 +21,7 @@ package org.apache.archiva.rest.services;
 import org.apache.archiva.admin.model.beans.ManagedRepository;
 import org.apache.archiva.common.utils.VersionComparator;
 import org.apache.archiva.dependency.tree.maven2.DependencyTreeBuilder;
+import org.apache.archiva.dependency.tree.maven2.Maven3DependencyTreeBuilder;
 import org.apache.archiva.metadata.generic.GenericMetadataFacet;
 import org.apache.archiva.metadata.model.ArtifactMetadata;
 import org.apache.archiva.metadata.model.MetadataFacet;
@@ -56,6 +57,7 @@ import org.apache.commons.collections.CollectionUtils;
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.io.IOUtils;
 import org.apache.commons.lang.StringUtils;
+import org.apache.maven.project.DependencyResolutionResult;
 import org.apache.maven.shared.dependency.tree.DependencyTreeBuilderException;
 import org.springframework.stereotype.Service;
 
@@ -91,6 +93,9 @@ public class DefaultBrowseService
     @Inject
     private DependencyTreeBuilder dependencyTreeBuilder;
 
+    @Inject
+    private Maven3DependencyTreeBuilder maven3DependencyTreeBuilder;
+
     @Inject
     private RepositoryContentFactory repositoryContentFactory;
 
@@ -426,6 +431,7 @@ public class DefaultBrowseService
 
         List<TreeEntry> treeEntries = new ArrayList<TreeEntry>();
         TreeDependencyNodeVisitor treeDependencyNodeVisitor = new TreeDependencyNodeVisitor( treeEntries );
+        /*
         try
         {
             dependencyTreeBuilder.buildDependencyTree( selectedRepos, groupId, artifactId, version,
@@ -436,6 +442,18 @@ public class DefaultBrowseService
             throw new ArchivaRestServiceException( e.getMessage(),
                                                    Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e );
         }
+        */
+        try
+        {
+            DependencyResolutionResult result =
+                maven3DependencyTreeBuilder.buildDependencyTree( selectedRepos, groupId, artifactId, version, treeDependencyNodeVisitor );
+            log.debug( "result: {}", result );
+        }
+        catch ( Exception e )
+        {
+            log.error( e.getMessage(), e );
+        }
+
         return treeEntries;
     }
 
index 13cd7760bb6e62e88441892a460517963379d8e3..22e4b0af279a373eadb59995f0667eab9946433b 100644 (file)
@@ -23,6 +23,7 @@ import org.apache.archiva.rest.api.model.Artifact;
 import org.apache.archiva.rest.api.model.TreeEntry;
 import org.apache.maven.shared.dependency.tree.DependencyNode;
 import org.apache.maven.shared.dependency.tree.traversal.DependencyNodeVisitor;
+import org.sonatype.aether.graph.DependencyVisitor;
 
 import java.util.List;
 
@@ -31,7 +32,7 @@ import java.util.List;
  * @since 1.4-M3
  */
 public class TreeDependencyNodeVisitor
-    implements DependencyNodeVisitor
+    implements DependencyNodeVisitor, DependencyVisitor
 {
 
     final List<TreeEntry> treeEntries;
@@ -40,6 +41,8 @@ public class TreeDependencyNodeVisitor
 
     private DependencyNode firstNode;
 
+    private org.sonatype.aether.graph.DependencyNode firstDependencyNode;
+
     public TreeDependencyNodeVisitor( List<TreeEntry> treeEntries )
     {
         this.treeEntries = treeEntries;
@@ -69,4 +72,27 @@ public class TreeDependencyNodeVisitor
         return true;
     }
 
+    public boolean visitEnter( org.sonatype.aether.graph.DependencyNode dependencyNode )
+    {
+        TreeEntry entry = new TreeEntry( new BeanReplicator().replicateBean( dependencyNode.getDependency().getArtifact(), Artifact.class ) );
+        entry.setParent( currentEntry );
+        currentEntry = entry;
+
+        if ( firstDependencyNode == null )
+        {
+            firstDependencyNode = dependencyNode;
+            treeEntries.add( currentEntry );
+        }
+        else
+        {
+            currentEntry.getParent().getChilds().add( currentEntry );
+        }
+        return true;
+    }
+
+    public boolean visitLeave( org.sonatype.aether.graph.DependencyNode dependencyNode )
+    {
+        currentEntry = currentEntry.getParent();
+        return true;
+    }
 }
index b8ff669074041bf33a26c07ce082bbf1c10287e1..b30f8a57cb17e62133536c6c3c24394328faf9a1 100644 (file)
       <artifactId>wagon-file</artifactId>
       <scope>runtime</scope>
     </dependency>
+
+    <dependency>
+      <groupId>org.sonatype.aether</groupId>
+      <artifactId>aether-api</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.sonatype.aether</groupId>
+      <artifactId>aether-impl</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.sonatype.aether</groupId>
+      <artifactId>aether-util</artifactId>
+    </dependency>
+
     <dependency>
       <groupId>org.apache.httpcomponents</groupId>
       <artifactId>httpclient</artifactId>
index d22ad884d99353a342f08295f3ef2decd3ee5937..49fb07e4bf07f3eed9b70d976c0c4be14c46a9ec 100644 (file)
       <groupId>org.apache.maven</groupId>
       <artifactId>maven-model</artifactId>
     </dependency>
+
+    <dependency>
+      <groupId>org.apache.maven</groupId>
+      <artifactId>maven-aether-provider</artifactId>
+      <version>${maven3x.version}</version>
+    </dependency>
+
+    <dependency>
+      <groupId>org.sonatype.aether</groupId>
+      <artifactId>aether-api</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.sonatype.aether</groupId>
+      <artifactId>aether-impl</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.sonatype.aether</groupId>
+      <artifactId>aether-util</artifactId>
+    </dependency>
+
+    <dependency>
+      <groupId>org.sonatype.aether</groupId>
+      <artifactId>aether-connector-file</artifactId>
+      <version>${aether.version}</version>
+    </dependency>
+
     <dependency>
       <groupId>org.apache.maven.shared</groupId>
       <artifactId>maven-dependency-tree</artifactId>
index 30822d2446e430d702f2674ca9361580226e1a76..0b99194d2dd46bda1799fc32f42861028a784e3e 100644 (file)
@@ -51,6 +51,10 @@ import org.apache.maven.project.DefaultProjectBuildingRequest;
 import org.apache.maven.project.DependencyResolutionException;
 import org.apache.maven.project.DependencyResolutionResult;
 import org.apache.maven.project.MavenProject;
+import org.apache.maven.repository.internal.DefaultArtifactDescriptorReader;
+import org.apache.maven.repository.internal.DefaultVersionRangeResolver;
+import org.apache.maven.repository.internal.DefaultVersionResolver;
+import org.apache.maven.repository.internal.MavenRepositorySystemSession;
 import org.codehaus.plexus.util.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -60,14 +64,30 @@ import org.sonatype.aether.RequestTrace;
 import org.sonatype.aether.artifact.ArtifactType;
 import org.sonatype.aether.artifact.ArtifactTypeRegistry;
 import org.sonatype.aether.collection.CollectRequest;
+import org.sonatype.aether.collection.CollectResult;
 import org.sonatype.aether.collection.DependencyCollectionException;
+import org.sonatype.aether.connector.file.FileRepositoryConnectorFactory;
 import org.sonatype.aether.graph.Dependency;
 import org.sonatype.aether.graph.DependencyFilter;
+import org.sonatype.aether.graph.DependencyVisitor;
+import org.sonatype.aether.impl.ArtifactDescriptorReader;
+import org.sonatype.aether.impl.VersionRangeResolver;
+import org.sonatype.aether.impl.VersionResolver;
+import org.sonatype.aether.impl.internal.DefaultServiceLocator;
 import org.sonatype.aether.impl.internal.SimpleLocalRepositoryManager;
+import org.sonatype.aether.repository.LocalRepository;
 import org.sonatype.aether.resolution.DependencyRequest;
+import org.sonatype.aether.spi.connector.ArtifactDownload;
+import org.sonatype.aether.spi.connector.ArtifactUpload;
+import org.sonatype.aether.spi.connector.MetadataDownload;
+import org.sonatype.aether.spi.connector.MetadataUpload;
+import org.sonatype.aether.spi.connector.RepositoryConnector;
+import org.sonatype.aether.spi.connector.RepositoryConnectorFactory;
+import org.sonatype.aether.transfer.NoRepositoryConnectorException;
 import org.sonatype.aether.util.DefaultRepositorySystemSession;
 import org.sonatype.aether.util.DefaultRequestTrace;
 import org.sonatype.aether.util.artifact.ArtifacIdUtils;
+import org.sonatype.aether.util.artifact.DefaultArtifact;
 import org.sonatype.aether.util.artifact.JavaScopes;
 import org.sonatype.aether.version.VersionConstraint;
 import org.springframework.stereotype.Service;
@@ -133,7 +153,8 @@ public class Maven3DependencyTreeBuilder
     }
 
     public DependencyResolutionResult buildDependencyTree( List<String> repositoryIds, String groupId,
-                                                           String artifactId, String version )
+                                                           String artifactId, String version,
+                                                           DependencyVisitor dependencyVisitor )
         throws Exception
     {
         Artifact projectArtifact = factory.createProjectArtifact( groupId, artifactId, version );
@@ -217,6 +238,9 @@ public class Maven3DependencyTreeBuilder
 
         log.debug( "dependency graph build" );
 
+        // FIXME take care of relative path
+        test( repository.getLocation(), groupId, artifactId, version, dependencyVisitor );
+
         return resolutionResult;
     }
 
@@ -296,6 +320,10 @@ public class Maven3DependencyTreeBuilder
             }
         }
 
+        collect.setRoot( new org.sonatype.aether.graph.Dependency(
+            new org.sonatype.aether.util.artifact.DefaultArtifact( project.getGroupId(), project.getArtifactId(), null,
+                                                                   project.getVersion() ), "compile" ) );
+
         DependencyRequest depRequest = new DependencyRequest( collect, filter );
         depRequest.setTrace( trace );
 
@@ -321,6 +349,112 @@ public class Maven3DependencyTreeBuilder
         return result;
     }
 
+    private void test( String localRepoDir, String groupId, String artifactId, String version,
+                       DependencyVisitor dependencyVisitor )
+    {
+
+        RepositorySystem system = newRepositorySystem();
+
+        RepositorySystemSession session = newRepositorySystemSession( system, localRepoDir );
+
+        org.sonatype.aether.artifact.Artifact artifact =
+            new DefaultArtifact( groupId + ":" + artifactId + ":" + version );
+
+        //RemoteRepository repo = Booter.newCentralRepository();
+
+        CollectRequest collectRequest = new CollectRequest();
+        collectRequest.setRoot( new Dependency( artifact, "" ) );
+        //collectRequest.addRepository( repo );
+
+        try
+        {
+            CollectResult collectResult = system.collectDependencies( session, collectRequest );
+            collectResult.getRoot().accept( dependencyVisitor );
+            log.debug( "test" );
+        }
+        catch ( DependencyCollectionException e )
+        {
+            log.error( e.getMessage(), e );
+        }
+
+
+    }
+
+    public static class MyFileRepositoryConnectorFactory
+        extends FileRepositoryConnectorFactory
+    {
+
+        public MyFileRepositoryConnectorFactory()
+        {
+
+        }
+
+        public RepositoryConnector newInstance( RepositorySystemSession session,
+                                                org.sonatype.aether.repository.RemoteRepository repository )
+            throws NoRepositoryConnectorException
+        {
+
+            try
+            {
+                return super.newInstance( session, repository );
+            }
+            catch ( NoRepositoryConnectorException e )
+            {
+
+            }
+
+            return new RepositoryConnector()
+            {
+
+                private Logger log = LoggerFactory.getLogger( getClass() );
+
+                public void get( Collection<? extends ArtifactDownload> artifactDownloads,
+                                 Collection<? extends MetadataDownload> metadataDownloads )
+                {
+                    log.debug( "get" );
+                }
+
+                public void put( Collection<? extends ArtifactUpload> artifactUploads,
+                                 Collection<? extends MetadataUpload> metadataUploads )
+                {
+                    log.debug( "put" );
+                }
+
+                public void close()
+                {
+                    log.debug( "close" );
+                }
+            };
+        }
+    }
+
+    public static RepositorySystem newRepositorySystem()
+    {
+        DefaultServiceLocator locator = new DefaultServiceLocator();
+        locator.addService( RepositoryConnectorFactory.class,
+                            MyFileRepositoryConnectorFactory.class );// FileRepositoryConnectorFactory.class );
+        locator.addService( VersionResolver.class, DefaultVersionResolver.class );
+        locator.addService( VersionRangeResolver.class, DefaultVersionRangeResolver.class );
+        locator.addService( ArtifactDescriptorReader.class, DefaultArtifactDescriptorReader.class );
+        //locator.addService( RepositoryConnectorFactory.class, WagonRepositoryConnectorFactory.class );
+        //locator.setServices( WagonProvider.class,  );
+
+        return locator.getService( RepositorySystem.class );
+    }
+
+    public static RepositorySystemSession newRepositorySystemSession( RepositorySystem system, String localRepoDir )
+    {
+        MavenRepositorySystemSession session = new MavenRepositorySystemSession();
+
+        LocalRepository localRepo = new LocalRepository( localRepoDir );
+        session.setLocalRepositoryManager( system.newLocalRepositoryManager( localRepo ) );
+
+        //session.setTransferListener( new ConsoleTransferListener() );
+        //session.setRepositoryListener( new ConsoleRepositoryListener() );
+
+        return session;
+    }
+
     private String getVersionSelectedFromRange( VersionConstraint constraint )
     {
         if ( ( constraint == null ) || ( constraint.getVersion() != null ) )
index 168854b6f2cdb5000109b74957f0cd1a167c3a09..fe4089b11d9b89246d60056743c601bd69492eae 100644 (file)
@@ -37,6 +37,7 @@ import org.sonatype.aether.collection.CollectResult;
 import org.sonatype.aether.collection.DependencyCollectionException;
 import org.sonatype.aether.graph.Dependency;
 import org.sonatype.aether.graph.DependencyNode;
+import org.sonatype.aether.graph.DependencyVisitor;
 import org.sonatype.aether.impl.DependencyCollector;
 import org.sonatype.aether.impl.internal.DefaultRepositorySystem;
 import org.sonatype.aether.util.artifact.DefaultArtifact;
@@ -301,7 +302,18 @@ public class DependencyTreeBuilderTestMaven3
 
         DependencyResolutionResult resolutionResult =
             builder.buildDependencyTree( Collections.singletonList( TEST_REPO_ID ), TEST_GROUP_ID, TEST_ARTIFACT_ID,
-                                         TEST_VERSION );
+                                         TEST_VERSION, new DependencyVisitor()
+            {
+                public boolean visitEnter( DependencyNode dependencyNode )
+                {
+                    return true;
+                }
+
+                public boolean visitLeave( DependencyNode dependencyNode )
+                {
+                    return true;
+                }
+            } );
 
         assertNotNull( resolutionResult );
         assertEquals( 10, resolutionResult.getDependencies().size() );
diff --git a/pom.xml b/pom.xml
index d2276113f2507751d9f9bde7a9944f554058f2dd..427e4bb994c04e54681cacf15d61ca95b1dc6122 100644 (file)
--- a/pom.xml
+++ b/pom.xml
@@ -62,6 +62,8 @@
     <struts.version>2.2.3.1</struts.version>
     <maven3x.version>3.0.4</maven3x.version>
     <maven.version>2.0.8</maven.version>
+    <aether.version>1.13.1</aether.version>
+
     <maven-dependency-tree.version>2.0-SNAPSHOT</maven-dependency-tree.version>
     <maven-model-converter.version>2.1</maven-model-converter.version>
     <maven.indexer.version>4.1.3-SNAPSHOT</maven.indexer.version>
           </exclusion>
         </exclusions>
       </dependency>
+
+      <dependency>
+        <groupId>org.sonatype.aether</groupId>
+        <artifactId>aether-api</artifactId>
+        <version>${aether.version}</version>
+      </dependency>
+      <dependency>
+        <groupId>org.sonatype.aether</groupId>
+        <artifactId>aether-impl</artifactId>
+        <version>${aether.version}</version>
+      </dependency>
+      <dependency>
+        <groupId>org.sonatype.aether</groupId>
+        <artifactId>aether-util</artifactId>
+        <version>${aether.version}</version>
+      </dependency>
+
       <dependency>
         <groupId>org.apache.maven</groupId>
         <artifactId>maven-repository-metadata</artifactId>