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.util.List; 022 023import org.apache.maven.resolver.examples.util.Booter; 024import org.eclipse.aether.RepositorySystem; 025import org.eclipse.aether.RepositorySystemSession.CloseableSession; 026import org.eclipse.aether.artifact.Artifact; 027import org.eclipse.aether.artifact.DefaultArtifact; 028import org.eclipse.aether.collection.CollectRequest; 029import org.eclipse.aether.graph.Dependency; 030import org.eclipse.aether.graph.DependencyFilter; 031import org.eclipse.aether.resolution.ArtifactResult; 032import org.eclipse.aether.resolution.DependencyRequest; 033import org.eclipse.aether.util.artifact.JavaScopes; 034import org.eclipse.aether.util.filter.DependencyFilterUtils; 035 036/** 037 * Resolves the transitive (compile) dependencies of an artifact. 038 */ 039public class ResolveTransitiveDependencies { 040 041 /** 042 * Main. 043 * @param args 044 * @throws Exception 045 */ 046 public static void main(String[] args) throws Exception { 047 System.out.println("------------------------------------------------------------"); 048 System.out.println(ResolveTransitiveDependencies.class.getSimpleName()); 049 050 try (RepositorySystem system = Booter.newRepositorySystem(Booter.selectFactory(args)); 051 CloseableSession session = Booter.newRepositorySystemSession(system, Booter.selectFs(args)) 052 .build()) { 053 // An "interesting" artifact that depends on itself (different version). All Maven versions 054 // released so far contained dom4j:dom4j:1.5.2 on classpath for dom4j:dom4j:1.6.1 055 // In Resolver 2.0.19+ this is fixed. 056 Artifact artifact = new DefaultArtifact("dom4j:dom4j:1.6.1"); 057 058 DependencyFilter classpathFilter = DependencyFilterUtils.classpathFilter(JavaScopes.COMPILE); 059 060 CollectRequest collectRequest = new CollectRequest(); 061 collectRequest.setRoot(new Dependency(artifact, JavaScopes.COMPILE)); 062 collectRequest.setRepositories(Booter.newRepositories(system, session)); 063 064 DependencyRequest dependencyRequest = new DependencyRequest(collectRequest, classpathFilter); 065 066 List<ArtifactResult> artifactResults = 067 system.resolveDependencies(session, dependencyRequest).getArtifactResults(); 068 069 for (ArtifactResult artifactResult : artifactResults) { 070 System.out.println(artifactResult.getArtifact() + " resolved to " 071 + artifactResult.getArtifact().getPath()); 072 } 073 } 074 } 075}