From c2aa60f68f68b138f85722a96ea22edb40ce734c Mon Sep 17 00:00:00 2001 From: Olivier Lamy Date: Thu, 2 Aug 2012 16:23:04 +0000 Subject: [PATCH] lot of code cleanup for dependency tree git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1368580 13f79535-47bb-0310-9956-ffa450edef68 --- .../rest/services/DefaultBrowseService.java | 10 +- .../plugins/maven2-repository/pom.xml | 4 - .../maven2/Maven3DependencyTreeBuilder.java | 311 +----------------- .../DependencyTreeBuilderTestMaven3.java | 30 +- 4 files changed, 26 insertions(+), 329 deletions(-) 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 67397616f..061f8e87f 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 @@ -57,8 +57,6 @@ 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; import javax.inject.Inject; @@ -445,15 +443,17 @@ public class DefaultBrowseService */ try { - DependencyResolutionResult result = - maven3DependencyTreeBuilder.buildDependencyTree( selectedRepos, groupId, artifactId, version, treeDependencyNodeVisitor ); - log.debug( "result: {}", result ); + + maven3DependencyTreeBuilder.buildDependencyTree( selectedRepos, groupId, artifactId, version, + treeDependencyNodeVisitor ); + } catch ( Exception e ) { log.error( e.getMessage(), e ); } + log.debug( "treeEntrie: {}", treeEntries ); return treeEntries; } diff --git a/archiva-modules/plugins/maven2-repository/pom.xml b/archiva-modules/plugins/maven2-repository/pom.xml index 49fb07e4b..4a10197ca 100644 --- a/archiva-modules/plugins/maven2-repository/pom.xml +++ b/archiva-modules/plugins/maven2-repository/pom.xml @@ -73,10 +73,6 @@ org.apache.maven maven-artifact - - org.apache.maven - maven-core - org.apache.maven maven-artifact-manager 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 0b99194d2..04ec05999 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 @@ -31,52 +31,30 @@ import org.apache.archiva.admin.model.remote.RemoteRepositoryAdmin; import org.apache.archiva.common.plexusbridge.PlexusSisuBridge; import org.apache.archiva.common.plexusbridge.PlexusSisuBridgeException; import org.apache.archiva.metadata.repository.storage.RepositoryPathTranslator; -import org.apache.archiva.metadata.repository.storage.maven2.RepositoryModelResolver; import org.apache.archiva.proxy.common.WagonFactory; -import org.apache.maven.RepositoryUtils; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.factory.ArtifactFactory; -import org.apache.maven.artifact.versioning.VersionRange; -import org.apache.maven.model.DependencyManagement; -import org.apache.maven.model.Exclusion; -import org.apache.maven.model.Model; import org.apache.maven.model.building.DefaultModelBuilderFactory; -import org.apache.maven.model.building.DefaultModelBuildingRequest; import org.apache.maven.model.building.ModelBuilder; -import org.apache.maven.model.building.ModelBuildingException; -import org.apache.maven.model.building.ModelBuildingRequest; -import org.apache.maven.model.resolution.UnresolvableModelException; -import org.apache.maven.project.DefaultDependencyResolutionRequest; -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; import org.sonatype.aether.RepositorySystem; import org.sonatype.aether.RepositorySystemSession; -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; @@ -84,12 +62,7 @@ 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; import javax.annotation.PostConstruct; @@ -98,9 +71,7 @@ import javax.inject.Named; import java.io.File; import java.util.ArrayList; import java.util.Collection; -import java.util.Collections; import java.util.HashMap; -import java.util.IdentityHashMap; import java.util.List; import java.util.Map; @@ -152,9 +123,8 @@ public class Maven3DependencyTreeBuilder builder = defaultModelBuilderFactory.newInstance(); } - public DependencyResolutionResult buildDependencyTree( List repositoryIds, String groupId, - String artifactId, String version, - DependencyVisitor dependencyVisitor ) + public void buildDependencyTree( List repositoryIds, String groupId, String artifactId, String version, + DependencyVisitor dependencyVisitor ) throws Exception { Artifact projectArtifact = factory.createProjectArtifact( groupId, artifactId, version ); @@ -172,7 +142,7 @@ public class Maven3DependencyTreeBuilder if ( repository == null ) { // metadata could not be resolved - return new DefaultDependencyResolutionResult(); + return; } // MRM-1411 @@ -199,158 +169,14 @@ public class Maven3DependencyTreeBuilder } } - Model model = buildProject( - new RepositoryModelResolver( repository, pathTranslator, wagonFactory, remoteRepositories, networkProxies, - repository ), groupId, artifactId, version ); - - MavenProject project = new MavenProject( model ); - - DefaultRepositorySystemSession repositorySystemSession = new DefaultRepositorySystemSession(); - - // FIXME take care of relative path for getLocation - repositorySystemSession.setLocalRepositoryManager( - new SimpleLocalRepositoryManager( new File( repository.getLocation() ) ) ); - - DefaultProjectBuildingRequest projectBuildingRequest = new DefaultProjectBuildingRequest(); - - project.setProjectBuildingRequest( projectBuildingRequest ); - - projectBuildingRequest.setRepositorySession( repositorySystemSession ); - - DefaultDependencyResolutionRequest request = - new DefaultDependencyResolutionRequest( project, projectBuildingRequest.getRepositorySession() ); - - //DependencyFilter dependencyFilter - //request.setResolutionFilter( ) - - //DependencyResolutionResult result = projectDependenciesResolver.resolve( request ); - - //DependencyNode dependencyNode = buildDependencyNode( null, result.getDependencyGraph(), projectArtifact, null ); - /*DependencyNode dependencyNode = dependencyGraphBuilder.buildDependencyGraph( project, new ArtifactFilter() - { - public boolean include( Artifact artifact ) - { - return true; - } - } );*/ - - DependencyResolutionResult resolutionResult = resolve( request ); - - log.debug( "dependency graph build" ); - // FIXME take care of relative path - test( repository.getLocation(), groupId, artifactId, version, dependencyVisitor ); + resolve( repository.getLocation(), groupId, artifactId, version, dependencyVisitor ); - return resolutionResult; } - private DependencyResolutionResult resolve( DefaultDependencyResolutionRequest request ) - throws DependencyResolutionException - { - - RequestTrace trace = DefaultRequestTrace.newChild( null, request ); - - DefaultDependencyResolutionResult result = new DefaultDependencyResolutionResult(); - - MavenProject project = request.getMavenProject(); - RepositorySystemSession session = request.getRepositorySession(); - DependencyFilter filter = request.getResolutionFilter(); - - ArtifactTypeRegistry stereotypes = session.getArtifactTypeRegistry(); - - CollectRequest collect = new CollectRequest(); - collect.setRequestContext( "project" ); - collect.setRepositories( project.getRemoteProjectRepositories() ); - - if ( project.getDependencyArtifacts() == null ) - { - for ( org.apache.maven.model.Dependency dependency : project.getDependencies() ) - { - if ( StringUtils.isEmpty( dependency.getGroupId() ) || StringUtils.isEmpty( dependency.getArtifactId() ) - || StringUtils.isEmpty( dependency.getVersion() ) ) - { - // guard against case where best-effort resolution for invalid models is requested - continue; - } - collect.addDependency( RepositoryUtils.toDependency( dependency, stereotypes ) ); - } - } - else - { - Map dependencies = - new HashMap(); - for ( org.apache.maven.model.Dependency dependency : project.getDependencies() ) - { - String classifier = dependency.getClassifier(); - if ( classifier == null ) - { - ArtifactType type = stereotypes.get( dependency.getType() ); - if ( type != null ) - { - classifier = type.getClassifier(); - } - } - String key = ArtifacIdUtils.toVersionlessId( dependency.getGroupId(), dependency.getArtifactId(), - dependency.getType(), classifier ); - dependencies.put( key, dependency ); - } - for ( Artifact artifact : project.getDependencyArtifacts() ) - { - String key = artifact.getDependencyConflictId(); - org.apache.maven.model.Dependency dependency = dependencies.get( key ); - Collection exclusions = dependency != null ? dependency.getExclusions() : null; - org.sonatype.aether.graph.Dependency dep = RepositoryUtils.toDependency( artifact, exclusions ); - if ( !JavaScopes.SYSTEM.equals( dep.getScope() ) && dep.getArtifact().getFile() != null ) - { - // enable re-resolution - org.sonatype.aether.artifact.Artifact art = dep.getArtifact(); - art = art.setFile( null ).setVersion( art.getBaseVersion() ); - dep = dep.setArtifact( art ); - } - collect.addDependency( dep ); - } - } - - DependencyManagement depMngt = project.getDependencyManagement(); - if ( depMngt != null ) - { - for ( org.apache.maven.model.Dependency dependency : depMngt.getDependencies() ) - { - collect.addManagedDependency( RepositoryUtils.toDependency( dependency, stereotypes ) ); - } - } - 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 ); - - org.sonatype.aether.graph.DependencyNode node; - try - { - collect.setTrace( DefaultRequestTrace.newChild( trace, depRequest ) ); - node = repoSystem.collectDependencies( session, collect ).getRoot(); - result.setDependencyGraph( node ); - } - catch ( DependencyCollectionException e ) - { - result.setDependencyGraph( e.getResult().getRoot() ); - result.setCollectionErrors( e.getResult().getExceptions() ); - - throw new DependencyResolutionException( result, - "Could not resolve dependencies for project " + project.getId() - + ": " + e.getMessage(), e ); - } - - depRequest.setRoot( node ); - - return result; - } - - private void test( String localRepoDir, String groupId, String artifactId, String version, - DependencyVisitor dependencyVisitor ) + private void resolve( String localRepoDir, String groupId, String artifactId, String version, + DependencyVisitor dependencyVisitor ) { RepositorySystem system = newRepositorySystem(); @@ -449,56 +275,12 @@ public class Maven3DependencyTreeBuilder LocalRepository localRepo = new LocalRepository( localRepoDir ); session.setLocalRepositoryManager( system.newLocalRepositoryManager( localRepo ) ); - //session.setTransferListener( new ConsoleTransferListener() ); - //session.setRepositoryListener( new ConsoleRepositoryListener() ); + //session.setTransferListener( ); + //session.setRepositoryListener( n ); return session; } - private String getVersionSelectedFromRange( VersionConstraint constraint ) - { - if ( ( constraint == null ) || ( constraint.getVersion() != null ) ) - { - return null; - } - - StringBuilder sb = new StringBuilder(); - for ( org.sonatype.aether.version.VersionRange range : constraint.getRanges() ) - { - if ( sb.length() > 0 ) - { - sb.append( ',' ); - } - sb.append( range ); - } - - return sb.toString(); - } - - private Artifact getDependencyArtifact( Dependency dep ) - { - org.sonatype.aether.artifact.Artifact artifact = dep.getArtifact(); - - return factory.createDependencyArtifact( artifact.getGroupId(), artifact.getArtifactId(), - VersionRange.createFromVersion( artifact.getVersion() ), - artifact.getExtension(), artifact.getClassifier(), dep.getScope(), - dep.isOptional() ); - } - - private Model buildProject( RepositoryModelResolver modelResolver, String groupId, String artifactId, - String version ) - throws ModelBuildingException, UnresolvableModelException - { - DefaultModelBuildingRequest req = new DefaultModelBuildingRequest(); - req.setProcessPlugins( false ); - req.setModelSource( modelResolver.resolveModel( groupId, artifactId, version ) ); - req.setModelResolver( modelResolver ); - req.setValidationLevel( ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL ); - //MRM-1607. olamy this will resolve jdk profiles on the current running archiva jvm - req.setSystemProperties( System.getProperties() ); - - return builder.build( req ).getEffectiveModel(); - } private ManagedRepository findArtifactInRepositories( List repositoryIds, Artifact projectArtifact ) throws RepositoryAdminException @@ -521,82 +303,5 @@ public class Maven3DependencyTreeBuilder return null; } - public static class DefaultDependencyResolutionResult - implements DependencyResolutionResult - { - - private org.sonatype.aether.graph.DependencyNode root; - - private List dependencies = new ArrayList(); - - private List resolvedDependencies = new ArrayList(); - - private List unresolvedDependencies = new ArrayList(); - - private List collectionErrors = new ArrayList(); - - private Map> resolutionErrors = new IdentityHashMap>(); - - public org.sonatype.aether.graph.DependencyNode getDependencyGraph() - { - return root; - } - - public void setDependencyGraph( org.sonatype.aether.graph.DependencyNode root ) - { - this.root = root; - } - - public List getDependencies() - { - return dependencies; - } - - public List getResolvedDependencies() - { - return resolvedDependencies; - } - - public void addResolvedDependency( Dependency dependency ) - { - dependencies.add( dependency ); - resolvedDependencies.add( dependency ); - } - - public List getUnresolvedDependencies() - { - return unresolvedDependencies; - } - - public List getCollectionErrors() - { - return collectionErrors; - } - public void setCollectionErrors( List exceptions ) - { - if ( exceptions != null ) - { - this.collectionErrors = exceptions; - } - else - { - this.collectionErrors = new ArrayList(); - } - } - - public List getResolutionErrors( Dependency dependency ) - { - List errors = resolutionErrors.get( dependency ); - return ( errors != null ) ? errors : Collections.emptyList(); - } - - public void setResolutionErrors( Dependency dependency, List errors ) - { - dependencies.add( dependency ); - unresolvedDependencies.add( dependency ); - resolutionErrors.put( dependency, errors ); - } - - } } 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 fe4089b11..7c70d9ba1 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 @@ -25,7 +25,6 @@ import org.apache.archiva.configuration.ArchivaConfiguration; import org.apache.archiva.configuration.Configuration; import org.apache.archiva.configuration.ManagedRepositoryConfiguration; import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner; -import org.apache.maven.project.DependencyResolutionResult; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -74,6 +73,8 @@ public class DependencyTreeBuilderTestMaven3 private DefaultRepositorySystem defaultRepositorySystem; + final Map nodes = new HashMap(); + @Inject @Named( value = "archivaConfiguration#test" ) ArchivaConfiguration config; @@ -86,8 +87,6 @@ public class DependencyTreeBuilderTestMaven3 defaultRepositorySystem = (DefaultRepositorySystem) plexusSisuBridge.lookup( RepositorySystem.class ); - final Map nodes = new HashMap(); - DefaultDependencyNode springContext = new DefaultDependencyNode( new Dependency( createArtifact( "org.springframework", "spring-context", "2.5.6" ), "compile" ) ); @@ -300,23 +299,20 @@ public class DependencyTreeBuilderTestMaven3 throws Exception { - DependencyResolutionResult resolutionResult = - builder.buildDependencyTree( Collections.singletonList( TEST_REPO_ID ), TEST_GROUP_ID, TEST_ARTIFACT_ID, - TEST_VERSION, new DependencyVisitor() + builder.buildDependencyTree( Collections.singletonList( TEST_REPO_ID ), TEST_GROUP_ID, TEST_ARTIFACT_ID, + TEST_VERSION, new DependencyVisitor() + { + public boolean visitEnter( DependencyNode dependencyNode ) { - public boolean visitEnter( DependencyNode dependencyNode ) - { - return true; - } + return true; + } - public boolean visitLeave( DependencyNode dependencyNode ) - { - return true; - } - } ); + public boolean visitLeave( DependencyNode dependencyNode ) + { + return true; + } + } ); - assertNotNull( resolutionResult ); - assertEquals( 10, resolutionResult.getDependencies().size() ); } } -- 2.39.5