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 java.util.ArrayList;
23  import java.util.List;
24  
25  import org.apache.maven.RepositoryUtils;
26  import org.apache.maven.artifact.handler.ArtifactHandler;
27  import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
28  import org.apache.maven.model.Model;
29  import org.apache.maven.project.ProjectBuildingRequest;
30  import org.apache.maven.shared.dependencies.DependableCoordinate;
31  import org.apache.maven.shared.dependencies.collect.CollectorResult;
32  import org.apache.maven.shared.dependencies.collect.DependencyCollector;
33  import org.apache.maven.shared.dependencies.collect.DependencyCollectorException;
34  import org.codehaus.plexus.component.annotations.Component;
35  import org.codehaus.plexus.component.annotations.Requirement;
36  import org.eclipse.aether.RepositorySystem;
37  import org.eclipse.aether.RepositorySystemSession;
38  import org.eclipse.aether.artifact.Artifact;
39  import org.eclipse.aether.artifact.ArtifactTypeRegistry;
40  import org.eclipse.aether.artifact.DefaultArtifact;
41  import org.eclipse.aether.collection.CollectRequest;
42  import org.eclipse.aether.collection.DependencyCollectionException;
43  import org.eclipse.aether.graph.Dependency;
44  import org.eclipse.aether.repository.RemoteRepository;
45  
46  /**
47   * Maven 3.1+ implementation of the {@link DependencyCollector}
48   * 
49   * @author Robert Scholte
50   *
51   */
52  @Component( role = DependencyCollector.class, hint = "maven31" )
53  public class Maven31DependencyCollector
54      implements DependencyCollector
55  {
56      @Requirement
57      private RepositorySystem repositorySystem;
58  
59      @Requirement
60      private ArtifactHandlerManager artifactHandlerManager;
61  
62      @Override
63      public CollectorResult collectDependencies( final ProjectBuildingRequest buildingRequest,
64                                                  org.apache.maven.model.Dependency root )
65          throws DependencyCollectorException
66      {
67          ArtifactTypeRegistry typeRegistry =
68                          (ArtifactTypeRegistry) Invoker.invoke( RepositoryUtils.class, "newArtifactTypeRegistry",
69                                                                 ArtifactHandlerManager.class, artifactHandlerManager );
70  
71          CollectRequest request = new CollectRequest();
72          request.setRoot( toDependency( root, typeRegistry ) );
73  
74          return collectDependencies( buildingRequest, request );
75      }
76  
77      @Override
78      public CollectorResult collectDependencies( ProjectBuildingRequest buildingRequest, DependableCoordinate root )
79          throws DependencyCollectorException
80      {
81          ArtifactHandler artifactHandler = artifactHandlerManager.getArtifactHandler( root.getType() );
82          
83          String extension = artifactHandler != null ? artifactHandler.getExtension() : null;
84          
85          Artifact aetherArtifact = new DefaultArtifact( root.getGroupId(), root.getArtifactId(), root.getClassifier(),
86                                                         extension, root.getVersion() );
87          
88          CollectRequest request = new CollectRequest();
89          request.setRoot( new Dependency( aetherArtifact, null ) );
90  
91          return collectDependencies( buildingRequest, request );
92      }
93      
94      @Override
95      public CollectorResult collectDependencies( ProjectBuildingRequest buildingRequest, Model root )
96          throws DependencyCollectorException
97      {
98          // Are there examples where packaging and type are NOT in sync
99          ArtifactHandler artifactHandler = artifactHandlerManager.getArtifactHandler( root.getPackaging() );
100         
101         String extension = artifactHandler != null ? artifactHandler.getExtension() : null;
102         
103         Artifact aetherArtifact =
104             new DefaultArtifact( root.getGroupId(), root.getArtifactId(), extension, root.getVersion() );
105         
106         CollectRequest request = new CollectRequest();
107         request.setRoot( new Dependency( aetherArtifact, null ) );
108         
109         ArtifactTypeRegistry typeRegistry =
110                         (ArtifactTypeRegistry) Invoker.invoke( RepositoryUtils.class, "newArtifactTypeRegistry",
111                                                                ArtifactHandlerManager.class, artifactHandlerManager );
112 
113         List<Dependency> aetherDependencies = new ArrayList<Dependency>( root.getDependencies().size() );
114         for ( org.apache.maven.model.Dependency mavenDependency : root.getDependencies() )
115         {
116             aetherDependencies.add( toDependency( mavenDependency, typeRegistry ) );
117         }
118         request.setDependencies( aetherDependencies );
119 
120         if ( root.getDependencyManagement() != null )
121         {
122             List<Dependency> aetherManagerDependencies =
123                 new ArrayList<Dependency>( root.getDependencyManagement().getDependencies().size() );
124             
125             for ( org.apache.maven.model.Dependency mavenDependency : root.getDependencyManagement().getDependencies() )
126             {
127                 aetherManagerDependencies.add( toDependency( mavenDependency, typeRegistry ) );
128             }
129             
130             request.setManagedDependencies( aetherManagerDependencies );
131         }
132 
133         return collectDependencies( buildingRequest, request );
134     }
135 
136     private CollectorResult collectDependencies( ProjectBuildingRequest buildingRequest, CollectRequest request )
137         throws DependencyCollectorException
138     {
139         RepositorySystemSession session =
140             (RepositorySystemSession) Invoker.invoke( buildingRequest, "getRepositorySession" );
141 
142         @SuppressWarnings( "unchecked" )
143         List<RemoteRepository> aetherRepositories =
144             (List<RemoteRepository>) Invoker.invoke( RepositoryUtils.class, "toRepos", List.class,
145                                                      buildingRequest.getRemoteRepositories() );
146         request.setRepositories( aetherRepositories );
147 
148         try
149         {
150             return new Maven31CollectorResult( repositorySystem.collectDependencies( session, request ) );
151         }
152         catch ( DependencyCollectionException e )
153         {
154             throw new DependencyCollectorException( e.getMessage(), e );
155         }
156     }
157 
158     private static Dependency toDependency( org.apache.maven.model.Dependency root, ArtifactTypeRegistry typeRegistry )
159                     throws DependencyCollectorException
160     {
161         Class<?>[] argClasses = new Class<?>[] { org.apache.maven.model.Dependency.class, ArtifactTypeRegistry.class };
162 
163         Object[] args = new Object[] { root, typeRegistry };
164 
165         return (Dependency) Invoker.invoke( RepositoryUtils.class, "toDependency", argClasses, args );
166     }
167 }