001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.maven.resolver.examples;
020
021import java.io.File;
022import java.util.Collections;
023
024import org.apache.maven.resolver.examples.util.Booter;
025import org.eclipse.aether.DefaultRepositorySystemSession;
026import org.eclipse.aether.RepositorySystem;
027import org.eclipse.aether.artifact.Artifact;
028import org.eclipse.aether.artifact.DefaultArtifact;
029import org.eclipse.aether.collection.CollectRequest;
030import org.eclipse.aether.collection.CollectResult;
031import org.eclipse.aether.repository.RemoteRepository;
032import org.eclipse.aether.repository.RepositoryPolicy;
033import org.eclipse.aether.resolution.ArtifactDescriptorRequest;
034import org.eclipse.aether.resolution.ArtifactDescriptorResult;
035import org.eclipse.aether.util.graph.manager.DependencyManagerUtils;
036import org.eclipse.aether.util.graph.transformer.ConflictResolver;
037
038/**
039 * Visualizes the transitive dependencies of an artifact similar to m2e's dependency hierarchy view. Artifact in this
040 * test is not "plain" one as is original "demo" {@link GetDependencyHierarchy}, but specially crafted for case
041 * described in MRESOLVER-345.
042 *
043 * @see <a href="https://issues.apache.org/jira/browse/MRESOLVER-345">MRESOLVER-345</a>
044 */
045public class DependencyHierarchyWithRanges {
046
047    /**
048     * Main.
049     */
050    public static void main(String[] args) throws Exception {
051        System.out.println("------------------------------------------------------------");
052        System.out.println(DependencyHierarchyWithRanges.class.getSimpleName());
053
054        RepositorySystem system = Booter.newRepositorySystem(Booter.selectFactory(args));
055
056        DefaultRepositorySystemSession session = Booter.newRepositorySystemSession(system);
057
058        session.setChecksumPolicy(RepositoryPolicy.CHECKSUM_POLICY_IGNORE); // to not bother with checksums
059        session.setConfigProperty(ConflictResolver.CONFIG_PROP_VERBOSE, true);
060        session.setConfigProperty(DependencyManagerUtils.CONFIG_PROP_VERBOSE, true);
061
062        // this artifact is in "remote" repository in src/main/remote-repository
063        Artifact artifact = new DefaultArtifact("org.apache.maven.resolver.demo.mresolver345:a:1.0");
064
065        File remoteRepoBasedir = new File("src/main/remote-repository");
066
067        ArtifactDescriptorRequest descriptorRequest = new ArtifactDescriptorRequest();
068        descriptorRequest.setArtifact(artifact);
069        descriptorRequest.setRepositories(Collections.singletonList(new RemoteRepository.Builder(
070                        "remote", "default", remoteRepoBasedir.toURI().toASCIIString())
071                .build()));
072        ArtifactDescriptorResult descriptorResult = system.readArtifactDescriptor(session, descriptorRequest);
073
074        CollectRequest collectRequest = new CollectRequest();
075        collectRequest.setRootArtifact(descriptorResult.getArtifact());
076        collectRequest.setDependencies(descriptorResult.getDependencies());
077        collectRequest.setManagedDependencies(descriptorResult.getManagedDependencies());
078        collectRequest.setRepositories(descriptorRequest.getRepositories());
079
080        CollectResult collectResult = system.collectDependencies(session, collectRequest);
081
082        collectResult.getRoot().accept(Booter.DUMPER_SOUT);
083    }
084}