1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.resolver.examples;
20
21 import org.apache.maven.resolver.examples.util.Booter;
22 import org.eclipse.aether.RepositorySystem;
23 import org.eclipse.aether.RepositorySystemSession.CloseableSession;
24 import org.eclipse.aether.RepositorySystemSession.SessionBuilder;
25 import org.eclipse.aether.artifact.Artifact;
26 import org.eclipse.aether.artifact.DefaultArtifact;
27 import org.eclipse.aether.collection.CollectRequest;
28 import org.eclipse.aether.collection.UnsolvableVersionConflictException;
29 import org.eclipse.aether.resolution.ArtifactDescriptorRequest;
30 import org.eclipse.aether.resolution.ArtifactDescriptorResult;
31 import org.eclipse.aether.util.graph.manager.DependencyManagerUtils;
32 import org.eclipse.aether.util.graph.transformer.ChainedDependencyGraphTransformer;
33 import org.eclipse.aether.util.graph.transformer.ConfigurableVersionSelector;
34 import org.eclipse.aether.util.graph.transformer.ConflictResolver;
35 import org.eclipse.aether.util.graph.transformer.JavaDependencyContextRefiner;
36 import org.eclipse.aether.util.graph.transformer.JavaScopeDeriver;
37 import org.eclipse.aether.util.graph.transformer.JavaScopeSelector;
38 import org.eclipse.aether.util.graph.transformer.SimpleOptionalitySelector;
39
40
41
42
43 public class GetDependencyHierarchyWithConflicts {
44
45
46
47
48
49
50 public static void main(String[] args) throws Exception {
51 System.out.println("------------------------------------------------------------");
52 System.out.println(GetDependencyHierarchyWithConflicts.class.getSimpleName());
53
54
55 try (RepositorySystem system = Booter.newRepositorySystem(Booter.selectFactory(args))) {
56 SessionBuilder sessionBuilder = Booter.newRepositorySystemSession(system);
57 sessionBuilder.setConfigProperty(ConflictResolver.CONFIG_PROP_VERBOSE, true);
58 sessionBuilder.setConfigProperty(DependencyManagerUtils.CONFIG_PROP_VERBOSE, true);
59 try (CloseableSession session = sessionBuilder
60 .setDependencyGraphTransformer(new ChainedDependencyGraphTransformer(
61 new ConflictResolver(
62 new ConfigurableVersionSelector(
63 new ConfigurableVersionSelector.MajorVersionConvergence(
64 new ConfigurableVersionSelector.Nearest())),
65 new JavaScopeSelector(),
66 new SimpleOptionalitySelector(),
67 new JavaScopeDeriver()),
68 new JavaDependencyContextRefiner()))
69 .build()) {
70 Artifact artifact = new DefaultArtifact("org.apache.maven.shared:maven-dependency-tree:3.0.1");
71
72 ArtifactDescriptorRequest descriptorRequest = new ArtifactDescriptorRequest();
73 descriptorRequest.setArtifact(artifact);
74 descriptorRequest.setRepositories(Booter.newRepositories(system, session));
75 ArtifactDescriptorResult descriptorResult = system.readArtifactDescriptor(session, descriptorRequest);
76
77 CollectRequest collectRequest = new CollectRequest();
78 collectRequest.setRootArtifact(descriptorResult.getArtifact());
79 collectRequest.setDependencies(descriptorResult.getDependencies());
80 collectRequest.setManagedDependencies(descriptorResult.getManagedDependencies());
81 collectRequest.setRepositories(descriptorRequest.getRepositories());
82
83 system.collectDependencies(session, collectRequest);
84 throw new IllegalStateException("should fail");
85 }
86 } catch (Exception e) {
87 e.printStackTrace();
88 if (e.getCause() instanceof UnsolvableVersionConflictException) {
89 String cause = e.getCause().getMessage();
90 if (!cause.contains(
91 "Incompatible versions for org.apache.maven:maven-core, incompatible versions: [2.0], all versions [2.0, 3.0.4]")) {
92 throw new IllegalStateException("should fail due incompatible versions");
93 }
94 } else {
95 throw new IllegalStateException("should fail due incompatible versions");
96 }
97 }
98
99
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 }