View Javadoc
1   package org.apache.maven.shared.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.artifact.ArtifactCoordinate;
25  import org.apache.maven.shared.artifact.resolve.ArtifactResolver;
26  import org.apache.maven.shared.artifact.resolve.ArtifactResolverException;
27  import org.apache.maven.shared.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  public 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
48      {
49          try
50          {
51              String hint = isMaven31() ? "maven31" : "maven3";
52  
53              ArtifactResolver effectiveArtifactResolver = container.lookup( ArtifactResolver.class, hint );
54  
55              return effectiveArtifactResolver.resolveArtifact( buildingRequest, 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
66      {
67          try
68          {
69              String hint = isMaven31() ? "maven31" : "maven3";
70  
71              ArtifactResolver effectiveArtifactResolver = container.lookup( ArtifactResolver.class, hint );
72  
73              return effectiveArtifactResolver.resolveArtifact( buildingRequest, coordinate );
74          }
75          catch ( ComponentLookupException e )
76          {
77              throw new ArtifactResolverException( e.getMessage(), e );
78          }
79      }
80  
81  //    @Override
82  //    public Iterable<ArtifactResult> resolveDependencies( ProjectBuildingRequest buildingRequest,
83  //                                                         Collection<Dependency> coordinates,
84  //                                                         Collection<Dependency> managedDependencies,
85  //                                                         TransformableFilter filter )
86  //                                                             throws ArtifactResolverException
87  //    {
88  //        try
89  //        {
90  //            String hint = isMaven31() ? "maven31" : "maven3";
91  //
92  //            ArtifactResolver effectiveArtifactResolver = container.lookup( ArtifactResolver.class, hint );
93  //
94  //            return effectiveArtifactResolver.resolveDependencies( buildingRequest, coordinates, null, filter );
95  //        }
96  //        catch ( ComponentLookupException e )
97  //        {
98  //            throw new ArtifactResolverException( e.getMessage(), e );
99  //        }
100 //    }
101 //
102 //    @Override
103 //    public Iterable<ArtifactResult> resolveDependencies( ProjectBuildingRequest buildingRequest,
104 //                                                         ArtifactCoordinate coordinate, TransformableFilter filter )
105 //                                                             throws ArtifactResolverException
106 //    {
107 //        try
108 //        {
109 //            String hint = isMaven31() ? "maven31" : "maven3";
110 //
111 //            ArtifactResolver effectiveArtifactResolver = container.lookup( ArtifactResolver.class, hint );
112 //
113 //            return effectiveArtifactResolver.resolveDependencies( buildingRequest, coordinate, filter );
114 //        }
115 //        catch ( ComponentLookupException e )
116 //        {
117 //            throw new ArtifactResolverException( e.getMessage(), e );
118 //        }
119 //    }
120 
121     /**
122      * @return true if the current Maven version is Maven 3.1.
123      */
124     protected static boolean isMaven31()
125     {
126         return canFindCoreClass( "org.eclipse.aether.artifact.Artifact" ); // Maven 3.1 specific
127     }
128 
129     private static boolean canFindCoreClass( String className )
130     {
131         try
132         {
133             Thread.currentThread().getContextClassLoader().loadClass( className );
134 
135             return true;
136         }
137         catch ( ClassNotFoundException e )
138         {
139             return false;
140         }
141     }
142 
143     /**
144      * Injects the Plexus content.
145      *
146      * @param context Plexus context to inject.
147      * @throws ContextException if the PlexusContainer could not be located.
148      */
149     public void contextualize( Context context )
150         throws ContextException
151     {
152         container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY );
153     }
154 }