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 org.apache.maven.resolver.examples.util.Booter; 022import org.eclipse.aether.RepositorySystem; 023import org.eclipse.aether.RepositorySystemSession.CloseableSession; 024import org.eclipse.aether.RepositorySystemSession.SessionBuilder; 025import org.eclipse.aether.artifact.Artifact; 026import org.eclipse.aether.artifact.DefaultArtifact; 027import org.eclipse.aether.collection.CollectRequest; 028import org.eclipse.aether.collection.UnsolvableVersionConflictException; 029import org.eclipse.aether.resolution.ArtifactDescriptorRequest; 030import org.eclipse.aether.resolution.ArtifactDescriptorResult; 031import org.eclipse.aether.util.graph.manager.DependencyManagerUtils; 032import org.eclipse.aether.util.graph.transformer.ChainedDependencyGraphTransformer; 033import org.eclipse.aether.util.graph.transformer.ConfigurableVersionSelector; 034import org.eclipse.aether.util.graph.transformer.ConflictResolver; 035import org.eclipse.aether.util.graph.transformer.JavaDependencyContextRefiner; 036import org.eclipse.aether.util.graph.transformer.JavaScopeDeriver; 037import org.eclipse.aether.util.graph.transformer.JavaScopeSelector; 038import org.eclipse.aether.util.graph.transformer.SimpleOptionalitySelector; 039 040/** 041 * Visualizes the transitive dependencies of an artifact similar to m2e's dependency hierarchy view. 042 */ 043public class GetDependencyHierarchyWithConflicts { 044 045 /** 046 * Main. 047 * @param args 048 * @throws Exception 049 */ 050 public static void main(String[] args) throws Exception { 051 System.out.println("------------------------------------------------------------"); 052 System.out.println(GetDependencyHierarchyWithConflicts.class.getSimpleName()); 053 054 // incompatible versions: two incompatible versions present in graph 055 try (RepositorySystem system = Booter.newRepositorySystem(Booter.selectFactory(args))) { 056 SessionBuilder sessionBuilder = Booter.newRepositorySystemSession(system); 057 sessionBuilder.setConfigProperty(ConflictResolver.CONFIG_PROP_VERBOSE, true); 058 sessionBuilder.setConfigProperty(DependencyManagerUtils.CONFIG_PROP_VERBOSE, true); 059 try (CloseableSession session = sessionBuilder 060 .setDependencyGraphTransformer(new ChainedDependencyGraphTransformer( 061 new ConflictResolver( 062 new ConfigurableVersionSelector( 063 new ConfigurableVersionSelector.MajorVersionConvergence( 064 new ConfigurableVersionSelector.Nearest())), 065 new JavaScopeSelector(), 066 new SimpleOptionalitySelector(), 067 new JavaScopeDeriver()), 068 new JavaDependencyContextRefiner())) 069 .build()) { 070 Artifact artifact = new DefaultArtifact("org.apache.maven.shared:maven-dependency-tree:3.0.1"); 071 072 ArtifactDescriptorRequest descriptorRequest = new ArtifactDescriptorRequest(); 073 descriptorRequest.setArtifact(artifact); 074 descriptorRequest.setRepositories(Booter.newRepositories(system, session)); 075 ArtifactDescriptorResult descriptorResult = system.readArtifactDescriptor(session, descriptorRequest); 076 077 CollectRequest collectRequest = new CollectRequest(); 078 collectRequest.setRootArtifact(descriptorResult.getArtifact()); 079 collectRequest.setDependencies(descriptorResult.getDependencies()); 080 collectRequest.setManagedDependencies(descriptorResult.getManagedDependencies()); 081 collectRequest.setRepositories(descriptorRequest.getRepositories()); 082 083 system.collectDependencies(session, collectRequest); 084 throw new IllegalStateException("should fail"); 085 } 086 } catch (Exception e) { 087 e.printStackTrace(); 088 if (e.getCause() instanceof UnsolvableVersionConflictException) { 089 String cause = e.getCause().getMessage(); 090 if (!cause.contains( 091 "Incompatible versions for org.apache.maven:maven-core, incompatible versions: [2.0], all versions [2.0, 3.0.4]")) { 092 throw new IllegalStateException("should fail due incompatible versions"); 093 } 094 } else { 095 throw new IllegalStateException("should fail due incompatible versions"); 096 } 097 } 098 099 // dependency divergence: multiple versions of same GA present in graph 100 try (RepositorySystem system = Booter.newRepositorySystem(Booter.selectFactory(args))) { 101 SessionBuilder sessionBuilder = Booter.newRepositorySystemSession(system); 102 sessionBuilder.setConfigProperty(ConflictResolver.CONFIG_PROP_VERBOSE, true); 103 sessionBuilder.setConfigProperty(DependencyManagerUtils.CONFIG_PROP_VERBOSE, true); 104 try (CloseableSession session = sessionBuilder 105 .setDependencyGraphTransformer(new ChainedDependencyGraphTransformer( 106 new ConflictResolver( 107 new ConfigurableVersionSelector(new ConfigurableVersionSelector.VersionConvergence( 108 new ConfigurableVersionSelector.Nearest())), 109 new JavaScopeSelector(), 110 new SimpleOptionalitySelector(), 111 new JavaScopeDeriver()), 112 new JavaDependencyContextRefiner())) 113 .build()) { 114 Artifact artifact = new DefaultArtifact("org.apache.maven.shared:maven-dependency-tree:3.1.0"); 115 116 ArtifactDescriptorRequest descriptorRequest = new ArtifactDescriptorRequest(); 117 descriptorRequest.setArtifact(artifact); 118 descriptorRequest.setRepositories(Booter.newRepositories(system, session)); 119 ArtifactDescriptorResult descriptorResult = system.readArtifactDescriptor(session, descriptorRequest); 120 121 CollectRequest collectRequest = new CollectRequest(); 122 collectRequest.setRootArtifact(descriptorResult.getArtifact()); 123 collectRequest.setDependencies(descriptorResult.getDependencies()); 124 collectRequest.setManagedDependencies(descriptorResult.getManagedDependencies()); 125 collectRequest.setRepositories(descriptorRequest.getRepositories()); 126 127 system.collectDependencies(session, collectRequest); 128 throw new IllegalStateException("should fail"); 129 } 130 } catch (Exception e) { 131 e.printStackTrace(); 132 if (e.getCause() instanceof UnsolvableVersionConflictException) { 133 String cause = e.getCause().getMessage(); 134 if (!cause.contains( 135 "Convergence violated for org.codehaus.plexus:plexus-utils, versions present: [2.1, 1.5.5, 2.0.6]")) { 136 throw new IllegalStateException("should fail due convergence violation"); 137 } 138 } else { 139 throw new IllegalStateException("should fail due convergence violation"); 140 } 141 } 142 } 143}