View Javadoc
1   package org.apache.maven.resolver.examples;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   * 
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.List;
23  
24  import org.apache.maven.resolver.examples.util.Booter;
25  import org.eclipse.aether.RepositorySystem;
26  import org.eclipse.aether.RepositorySystemSession;
27  import org.eclipse.aether.artifact.Artifact;
28  import org.eclipse.aether.artifact.DefaultArtifact;
29  import org.eclipse.aether.collection.CollectRequest;
30  import org.eclipse.aether.graph.Dependency;
31  import org.eclipse.aether.graph.DependencyFilter;
32  import org.eclipse.aether.resolution.ArtifactResult;
33  import org.eclipse.aether.resolution.DependencyRequest;
34  import org.eclipse.aether.util.artifact.JavaScopes;
35  import org.eclipse.aether.util.filter.DependencyFilterUtils;
36  
37  /**
38   * Resolves the transitive (compile) dependencies of an artifact.
39   */
40  public class ResolveTransitiveDependencies
41  {
42  
43      public static void main( String[] args )
44          throws Exception
45      {
46          System.out.println( "------------------------------------------------------------" );
47          System.out.println( ResolveTransitiveDependencies.class.getSimpleName() );
48  
49          RepositorySystem system = Booter.newRepositorySystem();
50  
51          RepositorySystemSession session = Booter.newRepositorySystemSession( system );
52  
53          Artifact artifact = new DefaultArtifact( "org.eclipse.aether:aether-impl:1.0.0.v20140518" );
54  
55          DependencyFilter classpathFlter = DependencyFilterUtils.classpathFilter( JavaScopes.COMPILE );
56  
57          CollectRequest collectRequest = new CollectRequest();
58          collectRequest.setRoot( new Dependency( artifact, JavaScopes.COMPILE ) );
59          collectRequest.setRepositories( Booter.newRepositories( system, session ) );
60  
61          DependencyRequest dependencyRequest = new DependencyRequest( collectRequest, classpathFlter );
62  
63          List<ArtifactResult> artifactResults =
64              system.resolveDependencies( session, dependencyRequest ).getArtifactResults();
65  
66          for ( ArtifactResult artifactResult : artifactResults )
67          {
68              System.out.println( artifactResult.getArtifact() + " resolved to "
69                  + artifactResult.getArtifact().getFile() );
70          }
71      }
72  
73  }