From: Olivier Lamy Date: Thu, 2 Aug 2012 16:22:37 +0000 (+0000) Subject: use directly aether api to get dependency tree rather than passing tru maven resoluti... X-Git-Tag: archiva-1.4-M3~474 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=4c24639c91c228a9068791a6b4de4feb681c22e2;p=archiva.git use directly aether api to get dependency tree rather than passing tru maven resolution mechanism git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1368579 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultBrowseService.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultBrowseService.java index 7896ec50c..67397616f 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultBrowseService.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultBrowseService.java @@ -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 treeEntries = new ArrayList(); 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; } diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/utils/TreeDependencyNodeVisitor.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/utils/TreeDependencyNodeVisitor.java index 13cd7760b..22e4b0af2 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/utils/TreeDependencyNodeVisitor.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/utils/TreeDependencyNodeVisitor.java @@ -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 treeEntries; @@ -40,6 +41,8 @@ public class TreeDependencyNodeVisitor private DependencyNode firstNode; + private org.sonatype.aether.graph.DependencyNode firstDependencyNode; + public TreeDependencyNodeVisitor( List 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; + } } diff --git a/archiva-modules/archiva-web/archiva-webapp-js/pom.xml b/archiva-modules/archiva-web/archiva-webapp-js/pom.xml index b8ff66907..b30f8a57c 100644 --- a/archiva-modules/archiva-web/archiva-webapp-js/pom.xml +++ b/archiva-modules/archiva-web/archiva-webapp-js/pom.xml @@ -258,6 +258,20 @@ wagon-file runtime + + + org.sonatype.aether + aether-api + + + org.sonatype.aether + aether-impl + + + org.sonatype.aether + aether-util + + org.apache.httpcomponents httpclient diff --git a/archiva-modules/plugins/maven2-repository/pom.xml b/archiva-modules/plugins/maven2-repository/pom.xml index d22ad884d..49fb07e4b 100644 --- a/archiva-modules/plugins/maven2-repository/pom.xml +++ b/archiva-modules/plugins/maven2-repository/pom.xml @@ -89,6 +89,32 @@ org.apache.maven maven-model + + + org.apache.maven + maven-aether-provider + ${maven3x.version} + + + + org.sonatype.aether + aether-api + + + org.sonatype.aether + aether-impl + + + org.sonatype.aether + aether-util + + + + org.sonatype.aether + aether-connector-file + ${aether.version} + + org.apache.maven.shared maven-dependency-tree diff --git a/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/dependency/tree/maven2/Maven3DependencyTreeBuilder.java b/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/dependency/tree/maven2/Maven3DependencyTreeBuilder.java index 30822d244..0b99194d2 100644 --- a/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/dependency/tree/maven2/Maven3DependencyTreeBuilder.java +++ b/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/dependency/tree/maven2/Maven3DependencyTreeBuilder.java @@ -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 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 artifactDownloads, + Collection metadataDownloads ) + { + log.debug( "get" ); + } + + public void put( Collection artifactUploads, + Collection 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 ) ) diff --git a/archiva-modules/plugins/maven2-repository/src/test/java/org/apache/archiva/dependency/tree/maven2/DependencyTreeBuilderTestMaven3.java b/archiva-modules/plugins/maven2-repository/src/test/java/org/apache/archiva/dependency/tree/maven2/DependencyTreeBuilderTestMaven3.java index 168854b6f..fe4089b11 100644 --- a/archiva-modules/plugins/maven2-repository/src/test/java/org/apache/archiva/dependency/tree/maven2/DependencyTreeBuilderTestMaven3.java +++ b/archiva-modules/plugins/maven2-repository/src/test/java/org/apache/archiva/dependency/tree/maven2/DependencyTreeBuilderTestMaven3.java @@ -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 d2276113f..427e4bb99 100644 --- a/pom.xml +++ b/pom.xml @@ -62,6 +62,8 @@ 2.2.3.1 3.0.4 2.0.8 + 1.13.1 + 2.0-SNAPSHOT 2.1 4.1.3-SNAPSHOT @@ -777,6 +779,23 @@ + + + org.sonatype.aether + aether-api + ${aether.version} + + + org.sonatype.aether + aether-impl + ${aether.version} + + + org.sonatype.aether + aether-util + ${aether.version} + + org.apache.maven maven-repository-metadata