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