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      /**
44       * Main.
45       * @param args
46       * @throws Exception
47       */
48      public static void main( String[] args )
49          throws Exception
50      {
51          System.out.println( "------------------------------------------------------------" );
52          System.out.println( ResolveTransitiveDependencies.class.getSimpleName() );
53  
54          RepositorySystem system = Booter.newRepositorySystem( Booter.selectFactory( args ) );
55  
56          RepositorySystemSession session = Booter.newRepositorySystemSession( system );
57  
58          Artifact artifact = new DefaultArtifact( "org.apache.maven.resolver:maven-resolver-impl:1.3.3" );
59  
60          DependencyFilter classpathFlter = DependencyFilterUtils.classpathFilter( JavaScopes.COMPILE );
61  
62          CollectRequest collectRequest = new CollectRequest();
63          collectRequest.setRoot( new Dependency( artifact, JavaScopes.COMPILE ) );
64          collectRequest.setRepositories( Booter.newRepositories( system, session ) );
65  
66          DependencyRequest dependencyRequest = new DependencyRequest( collectRequest, classpathFlter );
67  
68          List<ArtifactResult> artifactResults =
69              system.resolveDependencies( session, dependencyRequest ).getArtifactResults();
70  
71          for ( ArtifactResult artifactResult : artifactResults )
72          {
73              System.out.println( artifactResult.getArtifact() + " resolved to "
74                  + artifactResult.getArtifact().getFile() );
75          }
76      }
77  
78  }