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.resolver; 020 021import java.io.ByteArrayOutputStream; 022import java.io.PrintStream; 023import java.nio.charset.StandardCharsets; 024import java.nio.file.Path; 025 026import org.apache.maven.resolver.examples.util.Booter; 027import org.eclipse.aether.RepositorySystem; 028import org.eclipse.aether.RepositorySystemSession; 029import org.eclipse.aether.artifact.Artifact; 030import org.eclipse.aether.artifact.DefaultArtifact; 031import org.eclipse.aether.collection.CollectRequest; 032import org.eclipse.aether.deployment.DeployRequest; 033import org.eclipse.aether.deployment.DeploymentException; 034import org.eclipse.aether.graph.Dependency; 035import org.eclipse.aether.graph.DependencyNode; 036import org.eclipse.aether.installation.InstallRequest; 037import org.eclipse.aether.installation.InstallationException; 038import org.eclipse.aether.repository.Authentication; 039import org.eclipse.aether.repository.LocalRepository; 040import org.eclipse.aether.repository.RemoteRepository; 041import org.eclipse.aether.resolution.DependencyRequest; 042import org.eclipse.aether.resolution.DependencyResolutionException; 043import org.eclipse.aether.util.graph.visitor.DependencyGraphDumper; 044import org.eclipse.aether.util.graph.visitor.NodeListGenerator; 045import org.eclipse.aether.util.graph.visitor.PreorderDependencyNodeConsumerVisitor; 046import org.eclipse.aether.util.repository.AuthenticationBuilder; 047 048/** 049 */ 050public class Resolver { 051 private final String[] args; 052 053 private final String remoteRepository; 054 055 private final RepositorySystem repositorySystem; 056 057 private final LocalRepository localRepository; 058 059 public Resolver(String[] args, String remoteRepository, Path localRepository) { 060 this.args = args; 061 this.remoteRepository = remoteRepository; 062 this.repositorySystem = Booter.newRepositorySystem(Booter.selectFactory(args)); 063 this.localRepository = new LocalRepository(localRepository); 064 } 065 066 private RepositorySystemSession newSession() { 067 return Booter.newRepositorySystemSession(repositorySystem, Booter.selectFs(args)) 068 .withLocalRepositories(localRepository) 069 .setRepositoryListener(null) 070 .setTransferListener(null) 071 .build(); 072 } 073 074 public ResolverResult resolve(String groupId, String artifactId, String version) 075 throws DependencyResolutionException { 076 RepositorySystemSession session = newSession(); 077 Dependency dependency = new Dependency(new DefaultArtifact(groupId, artifactId, "", "jar", version), "runtime"); 078 RemoteRepository central = new RemoteRepository.Builder("central", "default", remoteRepository).build(); 079 080 CollectRequest collectRequest = new CollectRequest(); 081 collectRequest.setRoot(dependency); 082 collectRequest.addRepository(central); 083 084 DependencyRequest dependencyRequest = new DependencyRequest(); 085 dependencyRequest.setCollectRequest(collectRequest); 086 087 DependencyNode rootNode = 088 repositorySystem.resolveDependencies(session, dependencyRequest).getRoot(); 089 090 StringBuilder dump = new StringBuilder(); 091 displayTree(rootNode, dump); 092 System.out.println("Tree:"); 093 System.out.println(dump); 094 095 NodeListGenerator nlg = new NodeListGenerator(); 096 rootNode.accept(new PreorderDependencyNodeConsumerVisitor(nlg)); 097 098 return new ResolverResult(rootNode, nlg.getFiles(), nlg.getClassPath()); 099 } 100 101 public void install(Artifact artifact, Artifact pom) throws InstallationException { 102 RepositorySystemSession session = newSession(); 103 104 InstallRequest installRequest = new InstallRequest(); 105 installRequest.addArtifact(artifact).addArtifact(pom); 106 107 repositorySystem.install(session, installRequest); 108 } 109 110 public void deploy(Artifact artifact, Artifact pom, String remoteRepository) throws DeploymentException { 111 RepositorySystemSession session = newSession(); 112 113 Authentication auth = new AuthenticationBuilder() 114 .addUsername("admin") 115 .addPassword("admin123") 116 .build(); 117 RemoteRepository nexus = new RemoteRepository.Builder("nexus", "default", remoteRepository) 118 .setAuthentication(auth) 119 .build(); 120 121 DeployRequest deployRequest = new DeployRequest(); 122 deployRequest.addArtifact(artifact).addArtifact(pom); 123 deployRequest.setRepository(nexus); 124 125 repositorySystem.deploy(session, deployRequest); 126 } 127 128 private void displayTree(DependencyNode node, StringBuilder sb) { 129 ByteArrayOutputStream os = new ByteArrayOutputStream(1024); 130 PrintStream ps = new PrintStream(os, true, StandardCharsets.UTF_8); 131 node.accept(new DependencyGraphDumper(ps::println)); 132 sb.append(os); 133 } 134}