View Javadoc
1   package org.apache.maven.internal.impl;
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.api.annotations.Nonnull;
23  import javax.inject.Inject;
24  import javax.inject.Named;
25  import javax.inject.Singleton;
26  
27  import java.util.List;
28  
29  import org.apache.maven.api.Node;
30  import org.apache.maven.api.services.DependencyCollector;
31  import org.apache.maven.api.services.DependencyCollectorException;
32  import org.apache.maven.api.services.DependencyCollectorRequest;
33  import org.apache.maven.api.services.DependencyCollectorResult;
34  import org.eclipse.aether.DefaultRepositorySystemSession;
35  import org.eclipse.aether.RepositorySystem;
36  import org.eclipse.aether.RepositorySystemSession;
37  import org.eclipse.aether.artifact.Artifact;
38  import org.eclipse.aether.collection.CollectRequest;
39  import org.eclipse.aether.collection.CollectResult;
40  import org.eclipse.aether.collection.DependencyCollectionException;
41  import org.eclipse.aether.graph.Dependency;
42  import org.eclipse.aether.util.graph.manager.DependencyManagerUtils;
43  import org.eclipse.aether.util.graph.transformer.ConflictResolver;
44  
45  import static org.apache.maven.internal.impl.Utils.cast;
46  import static org.apache.maven.internal.impl.Utils.nonNull;
47  
48  @Named
49  @Singleton
50  public class DefaultDependencyCollector implements DependencyCollector
51  {
52  
53      private final RepositorySystem repositorySystem;
54  
55      @Inject
56      DefaultDependencyCollector( @Nonnull RepositorySystem repositorySystem )
57      {
58          this.repositorySystem = repositorySystem;
59      }
60  
61      @Nonnull
62      @Override
63      public DependencyCollectorResult collect( @Nonnull DependencyCollectorRequest request )
64              throws DependencyCollectorException, IllegalArgumentException
65      {
66          nonNull( request, "request can not be null" );
67          DefaultSession session = cast( DefaultSession.class, request.getSession(),
68                  "request.session should be a " + DefaultSession.class );
69  
70          Artifact rootArtifact = request.getRootArtifact().map( session::toArtifact ).orElse( null );
71          Dependency root = request.getRoot().map( session::toDependency ).orElse( null );
72          CollectRequest collectRequest = new CollectRequest()
73                  .setRootArtifact( rootArtifact )
74                  .setRoot( root )
75                  .setDependencies( session.toDependencies( request.getDependencies() ) )
76                  .setManagedDependencies( session.toDependencies( request.getManagedDependencies() ) )
77                  .setRepositories( session.toRepositories( session.getRemoteRepositories() ) );
78  
79          RepositorySystemSession systemSession = session.getSession();
80          if ( request.getVerbose() )
81          {
82              systemSession = new DefaultRepositorySystemSession( systemSession )
83                      .setConfigProperty( ConflictResolver.CONFIG_PROP_VERBOSE, true )
84                      .setConfigProperty( DependencyManagerUtils.CONFIG_PROP_VERBOSE, true );
85          }
86  
87          try
88          {
89              final CollectResult
90                      result = repositorySystem.collectDependencies( systemSession, collectRequest );
91              return new DependencyCollectorResult()
92              {
93                  @Override
94                  public List<Exception> getExceptions()
95                  {
96                      return result.getExceptions();
97                  }
98  
99                  @Override
100                 public Node getRoot()
101                 {
102                     return session.getNode( result.getRoot(), request.getVerbose() );
103                 }
104             };
105         }
106         catch ( DependencyCollectionException e )
107         {
108             throw new DependencyCollectorException( "Unable to collect dependencies", e );
109         }
110     }
111 
112 }