From c31afc5c1c56430d97517d8f99881d10914a7eb8 Mon Sep 17 00:00:00 2001 From: Olivier Lamy Date: Thu, 2 Aug 2012 13:19:23 +0000 Subject: [PATCH] add a tree builder with maven3/aether api git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1368456 13f79535-47bb-0310-9956-ffa450edef68 --- .../plugins/maven2-repository/pom.xml | 19 +- .../maven2/Maven3DependencyTreeBuilder.java | 468 ++++++++++++++++++ .../DependencyTreeBuilderTestMaven3.java | 310 ++++++++++++ 3 files changed, 796 insertions(+), 1 deletion(-) create mode 100644 archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/dependency/tree/maven2/Maven3DependencyTreeBuilder.java create mode 100644 archiva-modules/plugins/maven2-repository/src/test/java/org/apache/archiva/dependency/tree/maven2/DependencyTreeBuilderTestMaven3.java diff --git a/archiva-modules/plugins/maven2-repository/pom.xml b/archiva-modules/plugins/maven2-repository/pom.xml index 4b0de9b6f..e82e84e30 100644 --- a/archiva-modules/plugins/maven2-repository/pom.xml +++ b/archiva-modules/plugins/maven2-repository/pom.xml @@ -73,6 +73,11 @@ org.apache.maven maven-artifact + + org.apache.maven + maven-core + ${maven3x.version} + org.apache.maven maven-artifact-manager @@ -205,7 +210,19 @@ org.codehaus.redback.components*, org.dom4j*;version="[1.6,2)", org.springframework*;version="[3,4)", - org.slf4j;resolution:=optional + org.slf4j;resolution:=optional, + org.apache.maven, + org.apache.maven.project, + org.sonatype.aether, + org.sonatype.aether.artifact, + org.sonatype.aether.collection, + org.sonatype.aether.graph, + org.sonatype.aether.impl.internal, + org.sonatype.aether.repository, + org.sonatype.aether.resolution, + org.sonatype.aether.util, + org.sonatype.aether.util.artifact, + org.sonatype.aether.version 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 new file mode 100644 index 000000000..30822d244 --- /dev/null +++ b/archiva-modules/plugins/maven2-repository/src/main/java/org/apache/archiva/dependency/tree/maven2/Maven3DependencyTreeBuilder.java @@ -0,0 +1,468 @@ +package org.apache.archiva.dependency.tree.maven2; +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + + +import org.apache.archiva.admin.model.RepositoryAdminException; +import org.apache.archiva.admin.model.beans.ManagedRepository; +import org.apache.archiva.admin.model.beans.NetworkProxy; +import org.apache.archiva.admin.model.beans.ProxyConnector; +import org.apache.archiva.admin.model.beans.RemoteRepository; +import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin; +import org.apache.archiva.admin.model.networkproxy.NetworkProxyAdmin; +import org.apache.archiva.admin.model.proxyconnector.ProxyConnectorAdmin; +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.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.DependencyCollectionException; +import org.sonatype.aether.graph.Dependency; +import org.sonatype.aether.graph.DependencyFilter; +import org.sonatype.aether.impl.internal.SimpleLocalRepositoryManager; +import org.sonatype.aether.resolution.DependencyRequest; +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.JavaScopes; +import org.sonatype.aether.version.VersionConstraint; +import org.springframework.stereotype.Service; + +import javax.annotation.PostConstruct; +import javax.inject.Inject; +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; + +/** + * @author Olivier Lamy + */ +@Service( "dependencyTreeBuilder#maven3" ) +public class Maven3DependencyTreeBuilder +{ + private Logger log = LoggerFactory.getLogger( getClass() ); + + @Inject + private PlexusSisuBridge plexusSisuBridge; + + @Inject + @Named( value = "repositoryPathTranslator#maven2" ) + private RepositoryPathTranslator pathTranslator; + + @Inject + private WagonFactory wagonFactory; + + @Inject + private ManagedRepositoryAdmin managedRepositoryAdmin; + + @Inject + private ProxyConnectorAdmin proxyConnectorAdmin; + + @Inject + private NetworkProxyAdmin networkProxyAdmin; + + @Inject + private RemoteRepositoryAdmin remoteRepositoryAdmin; + + private ArtifactFactory factory; + + private ModelBuilder builder; + + + private RepositorySystem repoSystem; + + @PostConstruct + public void initialize() + throws PlexusSisuBridgeException + { + factory = plexusSisuBridge.lookup( ArtifactFactory.class, "default" ); + + repoSystem = plexusSisuBridge.lookup( RepositorySystem.class ); + DefaultModelBuilderFactory defaultModelBuilderFactory = new DefaultModelBuilderFactory(); + builder = defaultModelBuilderFactory.newInstance(); + } + + public DependencyResolutionResult buildDependencyTree( List repositoryIds, String groupId, + String artifactId, String version ) + throws Exception + { + Artifact projectArtifact = factory.createProjectArtifact( groupId, artifactId, version ); + ManagedRepository repository = null; + try + { + repository = findArtifactInRepositories( repositoryIds, projectArtifact ); + } + catch ( RepositoryAdminException e ) + { + // FIXME better exception + throw new Exception( "Cannot build project dependency tree " + e.getMessage(), e ); + } + + if ( repository == null ) + { + // metadata could not be resolved + return new DefaultDependencyResolutionResult(); + } + + // MRM-1411 + // TODO: this is a workaround for a lack of proxy capability in the resolvers - replace when it can all be + // handled there. It doesn't cache anything locally! + List remoteRepositories = new ArrayList(); + Map networkProxies = new HashMap(); + + Map> proxyConnectorsMap = proxyConnectorAdmin.getProxyConnectorAsMap(); + List proxyConnectors = proxyConnectorsMap.get( repository.getId() ); + if ( proxyConnectors != null ) + { + for ( ProxyConnector proxyConnector : proxyConnectors ) + { + remoteRepositories.add( remoteRepositoryAdmin.getRemoteRepository( proxyConnector.getTargetRepoId() ) ); + + NetworkProxy networkProxyConfig = networkProxyAdmin.getNetworkProxy( proxyConnector.getProxyId() ); + + if ( networkProxyConfig != null ) + { + // key/value: remote repo ID/proxy info + networkProxies.put( proxyConnector.getTargetRepoId(), networkProxyConfig ); + } + } + } + + 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" ); + + 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 ) ); + } + } + + 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 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 + { + for ( String repoId : repositoryIds ) + { + ManagedRepository managedRepository = managedRepositoryAdmin.getManagedRepository( repoId ); + + File repoDir = new File( managedRepository.getLocation() ); + File file = pathTranslator.toFile( repoDir, projectArtifact.getGroupId(), projectArtifact.getArtifactId(), + projectArtifact.getBaseVersion(), + projectArtifact.getArtifactId() + "-" + projectArtifact.getVersion() + + ".pom" ); + + if ( file.exists() ) + { + return managedRepository; + } + } + 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 new file mode 100644 index 000000000..168854b6f --- /dev/null +++ b/archiva-modules/plugins/maven2-repository/src/test/java/org/apache/archiva/dependency/tree/maven2/DependencyTreeBuilderTestMaven3.java @@ -0,0 +1,310 @@ +package org.apache.archiva.dependency.tree.maven2; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import junit.framework.TestCase; +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.test.utils.ArchivaSpringJUnit4ClassRunner; +import org.apache.maven.project.DependencyResolutionResult; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.sonatype.aether.RepositorySystem; +import org.sonatype.aether.RepositorySystemSession; +import org.sonatype.aether.artifact.Artifact; +import org.sonatype.aether.collection.CollectRequest; +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.impl.DependencyCollector; +import org.sonatype.aether.impl.internal.DefaultRepositorySystem; +import org.sonatype.aether.util.artifact.DefaultArtifact; +import org.sonatype.aether.util.graph.DefaultDependencyNode; +import org.springframework.test.context.ContextConfiguration; + +import javax.inject.Inject; +import javax.inject.Named; +import java.io.File; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +@RunWith( ArchivaSpringJUnit4ClassRunner.class ) +@ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } ) +public class DependencyTreeBuilderTestMaven3 + extends TestCase +{ + @Inject + @Named( value = "dependencyTreeBuilder#maven3" ) + private Maven3DependencyTreeBuilder builder; + + @Inject + private PlexusSisuBridge plexusSisuBridge; + + private static final String TEST_REPO_ID = "test"; + + private static final String TEST_VERSION = "1.2.1"; + + private static final String TEST_ARTIFACT_ID = "archiva-common"; + + private static final String TEST_GROUP_ID = "org.apache.archiva"; + + private DefaultRepositorySystem defaultRepositorySystem; + + + @Inject + @Named( value = "archivaConfiguration#test" ) + ArchivaConfiguration config; + + @Before + public void setUp() + throws Exception + { + super.setUp(); + + 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" ) ); + + springContext.setPremanagedVersion( "2.5.5" ); + + nodes.put( getId( springContext.getDependency().getArtifact() ), springContext ); + + DefaultDependencyNode springTest = new DefaultDependencyNode( + new Dependency( createArtifact( "org.springframework", "spring-test", "2.5.5" ), "test" ) ); + + nodes.put( getId( springTest.getDependency().getArtifact() ), springTest ); + + DefaultDependencyNode plexusUtils = new DefaultDependencyNode( + new Dependency( createArtifact( "org.codehaus.plexus", "plexus-utils", "1.4.5" ), "compile" ) ); + + plexusUtils.setPremanagedVersion( "1.5.1" ); + + nodes.put( getId( plexusUtils.getDependency().getArtifact() ), plexusUtils ); + + DefaultDependencyNode slf4jLog4j12 = new DefaultDependencyNode( + new Dependency( createArtifact( "org.slf4j", "slf4j-log4j12", "1.5.0" ), "runtime" ) ); + + slf4jLog4j12.setPremanagedScope( "test" ); + + nodes.put( getId( slf4jLog4j12.getDependency().getArtifact() ), slf4jLog4j12 ); + + DefaultDependencyNode plexusLog4j = new DefaultDependencyNode( + new Dependency( createArtifact( "org.codehaus.plexus", "plexus-log4j-logging", "1.1-alpha-3" ), "test" ) ); + + nodes.put( getId( plexusLog4j.getDependency().getArtifact() ), plexusLog4j ); + + DefaultDependencyNode log4j = + new DefaultDependencyNode( new Dependency( createArtifact( "log4j", "log4j", "1.2.14" ), "test" ) ); + + nodes.put( getId( log4j.getDependency().getArtifact() ), log4j ); + + DefaultDependencyNode mavenArtifact = new DefaultDependencyNode( + new Dependency( createArtifact( "org.apache.maven", "maven-artifact", "2.0.8" ), "test" ) ); + + nodes.put( getId( mavenArtifact.getDependency().getArtifact() ), mavenArtifact ); + + DefaultDependencyNode mavenProject = new DefaultDependencyNode( + new Dependency( createArtifact( "org.apache.maven", "maven-project", "2.0.8" ), "test" ) ); + + nodes.put( getId( mavenProject.getDependency().getArtifact() ), mavenProject ); + + DefaultDependencyNode mavenCore = new DefaultDependencyNode( + new Dependency( createArtifact( "org.apache.maven", "maven-core", "2.0.8" ), "test" ) ); + + nodes.put( getId( mavenCore.getDependency().getArtifact() ), mavenCore ); + + DefaultDependencyNode mavenSettings = new DefaultDependencyNode( + new Dependency( createArtifact( "org.apache.maven", "maven-settings", "2.0.8" ), "test" ) ); + + nodes.put( getId( mavenSettings.getDependency().getArtifact() ), mavenSettings ); + + DefaultDependencyNode mavenModel = new DefaultDependencyNode( + new Dependency( createArtifact( "org.apache.maven", "maven-model", "2.0.8" ), "test" ) ); + + nodes.put( getId( mavenModel.getDependency().getArtifact() ), mavenModel ); + + DefaultDependencyNode plexusCommandLine = new DefaultDependencyNode( + new Dependency( createArtifact( "org.codehaus.plexus", "plexus-command-line", "1.0-alpha-2" ), "test" ) ); + + nodes.put( getId( plexusCommandLine.getDependency().getArtifact() ), plexusCommandLine ); + + DefaultDependencyNode plexusRegistryCommons = new DefaultDependencyNode( + new Dependency( createArtifact( "org.codehaus.plexus.registry", "plexus-registry-commons", "1.0-alpha-2" ), + "test" ) ); + + nodes.put( getId( plexusRegistryCommons.getDependency().getArtifact() ), plexusRegistryCommons ); + + plexusRegistryCommons.setPremanagedVersion( "1.0-alpha-3" ); + + DefaultDependencyNode plexusRegistryApi = new DefaultDependencyNode( + new Dependency( createArtifact( "org.codehaus.plexus.registry", "plexus-registry-api", "1.0-alpha-2" ), + "test" ) ); + + nodes.put( getId( plexusRegistryApi.getDependency().getArtifact() ), plexusRegistryApi ); + + plexusRegistryApi.setPremanagedVersion( "1.0-alpha-3" ); + + DefaultDependencyNode plexusSpring = new DefaultDependencyNode( + new Dependency( createArtifact( "org.codehaus.plexus", "plexus-spring", "1.2" ), "test" ) ); + + nodes.put( getId( plexusSpring.getDependency().getArtifact() ), plexusSpring ); + + plexusSpring.getChildren().add( springContext ); + plexusSpring.getChildren().add( springTest ); + plexusSpring.getChildren().add( plexusUtils ); + plexusSpring.getChildren().add( slf4jLog4j12 ); + plexusSpring.getChildren().add( plexusLog4j ); + plexusSpring.getChildren().add( log4j ); + plexusSpring.getChildren().add( mavenArtifact ); + plexusSpring.getChildren().add( mavenProject ); + plexusSpring.getChildren().add( mavenCore ); + plexusSpring.getChildren().add( mavenSettings ); + plexusSpring.getChildren().add( mavenModel ); + plexusSpring.getChildren().add( plexusCommandLine ); + plexusSpring.getChildren().add( plexusRegistryCommons ); + plexusSpring.getChildren().add( plexusRegistryApi ); + + DefaultDependencyNode commonsLang = new DefaultDependencyNode( + new Dependency( createArtifact( "commons-lang", "commons-lang", "2.2" ), "compile" ) ); + + nodes.put( getId( commonsLang.getDependency().getArtifact() ), commonsLang ); + + DefaultDependencyNode commonsIO = new DefaultDependencyNode( + new Dependency( createArtifact( "commons-io", "commons-io", "1.4" ), "compile" ) ); + + nodes.put( getId( commonsIO.getDependency().getArtifact() ), commonsIO ); + + DefaultDependencyNode slf4j = new DefaultDependencyNode( + new Dependency( createArtifact( "org.slf4j", "slf4j-api", "1.5.0" ), "compile" ) ); + + nodes.put( getId( slf4j.getDependency().getArtifact() ), slf4j ); + + DefaultDependencyNode plexusAPI = new DefaultDependencyNode( + new Dependency( createArtifact( "org.codehaus.plexus", "plexus-component-api", "1.0-alpha-22" ), + "compile" ) ); + + nodes.put( getId( plexusAPI.getDependency().getArtifact() ), plexusAPI ); + + DefaultDependencyNode xalan = + new DefaultDependencyNode( new Dependency( createArtifact( "xalan", "xalan", "2.7.0" ), "compile" ) ); + + nodes.put( getId( xalan.getDependency().getArtifact() ), xalan ); + + DefaultDependencyNode dom4j = + new DefaultDependencyNode( new Dependency( createArtifact( "dom4j", "dom4j", "1.6.1" ), "test" ) ); + + nodes.put( getId( dom4j.getDependency().getArtifact() ), dom4j ); + + //dom4j.setFailedUpdateScope("compile"); + + DefaultDependencyNode junit = + new DefaultDependencyNode( new Dependency( createArtifact( "junit", "junit", "3.8.1" ), "test" ) ); + + nodes.put( getId( junit.getDependency().getArtifact() ), junit ); + + DefaultDependencyNode easymock = new DefaultDependencyNode( + new Dependency( createArtifact( "easymock", "easymock", "1.2_Java1.3" ), "test" ) ); + + nodes.put( getId( easymock.getDependency().getArtifact() ), easymock ); + + DefaultDependencyNode easymockExt = new DefaultDependencyNode( + new Dependency( createArtifact( "easymock", "easymockclassextension", "1.2" ), "test" ) ); + + nodes.put( getId( easymockExt.getDependency().getArtifact() ), easymockExt ); + + DependencyNode mainNode = new DefaultDependencyNode( + new Dependency( createArtifact( TEST_GROUP_ID, TEST_ARTIFACT_ID, TEST_VERSION ), "compile" ) ); + + nodes.put( getId( mainNode.getDependency().getArtifact() ), mainNode ); + + mainNode.getChildren().add( commonsLang ); + mainNode.getChildren().add( commonsIO ); + mainNode.getChildren().add( slf4j ); + mainNode.getChildren().add( plexusAPI ); + mainNode.getChildren().add( plexusSpring ); + mainNode.getChildren().add( xalan ); + mainNode.getChildren().add( dom4j ); + mainNode.getChildren().add( junit ); + mainNode.getChildren().add( easymock ); + mainNode.getChildren().add( easymockExt ); + + defaultRepositorySystem.setDependencyCollector( new DependencyCollector() + { + + public CollectResult collectDependencies( RepositorySystemSession session, CollectRequest request ) + throws DependencyCollectionException + { + CollectResult collectResult = new CollectResult( request ); + collectResult.setRoot( new DefaultDependencyNode() ); + for ( Dependency dependency : request.getDependencies() ) + { + DependencyNode node = nodes.get( getId( dependency.getArtifact() ) ); + if ( node != null ) + { + collectResult.getRoot().getChildren().add( node ); + } + } + return collectResult; + } + } ); + + Configuration configuration = new Configuration(); + ManagedRepositoryConfiguration repoConfig = new ManagedRepositoryConfiguration(); + repoConfig.setId( TEST_REPO_ID ); + repoConfig.setLocation( new File( "target/test-repository" ).getAbsolutePath() ); + configuration.addManagedRepository( repoConfig ); + config.save( configuration ); + + //artifactFactory = ((DefaultDependencyTreeBuilder)this.builder).getFactory(); + } + + + private Artifact createArtifact( String groupId, String artifactId, String version ) + { + return new DefaultArtifact( groupId, artifactId, null, version ); + } + + private String getId( Artifact artifact ) + { + return artifact.getGroupId() + ":" + artifact.getArtifactId() + ":" + artifact.getVersion(); + } + + @Test + public void testBuilderDependencies() + throws Exception + { + + DependencyResolutionResult resolutionResult = + builder.buildDependencyTree( Collections.singletonList( TEST_REPO_ID ), TEST_GROUP_ID, TEST_ARTIFACT_ID, + TEST_VERSION ); + + assertNotNull( resolutionResult ); + assertEquals( 10, resolutionResult.getDependencies().size() ); + + } +} -- 2.39.5