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.artifact.handler.manager.ArtifactHandlerManager;
26  import org.apache.maven.project.ProjectBuildingRequest;
27  import org.apache.maven.shared.transfer.artifact.ArtifactCoordinate;
28  import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolver;
29  import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolverException;
30  import org.codehaus.plexus.component.annotations.Component;
31  import org.codehaus.plexus.component.annotations.Requirement;
32  import org.eclipse.aether.RepositorySystem;
33  import org.eclipse.aether.RepositorySystemSession;
34  import org.eclipse.aether.artifact.Artifact;
35  import org.eclipse.aether.artifact.DefaultArtifact;
36  import org.eclipse.aether.repository.RemoteRepository;
37  import org.eclipse.aether.resolution.ArtifactDescriptorException;
38  import org.eclipse.aether.resolution.ArtifactDescriptorRequest;
39  import org.eclipse.aether.resolution.ArtifactDescriptorResult;
40  import org.eclipse.aether.resolution.ArtifactRequest;
41  import org.eclipse.aether.resolution.ArtifactResolutionException;
42  
43  /**
44   * 
45   */
46  @Component( role = ArtifactResolver.class, hint = "maven31" )
47  class Maven31ArtifactResolver
48      implements ArtifactResolver
49  {
50      @Requirement
51      private RepositorySystem repositorySystem;
52  
53      @Requirement
54      private ArtifactHandlerManager artifactHandlerManager;
55  
56      @Override
57      // CHECKSTYLE_OFF: LineLength
58      public org.apache.maven.shared.transfer.artifact.resolve.ArtifactResult resolveArtifact( ProjectBuildingRequest buildingRequest,
59                                                                                      org.apache.maven.artifact.Artifact mavenArtifact )
60                                                                                          throws ArtifactResolverException
61      // CHECKSTYLE_ON: LineLength
62      {
63          Artifact aetherArtifact = (Artifact) Invoker.invoke( RepositoryUtils.class, "toArtifact",
64                                                               org.apache.maven.artifact.Artifact.class, mavenArtifact );
65  
66          return resolveArtifact( buildingRequest, aetherArtifact );
67      }
68  
69      @Override
70      // CHECKSTYLE_OFF: LineLength
71      public org.apache.maven.shared.transfer.artifact.resolve.ArtifactResult resolveArtifact( ProjectBuildingRequest buildingRequest,
72                                                                                      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( buildingRequest, aetherArtifact );
81      }
82  
83      // CHECKSTYLE_OFF: LineLength
84      private org.apache.maven.shared.transfer.artifact.resolve.ArtifactResult resolveArtifact( ProjectBuildingRequest buildingRequest,
85                                                                                       Artifact aetherArtifact )
86                                                                                           throws ArtifactResolverException
87      // CHECKSTYLE_ON: LineLength
88      {
89          @SuppressWarnings( "unchecked" )
90          List<RemoteRepository> aetherRepositories =
91              (List<RemoteRepository>) Invoker.invoke( RepositoryUtils.class, "toRepos", List.class,
92                                                       buildingRequest.getRemoteRepositories() );
93  
94          RepositorySystemSession session =
95              (RepositorySystemSession) Invoker.invoke( buildingRequest, "getRepositorySession" );
96  
97          try
98          {
99              // use descriptor to respect relocation
100             ArtifactDescriptorRequest descriptorRequest =
101                 new ArtifactDescriptorRequest( aetherArtifact, aetherRepositories, null );
102 
103             ArtifactDescriptorResult descriptorResult =
104                 repositorySystem.readArtifactDescriptor( session, descriptorRequest );
105 
106             ArtifactRequest request = new ArtifactRequest( descriptorResult.getArtifact(), aetherRepositories, null );
107 
108             return new Maven31ArtifactResult( repositorySystem.resolveArtifact( session, request ) );
109         }
110         catch ( ArtifactDescriptorException e )
111         {
112             throw new ArtifactResolverException( e.getMessage(), e );
113         }
114         catch ( ArtifactResolutionException e )
115         {
116             throw new ArtifactResolverException( e.getMessage(), e );
117         }
118     }
119 
120 }