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 org.apache.maven.artifact.Artifact;
23  import org.apache.maven.project.ProjectBuildingRequest;
24  import org.apache.maven.shared.transfer.artifact.ArtifactCoordinate;
25  import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolver;
26  import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolverException;
27  import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResult;
28  import org.codehaus.plexus.PlexusConstants;
29  import org.codehaus.plexus.PlexusContainer;
30  import org.codehaus.plexus.component.annotations.Component;
31  import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
32  import org.codehaus.plexus.context.Context;
33  import org.codehaus.plexus.context.ContextException;
34  import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
35  
36  /**
37   * 
38   */
39  @Component( role = ArtifactResolver.class, hint = "default" )
40  class DefaultArtifactResolver
41      implements ArtifactResolver, Contextualizable
42  {
43      private PlexusContainer container;
44  
45      @Override
46      public ArtifactResult resolveArtifact( ProjectBuildingRequest buildingRequest, Artifact mavenArtifact )
47          throws ArtifactResolverException, IllegalArgumentException
48      {
49          validateParameters( buildingRequest, mavenArtifact );
50          try
51          {
52              String hint = isMaven31() ? "maven31" : "maven3";
53  
54              ArtifactResolver effectiveArtifactResolver = container.lookup( ArtifactResolver.class, hint );
55  
56              return effectiveArtifactResolver.resolveArtifact( buildingRequest, mavenArtifact );
57          }
58          catch ( ComponentLookupException e )
59          {
60              throw new ArtifactResolverException( e.getMessage(), e );
61          }
62      }
63  
64      @Override
65      public ArtifactResult resolveArtifact( ProjectBuildingRequest buildingRequest, ArtifactCoordinate coordinate )
66          throws ArtifactResolverException, IllegalArgumentException
67      {
68          validateParameters( buildingRequest, coordinate );
69          try
70          {
71              String hint = isMaven31() ? "maven31" : "maven3";
72  
73              ArtifactResolver effectiveArtifactResolver = container.lookup( ArtifactResolver.class, hint );
74  
75              return effectiveArtifactResolver.resolveArtifact( buildingRequest, coordinate );
76          }
77          catch ( ComponentLookupException e )
78          {
79              throw new ArtifactResolverException( e.getMessage(), e );
80          }
81      }
82  
83      private void validateParameters( ProjectBuildingRequest buildingRequest, Artifact mavenArtifact )
84      {
85          if ( buildingRequest == null )
86          {
87              throw new IllegalArgumentException( "The parameter buildingRequest is not allowed to be null." );
88          }
89          if ( mavenArtifact == null )
90          {
91              throw new IllegalArgumentException( "The parameter mavenArtifact is not allowed to be null." );
92          }
93      }
94  
95      private void validateParameters( ProjectBuildingRequest buildingRequest, ArtifactCoordinate coordinate )
96      {
97          if ( buildingRequest == null )
98          {
99              throw new IllegalArgumentException( "The parameter buildingRequest is not allowed to be null." );
100         }
101         if ( coordinate == null )
102         {
103             throw new IllegalArgumentException( "The parameter coordinate is not allowed to be null." );
104         }
105     }
106 
107     // @Override
108     // public Iterable<ArtifactResult> resolveDependencies( ProjectBuildingRequest buildingRequest,
109     // Collection<Dependency> coordinates,
110     // Collection<Dependency> managedDependencies,
111     // TransformableFilter filter )
112     // throws ArtifactResolverException
113     // {
114     // try
115     // {
116     // String hint = isMaven31() ? "maven31" : "maven3";
117     //
118     // ArtifactResolver effectiveArtifactResolver = container.lookup( ArtifactResolver.class, hint );
119     //
120     // return effectiveArtifactResolver.resolveDependencies( buildingRequest, coordinates, null, filter );
121     // }
122     // catch ( ComponentLookupException e )
123     // {
124     // throw new ArtifactResolverException( e.getMessage(), e );
125     // }
126     // }
127     //
128     // @Override
129     // public Iterable<ArtifactResult> resolveDependencies( ProjectBuildingRequest buildingRequest,
130     // ArtifactCoordinate coordinate, TransformableFilter filter )
131     // throws ArtifactResolverException
132     // {
133     // try
134     // {
135     // String hint = isMaven31() ? "maven31" : "maven3";
136     //
137     // ArtifactResolver effectiveArtifactResolver = container.lookup( ArtifactResolver.class, hint );
138     //
139     // return effectiveArtifactResolver.resolveDependencies( buildingRequest, coordinate, filter );
140     // }
141     // catch ( ComponentLookupException e )
142     // {
143     // throw new ArtifactResolverException( e.getMessage(), e );
144     // }
145     // }
146 
147     /**
148      * @return true if the current Maven version is Maven 3.1.
149      */
150     protected static boolean isMaven31()
151     {
152         return canFindCoreClass( "org.eclipse.aether.artifact.Artifact" ); // Maven 3.1 specific
153     }
154 
155     private static boolean canFindCoreClass( String className )
156     {
157         try
158         {
159             Thread.currentThread().getContextClassLoader().loadClass( className );
160 
161             return true;
162         }
163         catch ( ClassNotFoundException e )
164         {
165             return false;
166         }
167     }
168 
169     /**
170      * Injects the Plexus content.
171      *
172      * @param context Plexus context to inject.
173      * @throws ContextException if the PlexusContainer could not be located.
174      */
175     public void contextualize( Context context )
176         throws ContextException
177     {
178         container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY );
179     }
180 }