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.RepositorySystem;
026import org.eclipse.aether.RepositorySystemSession.CloseableSession;
027import org.eclipse.aether.RepositorySystemSession.SessionBuilder;
028import org.eclipse.aether.artifact.Artifact;
029import org.eclipse.aether.artifact.DefaultArtifact;
030import org.eclipse.aether.collection.CollectRequest;
031import org.eclipse.aether.collection.CollectResult;
032import org.eclipse.aether.repository.RemoteRepository;
033import org.eclipse.aether.repository.RepositoryPolicy;
034import org.eclipse.aether.resolution.ArtifactDescriptorRequest;
035import org.eclipse.aether.resolution.ArtifactDescriptorResult;
036import org.eclipse.aether.util.graph.manager.DependencyManagerUtils;
037import org.eclipse.aether.util.graph.transformer.ConflictResolver;
038
039/**
040 * Visualizes the transitive dependencies of an artifact similar to m2e's dependency hierarchy view. Artifact in this
041 * test is not "plain" one as is original "demo" {@link GetDependencyHierarchy}, but specially crafted for case
042 * described in MRESOLVER-345.
043 *
044 * @see <a href="https://issues.apache.org/jira/browse/MRESOLVER-345">MRESOLVER-345</a>
045 */
046public class DependencyHierarchyWithRanges {
047
048    /**
049     * Main.
050     */
051    public static void main(String[] args) throws Exception {
052        System.out.println("------------------------------------------------------------");
053        System.out.println(DependencyHierarchyWithRanges.class.getSimpleName());
054
055        try (RepositorySystem system = Booter.newRepositorySystem(Booter.selectFactory(args))) {
056            SessionBuilder sessionBuilder = Booter.newRepositorySystemSession(system);
057            sessionBuilder.setChecksumPolicy(RepositoryPolicy.CHECKSUM_POLICY_IGNORE); // to not bother with checksums
058            sessionBuilder.setConfigProperty(ConflictResolver.CONFIG_PROP_VERBOSE, true);
059            sessionBuilder.setConfigProperty(DependencyManagerUtils.CONFIG_PROP_VERBOSE, true);
060            try (CloseableSession session = sessionBuilder.build()) {
061                // this artifact is in "remote" repository in src/main/remote-repository
062                Artifact artifact = new DefaultArtifact("org.apache.maven.resolver.demo.mresolver345:a:1.0");
063
064                File remoteRepoBasedir = new File("src/main/remote-repository");
065
066                ArtifactDescriptorRequest descriptorRequest = new ArtifactDescriptorRequest();
067                descriptorRequest.setArtifact(artifact);
068                descriptorRequest.setRepositories(Collections.singletonList(new RemoteRepository.Builder(
069                                "remote", "default", remoteRepoBasedir.toURI().toASCIIString())
070                        .build()));
071                ArtifactDescriptorResult descriptorResult = system.readArtifactDescriptor(session, descriptorRequest);
072
073                CollectRequest collectRequest = new CollectRequest();
074                collectRequest.setRootArtifact(descriptorResult.getArtifact());
075                collectRequest.setDependencies(descriptorResult.getDependencies());
076                collectRequest.setManagedDependencies(descriptorResult.getManagedDependencies());
077                collectRequest.setRepositories(descriptorRequest.getRepositories());
078
079                CollectResult collectResult = system.collectDependencies(session, collectRequest);
080
081                collectResult.getRoot().accept(Booter.DUMPER_SOUT);
082            }
083        }
084    }
085}