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