View Javadoc
1   package org.apache.maven.shared.transfer.collection.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  import java.util.Objects;
24  
25  import org.apache.maven.RepositoryUtils;
26  import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
27  import org.apache.maven.model.Dependency;
28  import org.apache.maven.model.Model;
29  import org.apache.maven.project.ProjectBuildingRequest;
30  import org.apache.maven.shared.transfer.collection.CollectResult;
31  import org.apache.maven.shared.transfer.collection.DependencyCollectionException;
32  import org.apache.maven.shared.transfer.collection.DependencyCollector;
33  import org.apache.maven.shared.transfer.dependencies.DependableCoordinate;
34  import org.codehaus.plexus.PlexusConstants;
35  import org.codehaus.plexus.PlexusContainer;
36  import org.codehaus.plexus.component.annotations.Component;
37  import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
38  import org.codehaus.plexus.context.Context;
39  import org.codehaus.plexus.context.ContextException;
40  import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
41  
42  /**
43   * This DependencyCollector passes the request to the proper Maven 3.x implementation
44   *
45   * @author Robert Scholte
46   */
47  @Component( role = DependencyCollector.class, hint = "default" )
48  class DefaultDependencyCollector implements DependencyCollector, Contextualizable
49  {
50      private PlexusContainer container;
51  
52      @Override
53      public CollectResult collectDependencies( ProjectBuildingRequest buildingRequest, Dependency root )
54              throws DependencyCollectionException
55      {
56          validateParameters( buildingRequest, root );
57  
58          try
59          {
60              return getMavenDependencyCollector( buildingRequest ).collectDependencies( root );
61          }
62          catch ( ComponentLookupException e )
63          {
64              throw new DependencyCollectionException( e.getMessage(), e );
65          }
66      }
67  
68      @Override
69      public CollectResult collectDependencies( ProjectBuildingRequest buildingRequest, DependableCoordinate root )
70              throws DependencyCollectionException
71      {
72          validateParameters( buildingRequest, root );
73  
74          try
75          {
76              return getMavenDependencyCollector( buildingRequest ).collectDependencies( root );
77          }
78          catch ( ComponentLookupException e )
79          {
80              throw new DependencyCollectionException( e.getMessage(), e );
81          }
82      }
83  
84      @Override
85      public CollectResult collectDependencies( ProjectBuildingRequest buildingRequest, Model root )
86              throws DependencyCollectionException
87      {
88          validateParameters( buildingRequest, root );
89  
90          try
91          {
92              return getMavenDependencyCollector( buildingRequest ).collectDependencies( root );
93          }
94          catch ( ComponentLookupException e )
95          {
96              throw new DependencyCollectionException( e.getMessage(), e );
97          }
98      }
99  
100   private void validateParameters( ProjectBuildingRequest buildingRequest, DependableCoordinate root )
101   {
102     validateBuildingRequestAndRoot( buildingRequest, root );
103   }
104 
105   private void validateParameters( ProjectBuildingRequest buildingRequest, Dependency root )
106   {
107     validateBuildingRequestAndRoot( buildingRequest, root );
108   }
109 
110   private void validateParameters( ProjectBuildingRequest buildingRequest, Model root )
111   {
112     validateBuildingRequestAndRoot( buildingRequest, root );
113   }
114 
115   private void validateBuildingRequestAndRoot( ProjectBuildingRequest buildingRequest, Object root )
116   {
117     validateBuildingRequest( buildingRequest );
118     validateRoot( root );
119   }
120 
121   private void validateBuildingRequest( ProjectBuildingRequest buildingRequest )
122   {
123     Objects.requireNonNull( buildingRequest, "The parameter buildingRequest is not allowed to be null." );
124   }
125 
126   private void validateRoot( Object root )
127   {
128     Objects.requireNonNull( root, "The parameter root is not allowed to be null." );
129   }
130 
131   /**
132    * @return true if the current Maven version is Maven 3.1.
133    */
134   private boolean isMaven31()
135   {
136       try
137       {
138           // Maven 3.1 specific
139           Thread.currentThread().getContextClassLoader().loadClass( "org.eclipse.aether.artifact.Artifact" );
140           return true;
141       }
142       catch ( ClassNotFoundException e )
143       {
144           return false;
145       }
146   }
147 
148     /**
149      * Injects the Plexus content.
150      *
151      * @param context Plexus context to inject.
152      * @throws ContextException if the PlexusContainer could not be located.
153      */
154     public void contextualize( Context context ) throws ContextException
155     {
156         container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY );
157     }
158 
159     private MavenDependencyCollector getMavenDependencyCollector( ProjectBuildingRequest buildingRequest )
160             throws ComponentLookupException, DependencyCollectionException
161     {
162         ArtifactHandlerManager artifactHandlerManager = container.lookup( ArtifactHandlerManager.class );
163 
164         if ( isMaven31() )
165         {
166             org.eclipse.aether.RepositorySystem m31RepositorySystem = container.lookup(
167                     org.eclipse.aether.RepositorySystem.class );
168 
169             org.eclipse.aether.RepositorySystemSession session = Invoker.invoke( buildingRequest,
170                     "getRepositorySession" );
171 
172             List<org.eclipse.aether.repository.RemoteRepository> aetherRepositories = Invoker.invoke(
173                     RepositoryUtils.class, "toRepos", List.class, buildingRequest.getRemoteRepositories() );
174 
175             return new Maven31DependencyCollector( m31RepositorySystem, artifactHandlerManager, session,
176                     aetherRepositories );
177         }
178         else
179         {
180             org.sonatype.aether.RepositorySystem m30RepositorySystem = container.lookup(
181                     org.sonatype.aether.RepositorySystem.class );
182 
183             org.sonatype.aether.RepositorySystemSession session = Invoker.invoke( buildingRequest,
184                     "getRepositorySession" );
185 
186             List<org.sonatype.aether.repository.RemoteRepository> aetherRepositories = Invoker.invoke(
187                     RepositoryUtils.class, "toRepos", List.class, buildingRequest.getRemoteRepositories() );
188 
189             return new Maven30DependencyCollector( m30RepositorySystem, artifactHandlerManager, session,
190                     aetherRepositories );
191         }
192 
193     }
194 
195 }