View Javadoc
1   package org.apache.maven.shared.transfer.artifact.resolve.internal;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.List;
23  
24  import org.apache.maven.RepositoryUtils;
25  import org.apache.maven.shared.transfer.artifact.ArtifactCoordinate;
26  import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolverException;
27  import org.sonatype.aether.RepositorySystem;
28  import org.sonatype.aether.RepositorySystemSession;
29  import org.sonatype.aether.artifact.Artifact;
30  import org.sonatype.aether.repository.RemoteRepository;
31  import org.sonatype.aether.resolution.ArtifactDescriptorException;
32  import org.sonatype.aether.resolution.ArtifactDescriptorRequest;
33  import org.sonatype.aether.resolution.ArtifactDescriptorResult;
34  import org.sonatype.aether.resolution.ArtifactRequest;
35  import org.sonatype.aether.resolution.ArtifactResolutionException;
36  import org.sonatype.aether.util.artifact.DefaultArtifact;
37  
38  /**
39   * 
40   */
41  class Maven30ArtifactResolver
42      implements MavenArtifactResolver
43  {
44      private final RepositorySystem repositorySystem;
45  
46      private final List<RemoteRepository> aetherRepositories;
47      
48      private final RepositorySystemSession session;
49  
50      Maven30ArtifactResolver( RepositorySystem repositorySystem, List<RemoteRepository> aetherRepositories,
51                                      RepositorySystemSession session )
52      {
53          this.repositorySystem = repositorySystem;
54          this.aetherRepositories = aetherRepositories;
55          this.session = session;
56      }
57  
58      @Override
59      // CHECKSTYLE_OFF: LineLength
60      public org.apache.maven.shared.transfer.artifact.resolve.ArtifactResult resolveArtifact( org.apache.maven.artifact.Artifact mavenArtifact )
61                                                                                          throws ArtifactResolverException
62      // CHECKSTYLE_ON: LineLength
63      {
64          Artifact aetherArtifact = Invoker.invoke( RepositoryUtils.class, "toArtifact",
65                                                               org.apache.maven.artifact.Artifact.class, mavenArtifact );
66  
67          return resolveArtifact( aetherArtifact );
68      }
69  
70      @Override
71      // CHECKSTYLE_OFF: LineLength
72      public org.apache.maven.shared.transfer.artifact.resolve.ArtifactResult resolveArtifact( ArtifactCoordinate coordinate )
73                                                                                          throws ArtifactResolverException
74      // CHECKSTYLE_ON: LineLength
75      {
76          Artifact aetherArtifact =
77              new DefaultArtifact( coordinate.getGroupId(), coordinate.getArtifactId(), coordinate.getClassifier(),
78                                   coordinate.getExtension(), coordinate.getVersion() );
79  
80          return resolveArtifact( aetherArtifact );
81      }
82  
83      // CHECKSTYLE_OFF: LineLength
84      private org.apache.maven.shared.transfer.artifact.resolve.ArtifactResult resolveArtifact( Artifact aetherArtifact )
85                                                                                           throws ArtifactResolverException
86      // CHECKSTYLE_ON: LineLength
87      {
88          try
89          {
90              // use descriptor to respect relocation
91              ArtifactDescriptorRequest descriptorRequest =
92                  new ArtifactDescriptorRequest( aetherArtifact, aetherRepositories, null );
93  
94              ArtifactDescriptorResult descriptorResult =
95                  repositorySystem.readArtifactDescriptor( session, descriptorRequest );
96  
97              ArtifactRequest request = new ArtifactRequest( descriptorResult.getArtifact(), aetherRepositories, null );
98  
99              return new Maven30ArtifactResult( repositorySystem.resolveArtifact( session, request ) );
100         }
101         catch ( ArtifactDescriptorException | ArtifactResolutionException e )
102         {
103             throw new ArtifactResolverException( e.getMessage(), e );
104         }
105     }
106 
107 }