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