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;
20  
21  import org.apache.maven.resolver.examples.util.Booter;
22  import org.eclipse.aether.RepositorySystem;
23  import org.eclipse.aether.RepositorySystemSession;
24  import org.eclipse.aether.RepositorySystemSession.CloseableSession;
25  import org.eclipse.aether.artifact.Artifact;
26  import org.eclipse.aether.artifact.DefaultArtifact;
27  import org.eclipse.aether.repository.RepositoryPolicy;
28  import org.eclipse.aether.resolution.ArtifactRequest;
29  import org.eclipse.aether.resolution.ArtifactResult;
30  
31  /**
32   * Resolves a single artifact.
33   */
34  public class ResolveArtifact {
35  
36      /**
37       * Main.
38       * @param args
39       * @throws Exception
40       */
41      public static void main(String[] args) throws Exception {
42          System.out.println("------------------------------------------------------------");
43          System.out.println(ResolveArtifact.class.getSimpleName());
44  
45          try (RepositorySystem system = Booter.newRepositorySystem(Booter.selectFactory(args))) {
46              RepositorySystemSession.SessionBuilder sessionBuilder = Booter.newRepositorySystemSession(system);
47              Artifact artifact;
48              ArtifactRequest artifactRequest;
49              ArtifactResult artifactResult;
50  
51              try (CloseableSession session = sessionBuilder.build()) {
52                  artifact = new DefaultArtifact("org.apache.maven.resolver:maven-resolver-util:1.3.3");
53  
54                  artifactRequest = new ArtifactRequest();
55                  artifactRequest.setArtifact(artifact);
56                  artifactRequest.setRepositories(Booter.newRepositories(system, session));
57  
58                  artifactResult = system.resolveArtifact(session, artifactRequest);
59  
60                  artifact = artifactResult.getArtifact();
61  
62                  System.out.println(artifact + " resolved to  " + artifact.getFile());
63              }
64  
65              // signature
66              sessionBuilder.setChecksumPolicy(RepositoryPolicy.CHECKSUM_POLICY_FAIL);
67  
68              try (CloseableSession session = sessionBuilder.build()) {
69                  artifact = new DefaultArtifact("org.apache.maven.resolver:maven-resolver-util:jar.asc:1.3.3");
70  
71                  artifactRequest = new ArtifactRequest();
72                  artifactRequest.setArtifact(artifact);
73                  artifactRequest.setRepositories(Booter.newRepositories(system, session));
74  
75                  artifactResult = system.resolveArtifact(session, artifactRequest);
76  
77                  artifact = artifactResult.getArtifact();
78  
79                  System.out.println(artifact + " resolved signature to  " + artifact.getFile());
80              }
81          }
82      }
83  }