View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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.PreorderNodeListGenerator;
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          DefaultRepositorySystemSession session = Booter.newRepositorySystemSession(repositorySystem);
65          session.setLocalRepositoryManager(repositorySystem.newLocalRepositoryManager(session, localRepository));
66          session.setTransferListener(null);
67          session.setRepositoryListener(null);
68          return session;
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          PreorderNodeListGenerator nlg = new PreorderNodeListGenerator();
93          rootNode.accept(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 }