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.plugin.internal;
20  
21  import javax.inject.Inject;
22  import javax.inject.Named;
23  import javax.inject.Singleton;
24  
25  import java.util.LinkedHashMap;
26  import java.util.List;
27  import java.util.Map;
28  
29  import org.apache.maven.RepositoryUtils;
30  import org.apache.maven.api.DependencyScope;
31  import org.apache.maven.model.Dependency;
32  import org.apache.maven.model.Plugin;
33  import org.apache.maven.plugin.PluginResolutionException;
34  import org.eclipse.aether.DefaultRepositorySystemSession;
35  import org.eclipse.aether.RepositorySystem;
36  import org.eclipse.aether.RepositorySystemSession;
37  import org.eclipse.aether.RequestTrace;
38  import org.eclipse.aether.artifact.Artifact;
39  import org.eclipse.aether.artifact.DefaultArtifact;
40  import org.eclipse.aether.collection.CollectRequest;
41  import org.eclipse.aether.collection.DependencyCollectionException;
42  import org.eclipse.aether.graph.DependencyFilter;
43  import org.eclipse.aether.graph.DependencyNode;
44  import org.eclipse.aether.repository.RemoteRepository;
45  import org.eclipse.aether.resolution.ArtifactDescriptorException;
46  import org.eclipse.aether.resolution.ArtifactDescriptorRequest;
47  import org.eclipse.aether.resolution.ArtifactDescriptorResult;
48  import org.eclipse.aether.resolution.ArtifactRequest;
49  import org.eclipse.aether.resolution.ArtifactResolutionException;
50  import org.eclipse.aether.resolution.DependencyRequest;
51  import org.eclipse.aether.resolution.DependencyResolutionException;
52  import org.eclipse.aether.resolution.DependencyResult;
53  import org.eclipse.aether.util.filter.AndDependencyFilter;
54  import org.eclipse.aether.util.filter.ScopeDependencyFilter;
55  import org.eclipse.aether.util.graph.visitor.DependencyGraphDumper;
56  import org.eclipse.aether.util.repository.SimpleArtifactDescriptorPolicy;
57  import org.slf4j.Logger;
58  import org.slf4j.LoggerFactory;
59  
60  /**
61   * Assists in resolving the dependencies of a plugin. <strong>Warning:</strong> This is an internal utility class that
62   * is only public for technical reasons, it is not part of the public API. In particular, this class can be changed or
63   * deleted without prior notice.
64   *
65   * @since 3.0
66   */
67  @Named
68  @Singleton
69  public class DefaultPluginDependenciesResolver implements PluginDependenciesResolver {
70      private static final String REPOSITORY_CONTEXT = "plugin";
71  
72      private final Logger logger = LoggerFactory.getLogger(getClass());
73  
74      private final RepositorySystem repoSystem;
75  
76      private final List<MavenPluginDependenciesValidator> dependenciesValidators;
77  
78      @Inject
79      public DefaultPluginDependenciesResolver(
80              RepositorySystem repoSystem, List<MavenPluginDependenciesValidator> dependenciesValidators) {
81          this.repoSystem = repoSystem;
82          this.dependenciesValidators = dependenciesValidators;
83      }
84  
85      private Artifact toArtifact(Plugin plugin, RepositorySystemSession session) {
86          return new DefaultArtifact(
87                  plugin.getGroupId(),
88                  plugin.getArtifactId(),
89                  null,
90                  "jar",
91                  plugin.getVersion(),
92                  session.getArtifactTypeRegistry().get("maven-plugin"));
93      }
94  
95      public Artifact resolve(Plugin plugin, List<RemoteRepository> repositories, RepositorySystemSession session)
96              throws PluginResolutionException {
97          RequestTrace trace = RequestTrace.newChild(null, plugin);
98  
99          Artifact pluginArtifact = toArtifact(plugin, session);
100 
101         try {
102             DefaultRepositorySystemSession pluginSession = new DefaultRepositorySystemSession(session);
103             pluginSession.setArtifactDescriptorPolicy(new SimpleArtifactDescriptorPolicy(true, false));
104 
105             ArtifactDescriptorRequest request =
106                     new ArtifactDescriptorRequest(pluginArtifact, repositories, REPOSITORY_CONTEXT);
107             request.setTrace(trace);
108             ArtifactDescriptorResult result = repoSystem.readArtifactDescriptor(pluginSession, request);
109 
110             for (MavenPluginDependenciesValidator dependenciesValidator : dependenciesValidators) {
111                 dependenciesValidator.validate(session, pluginArtifact, result);
112             }
113 
114             pluginArtifact = result.getArtifact();
115 
116             if (logger.isWarnEnabled() && !result.getRelocations().isEmpty()) {
117                 String message =
118                         pluginArtifact instanceof org.apache.maven.internal.impl.resolver.RelocatedArtifact relocated
119                                 ? ": " + relocated.getMessage()
120                                 : "";
121                 logger.warn(
122                         "The artifact {} has been relocated to {}{}",
123                         result.getRelocations().get(0),
124                         pluginArtifact,
125                         message);
126             }
127 
128             String requiredMavenVersion = (String) result.getProperties().get("prerequisites.maven");
129             if (requiredMavenVersion != null) {
130                 Map<String, String> props = new LinkedHashMap<>(pluginArtifact.getProperties());
131                 props.put("requiredMavenVersion", requiredMavenVersion);
132                 pluginArtifact = pluginArtifact.setProperties(props);
133             }
134         } catch (ArtifactDescriptorException e) {
135             throw new PluginResolutionException(plugin, e);
136         }
137 
138         try {
139             ArtifactRequest request = new ArtifactRequest(pluginArtifact, repositories, REPOSITORY_CONTEXT);
140             request.setTrace(trace);
141             pluginArtifact = repoSystem.resolveArtifact(session, request).getArtifact();
142         } catch (ArtifactResolutionException e) {
143             throw new PluginResolutionException(plugin, e);
144         }
145 
146         return pluginArtifact;
147     }
148 
149     /**
150      * @since 3.3.0
151      */
152     public DependencyResult resolveCoreExtension(
153             Plugin plugin,
154             DependencyFilter dependencyFilter,
155             List<RemoteRepository> repositories,
156             RepositorySystemSession session)
157             throws PluginResolutionException {
158         return resolveInternal(plugin, null /* pluginArtifact */, dependencyFilter, repositories, session);
159     }
160 
161     public DependencyResult resolvePlugin(
162             Plugin plugin,
163             Artifact pluginArtifact,
164             DependencyFilter dependencyFilter,
165             List<RemoteRepository> repositories,
166             RepositorySystemSession session)
167             throws PluginResolutionException {
168         return resolveInternal(plugin, pluginArtifact, dependencyFilter, repositories, session);
169     }
170 
171     public DependencyNode resolve(
172             Plugin plugin,
173             Artifact pluginArtifact,
174             DependencyFilter dependencyFilter,
175             List<RemoteRepository> repositories,
176             RepositorySystemSession session)
177             throws PluginResolutionException {
178         return resolveInternal(plugin, pluginArtifact, dependencyFilter, repositories, session)
179                 .getRoot();
180     }
181 
182     private DependencyResult resolveInternal(
183             Plugin plugin,
184             Artifact pluginArtifact,
185             DependencyFilter dependencyFilter,
186             List<RemoteRepository> repositories,
187             RepositorySystemSession session)
188             throws PluginResolutionException {
189         RequestTrace trace = RequestTrace.newChild(null, plugin);
190 
191         if (pluginArtifact == null) {
192             pluginArtifact = toArtifact(plugin, session);
193         }
194 
195         DependencyFilter collectionFilter = new ScopeDependencyFilter("provided", "test");
196         DependencyFilter resolutionFilter = AndDependencyFilter.newInstance(collectionFilter, dependencyFilter);
197 
198         DependencyNode node;
199 
200         try {
201             DefaultRepositorySystemSession pluginSession = new DefaultRepositorySystemSession(session);
202             pluginSession.setDependencySelector(session.getDependencySelector());
203             pluginSession.setDependencyGraphTransformer(session.getDependencyGraphTransformer());
204 
205             CollectRequest request = new CollectRequest();
206             request.setRequestContext(REPOSITORY_CONTEXT);
207             request.setRepositories(repositories);
208             request.setRoot(new org.eclipse.aether.graph.Dependency(pluginArtifact, null));
209             for (Dependency dependency : plugin.getDependencies()) {
210                 org.eclipse.aether.graph.Dependency pluginDep =
211                         RepositoryUtils.toDependency(dependency, session.getArtifactTypeRegistry());
212                 if (!DependencyScope.SYSTEM.is(pluginDep.getScope())) {
213                     pluginDep = pluginDep.setScope(DependencyScope.RUNTIME.id());
214                 }
215                 request.addDependency(pluginDep);
216             }
217 
218             DependencyRequest depRequest = new DependencyRequest(request, resolutionFilter);
219             depRequest.setTrace(trace);
220 
221             request.setTrace(RequestTrace.newChild(trace, depRequest));
222 
223             node = repoSystem.collectDependencies(pluginSession, request).getRoot();
224 
225             if (logger.isDebugEnabled()) {
226                 node.accept(new DependencyGraphDumper(logger::debug));
227             }
228 
229             depRequest.setRoot(node);
230             return repoSystem.resolveDependencies(session, depRequest);
231         } catch (DependencyCollectionException e) {
232             throw new PluginResolutionException(plugin, e);
233         } catch (DependencyResolutionException e) {
234             throw new PluginResolutionException(plugin, e.getCause());
235         }
236     }
237 }