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