View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.resolver.examples.resolver;
20  
21  import java.io.File;
22  import java.util.List;
23  
24  import org.apache.maven.resolver.examples.util.Booter;
25  import org.eclipse.aether.artifact.Artifact;
26  import org.eclipse.aether.artifact.DefaultArtifact;
27  import org.eclipse.aether.deployment.DeploymentException;
28  import org.eclipse.aether.graph.DependencyNode;
29  import org.eclipse.aether.installation.InstallationException;
30  import org.eclipse.aether.resolution.DependencyResolutionException;
31  import org.eclipse.aether.util.artifact.SubArtifact;
32  
33  /**
34   */
35  @SuppressWarnings("unused")
36  public class ResolverDemo {
37      public static void main(String[] args) throws Exception {
38          System.out.println("------------------------------------------------------------");
39          System.out.println(ResolverDemo.class.getSimpleName());
40  
41          Resolver resolver =
42                  new Resolver(Booter.selectFactory(args), "https://repo.maven.apache.org/maven2/", "target/aether-repo");
43          ResolverResult result = resolver.resolve("junit", "junit", "4.13.2");
44  
45          System.out.println("Result:");
46          System.out.println("classpath=" + result.getResolvedClassPath());
47          System.out.println("files=" + result.getResolvedFiles());
48          System.out.println("root=" + result.getRoot());
49      }
50  
51      public void resolve(final String factory) throws DependencyResolutionException {
52          Resolver resolver =
53                  new Resolver(factory, "http://localhost:8081/nexus/content/groups/public", "target/aether-repo");
54  
55          ResolverResult result = resolver.resolve("com.mycompany.app", "super-app", "1.0");
56  
57          // Get the root of the resolved tree of artifacts
58          //
59          DependencyNode root = result.getRoot();
60  
61          // Get the list of files for the artifacts resolved
62          //
63          List<File> artifacts = result.getResolvedFiles();
64  
65          // Get the classpath of the artifacts resolved
66          //
67          String classpath = result.getResolvedClassPath();
68      }
69  
70      public void installAndDeploy(final String factory) throws InstallationException, DeploymentException {
71          Resolver resolver =
72                  new Resolver(factory, "http://localhost:8081/nexus/content/groups/public", "target/aether-repo");
73  
74          Artifact artifact = new DefaultArtifact("com.mycompany.super", "super-core", "jar", "0.1-SNAPSHOT");
75          artifact = artifact.setFile(new File("jar-from-whatever-process.jar"));
76          Artifact pom = new SubArtifact(artifact, null, "pom");
77          pom = pom.setFile(new File("pom-from-whatever-process.xml"));
78  
79          // Install into the local repository specified
80          //
81          resolver.install(artifact, pom);
82  
83          // Deploy to a remote reposistory
84          //
85          resolver.deploy(artifact, pom, "http://localhost:8081/nexus/content/repositories/snapshots/");
86      }
87  }