001package org.apache.maven.resolver.examples.resolver;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 * 
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 * 
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.io.ByteArrayOutputStream;
023import java.io.PrintStream;
024
025import org.apache.maven.resolver.examples.util.Booter;
026import org.apache.maven.resolver.examples.util.ConsoleDependencyGraphDumper;
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.PreorderNodeListGenerator;
045import org.eclipse.aether.util.repository.AuthenticationBuilder;
046
047/**
048 */
049public class Resolver
050{
051    private String remoteRepository;
052
053    private RepositorySystem repositorySystem;
054
055    private LocalRepository localRepository;
056
057    public Resolver( String remoteRepository, String localRepository )
058    {
059        this.remoteRepository = remoteRepository;
060        this.repositorySystem = Booter.newRepositorySystem();
061        this.localRepository = new LocalRepository( localRepository );
062    }
063
064    private RepositorySystemSession newSession()
065    {
066        DefaultRepositorySystemSession session = Booter.newRepositorySystemSession( repositorySystem );
067        session.setLocalRepositoryManager( repositorySystem.newLocalRepositoryManager( session, localRepository ) );
068        return session;
069    }
070
071    public ResolverResult resolve( String groupId, String artifactId, String version )
072        throws DependencyResolutionException
073    {
074        RepositorySystemSession session = newSession();
075        Dependency dependency =
076            new Dependency( new DefaultArtifact( groupId, artifactId, "", "jar", version ), "runtime" );
077        RemoteRepository central = new RemoteRepository.Builder( "central", "default", remoteRepository ).build();
078
079        CollectRequest collectRequest = new CollectRequest();
080        collectRequest.setRoot( dependency );
081        collectRequest.addRepository( central );
082
083        DependencyRequest dependencyRequest = new DependencyRequest();
084        dependencyRequest.setCollectRequest( collectRequest );
085
086        DependencyNode rootNode = repositorySystem.resolveDependencies( session, dependencyRequest ).getRoot();
087
088        StringBuilder dump = new StringBuilder();
089        displayTree( rootNode, dump );
090
091        PreorderNodeListGenerator nlg = new PreorderNodeListGenerator();
092        rootNode.accept( nlg );
093
094        return new ResolverResult( rootNode, nlg.getFiles(), nlg.getClassPath() );
095    }
096
097    public void install( Artifact artifact, Artifact pom )
098        throws InstallationException
099    {
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 )
109        throws DeploymentException
110    {
111        RepositorySystemSession session = newSession();
112
113        Authentication auth = new AuthenticationBuilder().addUsername( "admin" ).addPassword( "admin123" ).build();
114        RemoteRepository nexus =
115            new RemoteRepository.Builder( "nexus", "default", remoteRepository ).setAuthentication( auth ).build();
116
117        DeployRequest deployRequest = new DeployRequest();
118        deployRequest.addArtifact( artifact ).addArtifact( pom );
119        deployRequest.setRepository( nexus );
120
121        repositorySystem.deploy( session, deployRequest );
122    }
123
124    private void displayTree( DependencyNode node, StringBuilder sb )
125    {
126        ByteArrayOutputStream os = new ByteArrayOutputStream( 1024 );
127        node.accept( new ConsoleDependencyGraphDumper( new PrintStream( os ) ) );
128        sb.append( os.toString() );
129    }
130
131}