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
This commit is contained in:
Olivier Lamy 2012-08-04 21:27:44 +00:00
parent 2378be5050
commit f507ef2481
3 changed files with 63 additions and 43 deletions

View File

@ -20,18 +20,18 @@ package org.apache.archiva.web.tags;
*/ */
import com.opensymphony.xwork2.ActionContext; 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.common.ArchivaException;
import org.apache.archiva.dependency.tree.maven2.Maven3DependencyTreeBuilder;
import org.apache.archiva.model.Keys; import org.apache.archiva.model.Keys;
import org.apache.archiva.security.ArchivaXworkUser; import org.apache.archiva.security.ArchivaXworkUser;
import org.apache.archiva.security.UserRepositories; import org.apache.archiva.security.UserRepositories;
import org.apache.maven.artifact.Artifact; import org.apache.commons.lang.StringUtils;
import org.apache.maven.shared.dependency.tree.DependencyNode;
import org.apache.maven.shared.dependency.tree.DependencyTreeBuilderException; import org.apache.maven.shared.dependency.tree.DependencyTreeBuilderException;
import org.apache.maven.shared.dependency.tree.traversal.DependencyNodeVisitor;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; 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 org.springframework.stereotype.Service;
import javax.inject.Inject; import javax.inject.Inject;
@ -40,8 +40,6 @@ import java.util.List;
/** /**
* DependencyTree * DependencyTree
*
*
*/ */
@Service( "dependencyTree" ) @Service( "dependencyTree" )
public class DependencyTree public class DependencyTree
@ -50,7 +48,7 @@ public class DependencyTree
@Inject @Inject
private DependencyTreeBuilder dependencyTreeBuilder; private Maven3DependencyTreeBuilder dependencyTreeBuilder;
@Inject @Inject
private UserRepositories userRepositories; private UserRepositories userRepositories;
@ -129,7 +127,7 @@ public class DependencyTree
dependencyTreeBuilder.buildDependencyTree( userRepositories.getObservableRepositoryIds( getPrincipal() ), dependencyTreeBuilder.buildDependencyTree( userRepositories.getObservableRepositoryIds( getPrincipal() ),
groupId, artifactId, modelVersion, visitor ); groupId, artifactId, modelVersion, visitor );
} }
catch ( DependencyTreeBuilderException e ) catch ( Exception e )
{ {
throw new ArchivaException( "Unable to build dependency tree: " + e.getMessage(), e ); throw new ArchivaException( "Unable to build dependency tree: " + e.getMessage(), e );
} }
@ -143,7 +141,7 @@ public class DependencyTree
} }
private static class TreeListVisitor private static class TreeListVisitor
implements DependencyNodeVisitor implements DependencyVisitor
{ {
private List<TreeEntry> list; private List<TreeEntry> list;
@ -163,7 +161,7 @@ public class DependencyTree
return this.list; return this.list;
} }
public boolean visit( DependencyNode node ) public boolean visitEnter( DependencyNode node )
{ {
if ( firstNode == null ) if ( firstNode == null )
{ {
@ -178,7 +176,7 @@ public class DependencyTree
} }
currentEntry.appendPre( "<li>" ); currentEntry.appendPre( "<li>" );
currentEntry.setArtifact( node.getArtifact() ); currentEntry.setArtifact( node.getDependency().getArtifact() );
currentEntry.appendPost( "</li>" ); currentEntry.appendPost( "</li>" );
this.list.add( currentEntry ); this.list.add( currentEntry );
@ -190,7 +188,7 @@ public class DependencyTree
return true; return true;
} }
public boolean endVisit( DependencyNode node ) public boolean visitLeave( org.sonatype.aether.graph.DependencyNode node )
{ {
firstChild = false; firstChild = false;
@ -206,5 +204,7 @@ public class DependencyTree
return true; return true;
} }
} }
} }

View File

@ -26,26 +26,26 @@ import com.opensymphony.xwork2.inject.Container;
import com.opensymphony.xwork2.util.ValueStack; import com.opensymphony.xwork2.util.ValueStack;
import com.opensymphony.xwork2.util.ValueStackFactory; import com.opensymphony.xwork2.util.ValueStackFactory;
import junit.framework.TestCase; import junit.framework.TestCase;
import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
import org.apache.archiva.metadata.model.ProjectVersionMetadata;
import org.apache.archiva.metadata.repository.RepositorySession;
import org.apache.archiva.webtest.memory.TestMetadataResolver;
import org.apache.archiva.webtest.memory.TestRepositorySessionFactory;
import org.apache.archiva.common.ArchivaException; import org.apache.archiva.common.ArchivaException;
import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
import org.apache.archiva.configuration.ArchivaConfiguration; import org.apache.archiva.configuration.ArchivaConfiguration;
import org.apache.archiva.configuration.Configuration; import org.apache.archiva.configuration.Configuration;
import org.apache.archiva.configuration.ManagedRepositoryConfiguration; import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
import org.apache.maven.artifact.Artifact; import org.apache.archiva.metadata.model.ProjectVersionMetadata;
import org.apache.maven.artifact.factory.ArtifactFactory; 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.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; 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.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import java.util.List;
import javax.inject.Inject; 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.mock;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@ -65,7 +65,6 @@ public class DependencyTreeTest
@Inject @Inject
private ApplicationContext applicationContext; private ApplicationContext applicationContext;
private ArtifactFactory artifactFactory;
private static final String TEST_VERSION = "version"; private static final String TEST_VERSION = "version";
@ -103,8 +102,6 @@ public class DependencyTreeTest
ArchivaConfiguration archivaConfiguration = applicationContext.getBean( ArchivaConfiguration.class ); ArchivaConfiguration archivaConfiguration = applicationContext.getBean( ArchivaConfiguration.class );
archivaConfiguration.save( configuration ); archivaConfiguration.save( configuration );
artifactFactory = plexusSisuBridge.lookup( ArtifactFactory.class );
TestMetadataResolver metadataResolver = applicationContext.getBean( TestMetadataResolver.class ); TestMetadataResolver metadataResolver = applicationContext.getBean( TestMetadataResolver.class );
ProjectVersionMetadata metadata = new ProjectVersionMetadata(); ProjectVersionMetadata metadata = new ProjectVersionMetadata();
metadata.setId( TEST_VERSION ); metadata.setId( TEST_VERSION );
@ -126,52 +123,67 @@ public class DependencyTreeTest
DependencyTree.TreeEntry artifactId = entries.get( 0 ); DependencyTree.TreeEntry artifactId = entries.get( 0 );
assertEquals( "<ul><li>", artifactId.getPre() ); assertEquals( "<ul><li>", artifactId.getPre() );
assertEquals( createPomArtifact( TEST_GROUP_ID, TEST_ARTIFACT_ID, TEST_VERSION ), artifactId.getArtifact() ); // olamy tree with aether always create jar so createPomArtifact failed but it's not used
assertTrue( assertArtifactsEquals( createArtifact( TEST_GROUP_ID, TEST_ARTIFACT_ID, TEST_VERSION ),
artifactId.getArtifact() ) );
assertEquals( "</li>", artifactId.getPost() ); assertEquals( "</li>", artifactId.getPost() );
DependencyTree.TreeEntry child1 = entries.get( 1 ); DependencyTree.TreeEntry child1 = entries.get( 1 );
assertEquals( "<ul><li>", child1.getPre() ); assertEquals( "<ul><li>", child1.getPre() );
assertEquals( createArtifact( TEST_GROUP_ID, "child1", "1.0" ), child1.getArtifact() ); assertTrue( assertArtifactsEquals( createArtifact( TEST_GROUP_ID, "child1", "1.0" ), child1.getArtifact() ) );
assertEquals( "</li>", child1.getPost() ); assertEquals( "</li>", child1.getPost() );
DependencyTree.TreeEntry grandchild = entries.get( 2 ); DependencyTree.TreeEntry grandchild = entries.get( 2 );
assertEquals( "<ul><li>", grandchild.getPre() ); assertEquals( "<ul><li>", grandchild.getPre() );
assertEquals( createArtifact( TEST_GROUP_ID, "grandchild1", "2.0" ), grandchild.getArtifact() ); assertTrue(
assertArtifactsEquals( createArtifact( TEST_GROUP_ID, "grandchild1", "2.0" ), grandchild.getArtifact() ) );
assertEquals( "</li>", grandchild.getPost() ); assertEquals( "</li>", grandchild.getPost() );
DependencyTree.TreeEntry greatGrandchild = entries.get( 3 ); DependencyTree.TreeEntry greatGrandchild = entries.get( 3 );
assertEquals( "<ul><li>", greatGrandchild.getPre() ); assertEquals( "<ul><li>", greatGrandchild.getPre() );
assertEquals( createArtifact( TEST_GROUP_ID, "great-grandchild", "3.0" ), greatGrandchild.getArtifact() ); assertTrue( assertArtifactsEquals( createArtifact( TEST_GROUP_ID, "great-grandchild", "3.0" ),
greatGrandchild.getArtifact() ) );
assertEquals( "</li></ul></ul>", greatGrandchild.getPost() ); assertEquals( "</li></ul></ul>", greatGrandchild.getPost() );
DependencyTree.TreeEntry child2 = entries.get( 4 ); DependencyTree.TreeEntry child2 = entries.get( 4 );
assertEquals( "<li>", child2.getPre() ); assertEquals( "<li>", child2.getPre() );
assertEquals( createArtifact( TEST_GROUP_ID, "child2", "1.0" ), child2.getArtifact() ); assertTrue( assertArtifactsEquals( createArtifact( TEST_GROUP_ID, "child2", "1.0" ), child2.getArtifact() ) );
assertEquals( "</li>", child2.getPost() ); assertEquals( "</li>", child2.getPost() );
DependencyTree.TreeEntry grandchild2 = entries.get( 5 ); DependencyTree.TreeEntry grandchild2 = entries.get( 5 );
assertEquals( "<ul><li>", grandchild2.getPre() ); assertEquals( "<ul><li>", grandchild2.getPre() );
assertEquals( createArtifact( TEST_GROUP_ID, "grandchild2", "2.0" ), grandchild2.getArtifact() ); assertTrue(
assertArtifactsEquals( createArtifact( TEST_GROUP_ID, "grandchild2", "2.0" ), grandchild2.getArtifact() ) );
assertEquals( "</li>", grandchild2.getPost() ); assertEquals( "</li>", grandchild2.getPost() );
DependencyTree.TreeEntry grandchild3 = entries.get( 6 ); DependencyTree.TreeEntry grandchild3 = entries.get( 6 );
assertEquals( "<li>", grandchild3.getPre() ); assertEquals( "<li>", grandchild3.getPre() );
assertEquals( createArtifact( TEST_GROUP_ID, "grandchild3", "2.0" ), grandchild3.getArtifact() ); assertTrue(
assertArtifactsEquals( createArtifact( TEST_GROUP_ID, "grandchild3", "2.0" ), grandchild3.getArtifact() ) );
assertEquals( "</li></ul>", grandchild3.getPost() ); assertEquals( "</li></ul>", grandchild3.getPost() );
DependencyTree.TreeEntry child3 = entries.get( 7 ); DependencyTree.TreeEntry child3 = entries.get( 7 );
assertEquals( "<li>", child3.getPre() ); assertEquals( "<li>", child3.getPre() );
assertEquals( createArtifact( TEST_GROUP_ID, "child3", "1.0" ), child3.getArtifact() ); assertTrue( assertArtifactsEquals( createArtifact( TEST_GROUP_ID, "child3", "1.0" ), child3.getArtifact() ) );
assertEquals( "</li></ul></ul>", child3.getPost() ); assertEquals( "</li></ul></ul>", child3.getPost() );
} }
private Artifact createPomArtifact( String groupId, String artifactId, String version ) 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 ) 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() );
}
} }

View File

@ -68,7 +68,6 @@ import javax.inject.Inject;
import javax.inject.Named; import javax.inject.Named;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -122,8 +121,8 @@ public class Maven3DependencyTreeBuilder
builder = defaultModelBuilderFactory.newInstance(); builder = defaultModelBuilderFactory.newInstance();
} }
public List<TreeEntry> buildDependencyTree( List<String> repositoryIds, String groupId, String artifactId, public void buildDependencyTree( List<String> repositoryIds, String groupId, String artifactId, String version,
String version ) DependencyVisitor dependencyVisitor )
throws Exception throws Exception
{ {
Artifact projectArtifact = factory.createProjectArtifact( groupId, artifactId, version ); Artifact projectArtifact = factory.createProjectArtifact( groupId, artifactId, version );
@ -141,7 +140,7 @@ public class Maven3DependencyTreeBuilder
if ( repository == null ) if ( repository == null )
{ {
// metadata could not be resolved // metadata could not be resolved
return Collections.emptyList(); return;
} }
// MRM-1411 // MRM-1411
@ -168,11 +167,20 @@ public class Maven3DependencyTreeBuilder
} }
} }
// FIXME take care of relative path
resolve( repository.getLocation(), groupId, artifactId, version, dependencyVisitor );
}
public List<TreeEntry> buildDependencyTree( List<String> repositoryIds, String groupId, String artifactId,
String version )
throws Exception
{
List<TreeEntry> treeEntries = new ArrayList<TreeEntry>(); List<TreeEntry> treeEntries = new ArrayList<TreeEntry>();
TreeDependencyNodeVisitor treeDependencyNodeVisitor = new TreeDependencyNodeVisitor( treeEntries ); TreeDependencyNodeVisitor treeDependencyNodeVisitor = new TreeDependencyNodeVisitor( treeEntries );
// FIXME take care of relative path buildDependencyTree( repositoryIds, groupId, artifactId, version, treeDependencyNodeVisitor );
resolve( repository.getLocation(), groupId, artifactId, version, treeDependencyNodeVisitor );
log.debug( "treeEntrie: {}", treeEntries ); log.debug( "treeEntrie: {}", treeEntries );
return treeEntries; return treeEntries;