View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.internal.impl;
20  
21  import javax.inject.Inject;
22  import javax.inject.Named;
23  import javax.inject.Singleton;
24  
25  import java.util.List;
26  
27  import org.apache.maven.api.Node;
28  import org.apache.maven.api.annotations.Nonnull;
29  import org.apache.maven.api.services.DependencyCollector;
30  import org.apache.maven.api.services.DependencyCollectorException;
31  import org.apache.maven.api.services.DependencyCollectorRequest;
32  import org.apache.maven.api.services.DependencyCollectorResult;
33  import org.eclipse.aether.DefaultRepositorySystemSession;
34  import org.eclipse.aether.RepositorySystem;
35  import org.eclipse.aether.RepositorySystemSession;
36  import org.eclipse.aether.artifact.Artifact;
37  import org.eclipse.aether.collection.CollectRequest;
38  import org.eclipse.aether.collection.CollectResult;
39  import org.eclipse.aether.collection.DependencyCollectionException;
40  import org.eclipse.aether.graph.Dependency;
41  import org.eclipse.aether.util.graph.manager.DependencyManagerUtils;
42  import org.eclipse.aether.util.graph.transformer.ConflictResolver;
43  
44  import static org.apache.maven.internal.impl.Utils.cast;
45  import static org.apache.maven.internal.impl.Utils.nonNull;
46  
47  @Named
48  @Singleton
49  public class DefaultDependencyCollector implements DependencyCollector {
50  
51      private final RepositorySystem repositorySystem;
52  
53      @Inject
54      DefaultDependencyCollector(@Nonnull RepositorySystem repositorySystem) {
55          this.repositorySystem = repositorySystem;
56      }
57  
58      @Nonnull
59      @Override
60      public DependencyCollectorResult collect(@Nonnull DependencyCollectorRequest request)
61              throws DependencyCollectorException, IllegalArgumentException {
62          nonNull(request, "request can not be null");
63          DefaultSession session =
64                  cast(DefaultSession.class, request.getSession(), "request.session should be a " + DefaultSession.class);
65  
66          Artifact rootArtifact =
67                  request.getRootArtifact().map(session::toArtifact).orElse(null);
68          Dependency root = request.getRoot().map(session::toDependency).orElse(null);
69          CollectRequest collectRequest = new CollectRequest()
70                  .setRootArtifact(rootArtifact)
71                  .setRoot(root)
72                  .setDependencies(session.toDependencies(request.getDependencies()))
73                  .setManagedDependencies(session.toDependencies(request.getManagedDependencies()))
74                  .setRepositories(session.toRepositories(session.getRemoteRepositories()));
75  
76          RepositorySystemSession systemSession = session.getSession();
77          if (request.getVerbose()) {
78              systemSession = new DefaultRepositorySystemSession(systemSession)
79                      .setConfigProperty(ConflictResolver.CONFIG_PROP_VERBOSE, true)
80                      .setConfigProperty(DependencyManagerUtils.CONFIG_PROP_VERBOSE, true);
81          }
82  
83          try {
84              final CollectResult result = repositorySystem.collectDependencies(systemSession, collectRequest);
85              return new DependencyCollectorResult() {
86                  @Override
87                  public List<Exception> getExceptions() {
88                      return result.getExceptions();
89                  }
90  
91                  @Override
92                  public Node getRoot() {
93                      return session.getNode(result.getRoot(), request.getVerbose());
94                  }
95              };
96          } catch (DependencyCollectionException e) {
97              throw new DependencyCollectorException("Unable to collect dependencies", e);
98          }
99      }
100 }