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.File;
023import java.util.List;
024
025import org.apache.maven.resolver.examples.util.Booter;
026import org.eclipse.aether.artifact.Artifact;
027import org.eclipse.aether.artifact.DefaultArtifact;
028import org.eclipse.aether.deployment.DeploymentException;
029import org.eclipse.aether.graph.DependencyNode;
030import org.eclipse.aether.installation.InstallationException;
031import org.eclipse.aether.resolution.DependencyResolutionException;
032import org.eclipse.aether.util.artifact.SubArtifact;
033
034/**
035 */
036@SuppressWarnings( "unused" )
037public class ResolverDemo
038{
039    public static void main( String[] args ) throws Exception
040    {
041        System.out.println( "------------------------------------------------------------" );
042        System.out.println( ResolverDemo.class.getSimpleName() );
043
044        Resolver resolver = new Resolver(
045                Booter.selectFactory( args ),
046                "https://repo.maven.apache.org/maven2/",
047                "target/aether-repo"
048        );
049        ResolverResult result = resolver.resolve( "junit", "junit", "4.13.2" );
050
051        System.out.println( "Result:" );
052        System.out.println( "classpath=" + result.getResolvedClassPath() );
053        System.out.println( "files=" + result.getResolvedFiles() );
054        System.out.println( "root=" + result.getRoot() );
055    }
056
057    public void resolve( final String factory )
058        throws DependencyResolutionException
059    {
060        Resolver resolver = new Resolver(
061            factory,
062            "http://localhost:8081/nexus/content/groups/public", "target/aether-repo" );
063                
064        ResolverResult result = resolver.resolve( "com.mycompany.app", "super-app", "1.0" );
065
066        // Get the root of the resolved tree of artifacts
067        //
068        DependencyNode root = result.getRoot();
069
070        // Get the list of files for the artifacts resolved
071        //
072        List<File> artifacts = result.getResolvedFiles();
073        
074        // Get the classpath of the artifacts resolved
075        //
076        String classpath = result.getResolvedClassPath();        
077    }
078    
079    public void installAndDeploy( final String factory )
080        throws InstallationException, DeploymentException
081    {
082        Resolver resolver = new Resolver(
083            factory,
084            "http://localhost:8081/nexus/content/groups/public", "target/aether-repo" );
085        
086        Artifact artifact = new DefaultArtifact( "com.mycompany.super", "super-core", "jar", "0.1-SNAPSHOT" );
087        artifact = artifact.setFile( new File( "jar-from-whatever-process.jar" ) );
088        Artifact pom = new SubArtifact( artifact, null, "pom" );
089        pom = pom.setFile( new File( "pom-from-whatever-process.xml" ) );
090          
091        // Install into the local repository specified
092        //
093        resolver.install( artifact, pom );
094        
095        // Deploy to a remote reposistory
096        //
097        resolver.deploy( artifact, pom, "http://localhost:8081/nexus/content/repositories/snapshots/" );
098    }
099
100}