From f507ef248154800740d3abde4e16c3439046be38 Mon Sep 17 00:00:00 2001 From: Olivier Lamy Date: Sat, 4 Aug 2012 21:27:44 +0000 Subject: [PATCH] use dependency tree with aether for old webapp too git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1369471 13f79535-47bb-0310-9956-ffa450edef68 --- .../archiva/web/tags/DependencyTree.java | 26 ++++----- .../archiva/web/tags/DependencyTreeTest.java | 58 +++++++++++-------- .../maven2/Maven3DependencyTreeBuilder.java | 20 +++++-- 3 files changed, 62 insertions(+), 42 deletions(-) diff --git a/archiva-modules/archiva-web/archiva-webapp/src/main/java/org/apache/archiva/web/tags/DependencyTree.java b/archiva-modules/archiva-web/archiva-webapp/src/main/java/org/apache/archiva/web/tags/DependencyTree.java index 0a73834f8..cab7afee6 100644 --- a/archiva-modules/archiva-web/archiva-webapp/src/main/java/org/apache/archiva/web/tags/DependencyTree.java +++ b/archiva-modules/archiva-web/archiva-webapp/src/main/java/org/apache/archiva/web/tags/DependencyTree.java @@ -20,18 +20,18 @@ package org.apache.archiva.web.tags; */ import com.opensymphony.xwork2.ActionContext; -import org.apache.archiva.dependency.tree.maven2.DependencyTreeBuilder; -import org.apache.commons.lang.StringUtils; import org.apache.archiva.common.ArchivaException; +import org.apache.archiva.dependency.tree.maven2.Maven3DependencyTreeBuilder; import org.apache.archiva.model.Keys; import org.apache.archiva.security.ArchivaXworkUser; import org.apache.archiva.security.UserRepositories; -import org.apache.maven.artifact.Artifact; -import org.apache.maven.shared.dependency.tree.DependencyNode; +import org.apache.commons.lang.StringUtils; import org.apache.maven.shared.dependency.tree.DependencyTreeBuilderException; -import org.apache.maven.shared.dependency.tree.traversal.DependencyNodeVisitor; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.sonatype.aether.artifact.Artifact; +import org.sonatype.aether.graph.DependencyNode; +import org.sonatype.aether.graph.DependencyVisitor; import org.springframework.stereotype.Service; import javax.inject.Inject; @@ -40,8 +40,6 @@ import java.util.List; /** * DependencyTree - * - * */ @Service( "dependencyTree" ) public class DependencyTree @@ -50,7 +48,7 @@ public class DependencyTree @Inject - private DependencyTreeBuilder dependencyTreeBuilder; + private Maven3DependencyTreeBuilder dependencyTreeBuilder; @Inject private UserRepositories userRepositories; @@ -129,7 +127,7 @@ public class DependencyTree dependencyTreeBuilder.buildDependencyTree( userRepositories.getObservableRepositoryIds( getPrincipal() ), groupId, artifactId, modelVersion, visitor ); } - catch ( DependencyTreeBuilderException e ) + catch ( Exception e ) { throw new ArchivaException( "Unable to build dependency tree: " + e.getMessage(), e ); } @@ -143,7 +141,7 @@ public class DependencyTree } private static class TreeListVisitor - implements DependencyNodeVisitor + implements DependencyVisitor { private List list; @@ -163,7 +161,7 @@ public class DependencyTree return this.list; } - public boolean visit( DependencyNode node ) + public boolean visitEnter( DependencyNode node ) { if ( firstNode == null ) { @@ -178,7 +176,7 @@ public class DependencyTree } currentEntry.appendPre( "
  • " ); - currentEntry.setArtifact( node.getArtifact() ); + currentEntry.setArtifact( node.getDependency().getArtifact() ); currentEntry.appendPost( "
  • " ); this.list.add( currentEntry ); @@ -190,7 +188,7 @@ public class DependencyTree return true; } - public boolean endVisit( DependencyNode node ) + public boolean visitLeave( org.sonatype.aether.graph.DependencyNode node ) { firstChild = false; @@ -206,5 +204,7 @@ public class DependencyTree return true; } + + } } diff --git a/archiva-modules/archiva-web/archiva-webapp/src/test/java/org/apache/archiva/web/tags/DependencyTreeTest.java b/archiva-modules/archiva-web/archiva-webapp/src/test/java/org/apache/archiva/web/tags/DependencyTreeTest.java index 6141fde0f..2aaffa9db 100644 --- a/archiva-modules/archiva-web/archiva-webapp/src/test/java/org/apache/archiva/web/tags/DependencyTreeTest.java +++ b/archiva-modules/archiva-web/archiva-webapp/src/test/java/org/apache/archiva/web/tags/DependencyTreeTest.java @@ -26,33 +26,33 @@ import com.opensymphony.xwork2.inject.Container; import com.opensymphony.xwork2.util.ValueStack; import com.opensymphony.xwork2.util.ValueStackFactory; import junit.framework.TestCase; +import org.apache.archiva.common.ArchivaException; import org.apache.archiva.common.plexusbridge.PlexusSisuBridge; +import org.apache.archiva.configuration.ArchivaConfiguration; +import org.apache.archiva.configuration.Configuration; +import org.apache.archiva.configuration.ManagedRepositoryConfiguration; import org.apache.archiva.metadata.model.ProjectVersionMetadata; import org.apache.archiva.metadata.repository.RepositorySession; +import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner; import org.apache.archiva.webtest.memory.TestMetadataResolver; import org.apache.archiva.webtest.memory.TestRepositorySessionFactory; -import org.apache.archiva.common.ArchivaException; -import org.apache.archiva.configuration.ArchivaConfiguration; -import org.apache.archiva.configuration.Configuration; -import org.apache.archiva.configuration.ManagedRepositoryConfiguration; -import org.apache.maven.artifact.Artifact; -import org.apache.maven.artifact.factory.ArtifactFactory; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.sonatype.aether.artifact.Artifact; +import org.sonatype.aether.util.artifact.DefaultArtifact; import org.springframework.context.ApplicationContext; import org.springframework.test.context.ContextConfiguration; -import java.util.List; import javax.inject.Inject; -import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner; +import java.util.List; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @RunWith( ArchivaSpringJUnit4ClassRunner.class ) -@ContextConfiguration( locations = {"classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml", - "classpath:/spring-context-DependencyTreeTest.xml"} ) +@ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml", + "classpath:/spring-context-DependencyTreeTest.xml" } ) public class DependencyTreeTest extends TestCase { @@ -65,7 +65,6 @@ public class DependencyTreeTest @Inject private ApplicationContext applicationContext; - private ArtifactFactory artifactFactory; private static final String TEST_VERSION = "version"; @@ -103,8 +102,6 @@ public class DependencyTreeTest ArchivaConfiguration archivaConfiguration = applicationContext.getBean( ArchivaConfiguration.class ); archivaConfiguration.save( configuration ); - artifactFactory = plexusSisuBridge.lookup( ArtifactFactory.class ); - TestMetadataResolver metadataResolver = applicationContext.getBean( TestMetadataResolver.class ); ProjectVersionMetadata metadata = new ProjectVersionMetadata(); metadata.setId( TEST_VERSION ); @@ -126,52 +123,67 @@ public class DependencyTreeTest DependencyTree.TreeEntry artifactId = entries.get( 0 ); assertEquals( "", child3.getPost() ); } private Artifact createPomArtifact( String groupId, String artifactId, String version ) { - return artifactFactory.createProjectArtifact( groupId, artifactId, version ); + return new DefaultArtifact( groupId + ":" + artifactId + ":" + version ); } private Artifact createArtifact( String groupId, String artifactId, String version ) { - return artifactFactory.createBuildArtifact( groupId, artifactId, version, "jar" ); + return new DefaultArtifact( groupId, artifactId, "jar", version ); } + + + public boolean assertArtifactsEquals( Artifact a, Artifact b ) + { + return a.getArtifactId().equals( b.getArtifactId() ) && a.getGroupId().equals( b.getGroupId() ) + && a.getVersion().equals( b.getVersion() ) && a.getExtension().equals( b.getExtension() ) + && a.getClassifier().equals( b.getClassifier() ); + } + } 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 1e03b7dfb..eec25bf37 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 @@ -68,7 +68,6 @@ import javax.inject.Inject; import javax.inject.Named; import java.io.File; import java.util.ArrayList; -import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -122,8 +121,8 @@ public class Maven3DependencyTreeBuilder builder = defaultModelBuilderFactory.newInstance(); } - public List buildDependencyTree( List repositoryIds, String groupId, String artifactId, - String version ) + public void buildDependencyTree( List repositoryIds, String groupId, String artifactId, String version, + DependencyVisitor dependencyVisitor ) throws Exception { Artifact projectArtifact = factory.createProjectArtifact( groupId, artifactId, version ); @@ -141,7 +140,7 @@ public class Maven3DependencyTreeBuilder if ( repository == null ) { // metadata could not be resolved - return Collections.emptyList(); + return; } // MRM-1411 @@ -168,11 +167,20 @@ public class Maven3DependencyTreeBuilder } } + // FIXME take care of relative path + resolve( repository.getLocation(), groupId, artifactId, version, dependencyVisitor ); + } + + + public List buildDependencyTree( List repositoryIds, String groupId, String artifactId, + String version ) + throws Exception + { + List treeEntries = new ArrayList(); TreeDependencyNodeVisitor treeDependencyNodeVisitor = new TreeDependencyNodeVisitor( treeEntries ); - // FIXME take care of relative path - resolve( repository.getLocation(), groupId, artifactId, version, treeDependencyNodeVisitor ); + buildDependencyTree( repositoryIds, groupId, artifactId, version, treeDependencyNodeVisitor ); log.debug( "treeEntrie: {}", treeEntries ); return treeEntries; -- 2.39.5