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.io.File;
22  import java.util.Collections;
23  
24  import org.apache.maven.resolver.examples.util.Booter;
25  import org.eclipse.aether.RepositorySystem;
26  import org.eclipse.aether.RepositorySystemSession.CloseableSession;
27  import org.eclipse.aether.RepositorySystemSession.SessionBuilder;
28  import org.eclipse.aether.artifact.Artifact;
29  import org.eclipse.aether.artifact.DefaultArtifact;
30  import org.eclipse.aether.collection.CollectRequest;
31  import org.eclipse.aether.collection.CollectResult;
32  import org.eclipse.aether.repository.RemoteRepository;
33  import org.eclipse.aether.repository.RepositoryPolicy;
34  import org.eclipse.aether.resolution.ArtifactDescriptorRequest;
35  import org.eclipse.aether.resolution.ArtifactDescriptorResult;
36  import org.eclipse.aether.util.graph.manager.DependencyManagerUtils;
37  import org.eclipse.aether.util.graph.transformer.ConflictResolver;
38  
39  /**
40   * Visualizes the transitive dependencies of an artifact similar to m2e's dependency hierarchy view. Artifact in this
41   * test is not "plain" one as is original "demo" {@link GetDependencyHierarchy}, but specially crafted for case
42   * described in MRESOLVER-345.
43   *
44   * @see <a href="https://issues.apache.org/jira/browse/MRESOLVER-345">MRESOLVER-345</a>
45   */
46  public class DependencyHierarchyWithRanges {
47  
48      /**
49       * Main.
50       */
51      public static void main(String[] args) throws Exception {
52          System.out.println("------------------------------------------------------------");
53          System.out.println(DependencyHierarchyWithRanges.class.getSimpleName());
54  
55          try (RepositorySystem system = Booter.newRepositorySystem(Booter.selectFactory(args))) {
56              SessionBuilder sessionBuilder = Booter.newRepositorySystemSession(system);
57              sessionBuilder.setChecksumPolicy(RepositoryPolicy.CHECKSUM_POLICY_IGNORE); // to not bother with checksums
58              sessionBuilder.setConfigProperty(ConflictResolver.CONFIG_PROP_VERBOSE, true);
59              sessionBuilder.setConfigProperty(DependencyManagerUtils.CONFIG_PROP_VERBOSE, true);
60              try (CloseableSession session = sessionBuilder.build()) {
61                  // this artifact is in "remote" repository in src/main/remote-repository
62                  Artifact artifact = new DefaultArtifact("org.apache.maven.resolver.demo.mresolver345:a:1.0");
63  
64                  File remoteRepoBasedir = new File("src/main/remote-repository");
65  
66                  ArtifactDescriptorRequest descriptorRequest = new ArtifactDescriptorRequest();
67                  descriptorRequest.setArtifact(artifact);
68                  descriptorRequest.setRepositories(Collections.singletonList(new RemoteRepository.Builder(
69                                  "remote", "default", remoteRepoBasedir.toURI().toASCIIString())
70                          .build()));
71                  ArtifactDescriptorResult descriptorResult = system.readArtifactDescriptor(session, descriptorRequest);
72  
73                  CollectRequest collectRequest = new CollectRequest();
74                  collectRequest.setRootArtifact(descriptorResult.getArtifact());
75                  collectRequest.setDependencies(descriptorResult.getDependencies());
76                  collectRequest.setManagedDependencies(descriptorResult.getManagedDependencies());
77                  collectRequest.setRepositories(descriptorRequest.getRepositories());
78  
79                  CollectResult collectResult = system.collectDependencies(session, collectRequest);
80  
81                  collectResult.getRoot().accept(Booter.DUMPER_SOUT);
82              }
83          }
84      }
85  }