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