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.eclipse.aether.RepositorySystem;
28  import org.eclipse.aether.RepositorySystemSession;
29  import org.eclipse.aether.artifact.Artifact;
30  import org.eclipse.aether.artifact.DefaultArtifact;
31  import org.eclipse.aether.repository.RemoteRepository;
32  import org.eclipse.aether.resolution.ArtifactDescriptorException;
33  import org.eclipse.aether.resolution.ArtifactDescriptorRequest;
34  import org.eclipse.aether.resolution.ArtifactDescriptorResult;
35  import org.eclipse.aether.resolution.ArtifactRequest;
36  import org.eclipse.aether.resolution.ArtifactResolutionException;
37  
38  /**
39   * 
40   */
41  class Maven31ArtifactResolver
42      implements MavenArtifactResolver
43  {
44      private final RepositorySystem repositorySystem;
45      
46      private final List<RemoteRepository> aetherRepositories;
47      
48      private final RepositorySystemSession session;
49  
50      Maven31ArtifactResolver( 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      public org.apache.maven.shared.transfer.artifact.resolve.ArtifactResult resolveArtifact(
60              org.apache.maven.artifact.Artifact mavenArtifact ) throws ArtifactResolverException
61      {
62          Artifact aetherArtifact = Invoker.invoke( RepositoryUtils.class, "toArtifact",
63                  org.apache.maven.artifact.Artifact.class, mavenArtifact );
64  
65          return resolveArtifact( aetherArtifact );
66      }
67  
68      @Override
69      // CHECKSTYLE_OFF: LineLength
70      public org.apache.maven.shared.transfer.artifact.resolve.ArtifactResult resolveArtifact( ArtifactCoordinate coordinate )
71                                                                                          throws ArtifactResolverException
72      // CHECKSTYLE_ON: LineLength
73      {
74          Artifact aetherArtifact =
75              new DefaultArtifact( coordinate.getGroupId(), coordinate.getArtifactId(), coordinate.getClassifier(),
76                                   coordinate.getExtension(), coordinate.getVersion() );
77  
78          return resolveArtifact( aetherArtifact );
79      }
80  
81      // CHECKSTYLE_OFF: LineLength
82      private org.apache.maven.shared.transfer.artifact.resolve.ArtifactResult resolveArtifact( Artifact aetherArtifact )
83                                                                                           throws ArtifactResolverException
84      // CHECKSTYLE_ON: LineLength
85      {
86          try
87          {
88              // use descriptor to respect relocation
89              ArtifactDescriptorRequest descriptorRequest =
90                  new ArtifactDescriptorRequest( aetherArtifact, aetherRepositories, null );
91  
92              ArtifactDescriptorResult descriptorResult =
93                  repositorySystem.readArtifactDescriptor( session, descriptorRequest );
94  
95              ArtifactRequest request = new ArtifactRequest( descriptorResult.getArtifact(), aetherRepositories, null );
96  
97              return new Maven31ArtifactResult( repositorySystem.resolveArtifact( session, request ) );
98          }
99          catch ( ArtifactDescriptorException | ArtifactResolutionException e )
100         {
101             throw new ArtifactResolverException( e.getMessage(), e );
102         }
103     }
104 
105 }