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.Artifact;
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.apache.maven.shared.transfer.artifact.resolve.ArtifactResult;
31  import org.codehaus.plexus.PlexusConstants;
32  import org.codehaus.plexus.PlexusContainer;
33  import org.codehaus.plexus.component.annotations.Component;
34  import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
35  import org.codehaus.plexus.context.Context;
36  import org.codehaus.plexus.context.ContextException;
37  import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
38  
39  
40  /**
41   *
42   */
43  @Component( role = ArtifactResolver.class, hint = "default" )
44  class DefaultArtifactResolver implements ArtifactResolver, Contextualizable
45  {
46      private PlexusContainer container;
47  
48      @Override
49      public ArtifactResult resolveArtifact( ProjectBuildingRequest buildingRequest, Artifact mavenArtifact )
50              throws ArtifactResolverException, IllegalArgumentException
51      {
52          validateParameters( buildingRequest, mavenArtifact );
53          try
54          {
55              return getMavenArtifactResolver( buildingRequest ).resolveArtifact( mavenArtifact );
56          }
57          catch ( ComponentLookupException e )
58          {
59              throw new ArtifactResolverException( e.getMessage(), e );
60          }
61      }
62  
63      @Override
64      public ArtifactResult resolveArtifact( ProjectBuildingRequest buildingRequest, ArtifactCoordinate coordinate )
65              throws ArtifactResolverException, IllegalArgumentException
66      {
67          validateParameters( buildingRequest, coordinate );
68          try
69          {
70              return getMavenArtifactResolver( buildingRequest ).resolveArtifact( coordinate );
71          }
72          catch ( ComponentLookupException e )
73          {
74              throw new ArtifactResolverException( e.getMessage(), e );
75          }
76      }
77  
78      private void validateParameters( ProjectBuildingRequest buildingRequest, Artifact mavenArtifact )
79      {
80          if ( buildingRequest == null )
81          {
82              throw new IllegalArgumentException( "The parameter buildingRequest is not allowed to be null." );
83          }
84          if ( mavenArtifact == null )
85          {
86              throw new IllegalArgumentException( "The parameter mavenArtifact is not allowed to be null." );
87          }
88      }
89  
90      private void validateParameters( ProjectBuildingRequest buildingRequest, ArtifactCoordinate coordinate )
91      {
92          if ( buildingRequest == null )
93          {
94              throw new IllegalArgumentException( "The parameter buildingRequest is not allowed to be null." );
95          }
96          if ( coordinate == null )
97          {
98              throw new IllegalArgumentException( "The parameter coordinate is not allowed to be null." );
99          }
100     }
101 
102     /**
103      * @return true if the current Maven version is Maven 3.1.
104      */
105     private boolean isMaven31()
106     {
107         try
108         {
109             // Maven 3.1 specific
110             Thread.currentThread().getContextClassLoader().loadClass( "org.eclipse.aether.artifact.Artifact" );
111             return true;
112         }
113         catch ( ClassNotFoundException e )
114         {
115             return false;
116         }
117     }
118 
119     /**
120      * Injects the Plexus content.
121      *
122      * @param context Plexus context to inject.
123      * @throws ContextException if the PlexusContainer could not be located.
124      */
125     public void contextualize( Context context ) throws ContextException
126     {
127         container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY );
128     }
129 
130     private MavenArtifactResolver getMavenArtifactResolver( ProjectBuildingRequest buildingRequest )
131             throws ComponentLookupException, ArtifactResolverException
132     {
133         if ( isMaven31() )
134         {
135             org.eclipse.aether.RepositorySystem repositorySystem = container.lookup(
136                     org.eclipse.aether.RepositorySystem.class );
137 
138             List<org.eclipse.aether.repository.RemoteRepository> aetherRepositories = Invoker.invoke(
139                     RepositoryUtils.class, "toRepos", List.class, buildingRequest.getRemoteRepositories() );
140 
141             org.eclipse.aether.RepositorySystemSession session = Invoker.invoke( buildingRequest,
142                     "getRepositorySession" );
143 
144             return new Maven31ArtifactResolver( repositorySystem, aetherRepositories, session );
145 
146         }
147         else
148         {
149             org.sonatype.aether.RepositorySystem repositorySystem = container.lookup(
150                     org.sonatype.aether.RepositorySystem.class );
151 
152             List<org.sonatype.aether.repository.RemoteRepository> aetherRepositories = Invoker.invoke(
153                     RepositoryUtils.class, "toRepos", List.class, buildingRequest.getRemoteRepositories() );
154 
155             org.sonatype.aether.RepositorySystemSession session = Invoker.invoke( buildingRequest,
156                     "getRepositorySession" );
157 
158             return new Maven30ArtifactResolver( repositorySystem, aetherRepositories, session );
159         }
160 
161 
162     }
163 }