View Javadoc
1   package org.apache.maven.shared.dependencies.collect.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.model.Dependency;
23  import org.apache.maven.model.Model;
24  import org.apache.maven.project.ProjectBuildingRequest;
25  import org.apache.maven.shared.dependencies.DependableCoordinate;
26  import org.apache.maven.shared.dependencies.collect.CollectorResult;
27  import org.apache.maven.shared.dependencies.collect.DependencyCollector;
28  import org.apache.maven.shared.dependencies.collect.DependencyCollectorException;
29  import org.codehaus.plexus.PlexusConstants;
30  import org.codehaus.plexus.PlexusContainer;
31  import org.codehaus.plexus.component.annotations.Component;
32  import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
33  import org.codehaus.plexus.context.Context;
34  import org.codehaus.plexus.context.ContextException;
35  import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
36  
37  /**
38   * This DependencyCollector passes the request to the proper Maven 3.x implementation
39   *  
40   * @author Robert Scholte
41   */
42  @Component( role = DependencyCollector.class, hint = "default" )
43  public class DefaultDependencyCollector implements DependencyCollector, Contextualizable 
44  {
45      private PlexusContainer container;
46     
47      @Override
48      public CollectorResult collectDependencies( ProjectBuildingRequest buildingRequest, Dependency root )
49          throws DependencyCollectorException
50      {
51          try
52          {
53              String hint = isMaven31() ? "maven31" : "maven3";
54  
55              DependencyCollector effectiveDependencyCollector = container.lookup( DependencyCollector.class, hint );
56  
57              return effectiveDependencyCollector.collectDependencies( buildingRequest, root );
58          }
59          catch ( ComponentLookupException e )
60          {
61              throw new DependencyCollectorException( e.getMessage(), e );
62          }
63      }
64      
65      @Override
66      public CollectorResult collectDependencies( ProjectBuildingRequest buildingRequest, DependableCoordinate root )
67          throws DependencyCollectorException
68      {
69          try
70          {
71              String hint = isMaven31() ? "maven31" : "maven3";
72  
73              DependencyCollector effectiveDependencyCollector = container.lookup( DependencyCollector.class, hint );
74  
75              return effectiveDependencyCollector.collectDependencies( buildingRequest, root );
76          }
77          catch ( ComponentLookupException e )
78          {
79              throw new DependencyCollectorException( e.getMessage(), e );
80          }
81      }
82      
83      @Override
84      public CollectorResult collectDependencies( ProjectBuildingRequest buildingRequest, Model root )
85          throws DependencyCollectorException
86      {
87          try
88          {
89              String hint = isMaven31() ? "maven31" : "maven3";
90  
91              DependencyCollector effectiveDependencyCollector = container.lookup( DependencyCollector.class, hint );
92  
93              return effectiveDependencyCollector.collectDependencies( buildingRequest, root );
94          }
95          catch ( ComponentLookupException e )
96          {
97              throw new DependencyCollectorException( e.getMessage(), e );
98          }
99      }
100 
101     /**
102      * @return true if the current Maven version is Maven 3.1.
103      */
104     protected static boolean isMaven31()
105     {
106         return canFindCoreClass( "org.eclipse.aether.artifact.Artifact" ); // Maven 3.1 specific
107     }
108 
109     private static boolean canFindCoreClass( String className )
110     {
111         try
112         {
113             Thread.currentThread().getContextClassLoader().loadClass( className );
114 
115             return true;
116         }
117         catch ( ClassNotFoundException e )
118         {
119             return false;
120         }
121     }
122 
123     /**
124      * Injects the Plexus content.
125      *
126      * @param context Plexus context to inject.
127      * @throws ContextException if the PlexusContainer could not be located.
128      */
129     public void contextualize( Context context )
130         throws ContextException
131     {
132         container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY );
133     }
134 
135 }