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.io.UnsupportedEncodingException;
024import java.nio.charset.StandardCharsets;
025
026import org.apache.maven.resolver.examples.util.Booter;
027import org.eclipse.aether.DefaultRepositorySystemSession;
028import org.eclipse.aether.RepositorySystem;
029import org.eclipse.aether.RepositorySystemSession;
030import org.eclipse.aether.artifact.Artifact;
031import org.eclipse.aether.artifact.DefaultArtifact;
032import org.eclipse.aether.collection.CollectRequest;
033import org.eclipse.aether.deployment.DeployRequest;
034import org.eclipse.aether.deployment.DeploymentException;
035import org.eclipse.aether.graph.Dependency;
036import org.eclipse.aether.graph.DependencyNode;
037import org.eclipse.aether.installation.InstallRequest;
038import org.eclipse.aether.installation.InstallationException;
039import org.eclipse.aether.repository.Authentication;
040import org.eclipse.aether.repository.LocalRepository;
041import org.eclipse.aether.repository.RemoteRepository;
042import org.eclipse.aether.resolution.DependencyRequest;
043import org.eclipse.aether.resolution.DependencyResolutionException;
044import org.eclipse.aether.util.graph.visitor.DependencyGraphDumper;
045import org.eclipse.aether.util.graph.visitor.PreorderNodeListGenerator;
046import org.eclipse.aether.util.repository.AuthenticationBuilder;
047
048/**
049 */
050public class Resolver {
051    private final String remoteRepository;
052
053    private final RepositorySystem repositorySystem;
054
055    private final LocalRepository localRepository;
056
057    public Resolver(String factory, String remoteRepository, String localRepository) {
058        this.remoteRepository = remoteRepository;
059        this.repositorySystem = Booter.newRepositorySystem(factory);
060        this.localRepository = new LocalRepository(localRepository);
061    }
062
063    private RepositorySystemSession newSession() {
064        DefaultRepositorySystemSession session = Booter.newRepositorySystemSession(repositorySystem);
065        session.setLocalRepositoryManager(repositorySystem.newLocalRepositoryManager(session, localRepository));
066        session.setTransferListener(null);
067        session.setRepositoryListener(null);
068        return session;
069    }
070
071    public ResolverResult resolve(String groupId, String artifactId, String version)
072            throws DependencyResolutionException {
073        RepositorySystemSession session = newSession();
074        Dependency dependency = new Dependency(new DefaultArtifact(groupId, artifactId, "", "jar", version), "runtime");
075        RemoteRepository central = new RemoteRepository.Builder("central", "default", remoteRepository).build();
076
077        CollectRequest collectRequest = new CollectRequest();
078        collectRequest.setRoot(dependency);
079        collectRequest.addRepository(central);
080
081        DependencyRequest dependencyRequest = new DependencyRequest();
082        dependencyRequest.setCollectRequest(collectRequest);
083
084        DependencyNode rootNode =
085                repositorySystem.resolveDependencies(session, dependencyRequest).getRoot();
086
087        StringBuilder dump = new StringBuilder();
088        displayTree(rootNode, dump);
089        System.out.println("Tree:");
090        System.out.println(dump);
091
092        PreorderNodeListGenerator nlg = new PreorderNodeListGenerator();
093        rootNode.accept(nlg);
094
095        return new ResolverResult(rootNode, nlg.getFiles(), nlg.getClassPath());
096    }
097
098    public void install(Artifact artifact, Artifact pom) throws InstallationException {
099        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}