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.resolver;
20
21 import java.io.ByteArrayOutputStream;
22 import java.io.PrintStream;
23 import java.io.UnsupportedEncodingException;
24 import java.nio.charset.StandardCharsets;
25
26 import org.apache.maven.resolver.examples.util.Booter;
27 import org.eclipse.aether.DefaultRepositorySystemSession;
28 import org.eclipse.aether.RepositorySystem;
29 import org.eclipse.aether.RepositorySystemSession;
30 import org.eclipse.aether.artifact.Artifact;
31 import org.eclipse.aether.artifact.DefaultArtifact;
32 import org.eclipse.aether.collection.CollectRequest;
33 import org.eclipse.aether.deployment.DeployRequest;
34 import org.eclipse.aether.deployment.DeploymentException;
35 import org.eclipse.aether.graph.Dependency;
36 import org.eclipse.aether.graph.DependencyNode;
37 import org.eclipse.aether.installation.InstallRequest;
38 import org.eclipse.aether.installation.InstallationException;
39 import org.eclipse.aether.repository.Authentication;
40 import org.eclipse.aether.repository.LocalRepository;
41 import org.eclipse.aether.repository.RemoteRepository;
42 import org.eclipse.aether.resolution.DependencyRequest;
43 import org.eclipse.aether.resolution.DependencyResolutionException;
44 import org.eclipse.aether.util.graph.visitor.DependencyGraphDumper;
45 import org.eclipse.aether.util.graph.visitor.NodeListGenerator;
46 import org.eclipse.aether.util.graph.visitor.PreorderDependencyNodeConsumerVisitor;
47 import org.eclipse.aether.util.repository.AuthenticationBuilder;
48
49
50
51 public class Resolver {
52 private final String remoteRepository;
53
54 private final RepositorySystem repositorySystem;
55
56 private final LocalRepository localRepository;
57
58 public Resolver(String factory, String remoteRepository, String localRepository) {
59 this.remoteRepository = remoteRepository;
60 this.repositorySystem = Booter.newRepositorySystem(factory);
61 this.localRepository = new LocalRepository(localRepository);
62 }
63
64 private RepositorySystemSession newSession() {
65 DefaultRepositorySystemSession session = Booter.newRepositorySystemSession(repositorySystem);
66 session.setLocalRepositoryManager(repositorySystem.newLocalRepositoryManager(session, localRepository));
67 session.setTransferListener(null);
68 session.setRepositoryListener(null);
69 return session;
70 }
71
72 public ResolverResult resolve(String groupId, String artifactId, String version)
73 throws DependencyResolutionException {
74 RepositorySystemSession session = newSession();
75 Dependency dependency = new Dependency(new DefaultArtifact(groupId, artifactId, "", "jar", version), "runtime");
76 RemoteRepository central = new RemoteRepository.Builder("central", "default", remoteRepository).build();
77
78 CollectRequest collectRequest = new CollectRequest();
79 collectRequest.setRoot(dependency);
80 collectRequest.addRepository(central);
81
82 DependencyRequest dependencyRequest = new DependencyRequest();
83 dependencyRequest.setCollectRequest(collectRequest);
84
85 DependencyNode rootNode =
86 repositorySystem.resolveDependencies(session, dependencyRequest).getRoot();
87
88 StringBuilder dump = new StringBuilder();
89 displayTree(rootNode, dump);
90 System.out.println("Tree:");
91 System.out.println(dump);
92
93 NodeListGenerator nlg = new NodeListGenerator();
94 rootNode.accept(new PreorderDependencyNodeConsumerVisitor(nlg));
95
96 return new ResolverResult(rootNode, nlg.getFiles(), nlg.getClassPath());
97 }
98
99 public void install(Artifact artifact, Artifact pom) throws InstallationException {
100 RepositorySystemSession session = newSession();
101
102 InstallRequest installRequest = new InstallRequest();
103 installRequest.addArtifact(artifact).addArtifact(pom);
104
105 repositorySystem.install(session, installRequest);
106 }
107
108 public void deploy(Artifact artifact, Artifact pom, String remoteRepository) throws DeploymentException {
109 RepositorySystemSession session = newSession();
110
111 Authentication auth = new AuthenticationBuilder()
112 .addUsername("admin")
113 .addPassword("admin123")
114 .build();
115 RemoteRepository nexus = new RemoteRepository.Builder("nexus", "default", remoteRepository)
116 .setAuthentication(auth)
117 .build();
118
119 DeployRequest deployRequest = new DeployRequest();
120 deployRequest.addArtifact(artifact).addArtifact(pom);
121 deployRequest.setRepository(nexus);
122
123 repositorySystem.deploy(session, deployRequest);
124 }
125
126 private void displayTree(DependencyNode node, StringBuilder sb) {
127 try {
128 ByteArrayOutputStream os = new ByteArrayOutputStream(1024);
129 PrintStream ps = new PrintStream(os, true, StandardCharsets.UTF_8.name());
130 node.accept(new DependencyGraphDumper(ps::println));
131 sb.append(os);
132 } catch (UnsupportedEncodingException e) {
133 throw new RuntimeException(e);
134 }
135 }
136 }