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.lifecycle.internal;
20  
21  import javax.inject.Inject;
22  import javax.inject.Named;
23  import javax.inject.Singleton;
24  
25  import java.util.ArrayList;
26  import java.util.Collection;
27  import java.util.StringTokenizer;
28  
29  import org.apache.maven.execution.MavenSession;
30  import org.apache.maven.model.Plugin;
31  import org.apache.maven.plugin.BuildPluginManager;
32  import org.apache.maven.plugin.InvalidPluginDescriptorException;
33  import org.apache.maven.plugin.MojoNotFoundException;
34  import org.apache.maven.plugin.PluginDescriptorParsingException;
35  import org.apache.maven.plugin.PluginNotFoundException;
36  import org.apache.maven.plugin.PluginResolutionException;
37  import org.apache.maven.plugin.descriptor.MojoDescriptor;
38  import org.apache.maven.plugin.prefix.DefaultPluginPrefixRequest;
39  import org.apache.maven.plugin.prefix.NoPluginFoundForPrefixException;
40  import org.apache.maven.plugin.prefix.PluginPrefixRequest;
41  import org.apache.maven.plugin.prefix.PluginPrefixResolver;
42  import org.apache.maven.plugin.prefix.PluginPrefixResult;
43  import org.apache.maven.plugin.version.DefaultPluginVersionRequest;
44  import org.apache.maven.plugin.version.PluginVersionRequest;
45  import org.apache.maven.plugin.version.PluginVersionResolutionException;
46  import org.apache.maven.plugin.version.PluginVersionResolver;
47  import org.apache.maven.project.MavenProject;
48  import org.codehaus.plexus.configuration.PlexusConfiguration;
49  import org.codehaus.plexus.util.xml.Xpp3Dom;
50  import org.slf4j.Logger;
51  import org.slf4j.LoggerFactory;
52  
53  /**
54   * <p>
55   * Resolves dependencies for the artifacts in context of the lifecycle build
56   * </p>
57   * <strong>NOTE:</strong> This class is not part of any public api and can be changed or deleted without prior notice.
58   *
59   * @since 3.0
60   * @author Benjamin Bentmann
61   * @author Jason van Zyl
62   * @author jdcasey
63   * @author Kristian Rosenvold (extracted class only)
64   */
65  @Singleton
66  @Named
67  public class MojoDescriptorCreator {
68      private final Logger logger = LoggerFactory.getLogger(getClass());
69  
70      @Inject
71      private PluginVersionResolver pluginVersionResolver;
72  
73      @Inject
74      private BuildPluginManager pluginManager;
75  
76      @Inject
77      private PluginPrefixResolver pluginPrefixResolver;
78  
79      @Inject
80      private LifecyclePluginResolver lifecyclePluginResolver;
81  
82      public MojoDescriptorCreator() {}
83  
84      public MojoDescriptorCreator(
85              PluginVersionResolver pluginVersionResolver,
86              BuildPluginManager pluginManager,
87              PluginPrefixResolver pluginPrefixResolver,
88              LifecyclePluginResolver lifecyclePluginResolver) {
89          this.pluginVersionResolver = pluginVersionResolver;
90          this.pluginManager = pluginManager;
91          this.pluginPrefixResolver = pluginPrefixResolver;
92          this.lifecyclePluginResolver = lifecyclePluginResolver;
93      }
94  
95      private Plugin findPlugin(String groupId, String artifactId, Collection<Plugin> plugins) {
96          for (Plugin plugin : plugins) {
97              if (artifactId.equals(plugin.getArtifactId()) && groupId.equals(plugin.getGroupId())) {
98                  return plugin;
99              }
100         }
101 
102         return null;
103     }
104 
105     public static Xpp3Dom convert(MojoDescriptor mojoDescriptor) {
106         Xpp3Dom dom = new Xpp3Dom("configuration");
107 
108         PlexusConfiguration c = mojoDescriptor.getMojoConfiguration();
109 
110         PlexusConfiguration[] ces = c.getChildren();
111 
112         if (ces != null) {
113             for (PlexusConfiguration ce : ces) {
114                 String value = ce.getValue(null);
115                 String defaultValue = ce.getAttribute("default-value", null);
116                 if (value != null || defaultValue != null) {
117                     Xpp3Dom e = new Xpp3Dom(ce.getName());
118                     e.setValue(value);
119                     if (defaultValue != null) {
120                         e.setAttribute("default-value", defaultValue);
121                     }
122                     dom.addChild(e);
123                 }
124             }
125         }
126 
127         return dom;
128     }
129 
130     // org.apache.maven.plugins:maven-remote-resources-plugin:1.0:process@executionId
131 
132     public MojoDescriptor getMojoDescriptor(String task, MavenSession session, MavenProject project)
133             throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException,
134                     MojoNotFoundException, NoPluginFoundForPrefixException, InvalidPluginDescriptorException,
135                     PluginVersionResolutionException {
136         String goal = null;
137 
138         Plugin plugin = null;
139 
140         StringTokenizer tok = new StringTokenizer(task, ":");
141 
142         int numTokens = tok.countTokens();
143 
144         if (numTokens >= 4) {
145             // We have everything that we need
146             //
147             // org.apache.maven.plugins:maven-remote-resources-plugin:1.0:process
148             //
149             // groupId
150             // artifactId
151             // version
152             // goal
153             //
154             plugin = new Plugin();
155             plugin.setGroupId(tok.nextToken());
156             plugin.setArtifactId(tok.nextToken());
157             plugin.setVersion(tok.nextToken());
158             goal = tok.nextToken();
159 
160             // This won't be valid, but it constructs something easy to read in the error message
161             while (tok.hasMoreTokens()) {
162                 goal += ":" + tok.nextToken();
163             }
164         } else if (numTokens == 3) {
165             // groupId:artifactId:goal or pluginPrefix:version:goal (since Maven 3.9.0)
166 
167             String firstToken = tok.nextToken();
168             // groupId or pluginPrefix? heuristics: groupId contains dot (.) but not pluginPrefix
169             if (firstToken.contains(".")) {
170                 // We have everything that we need except the version
171                 //
172                 // org.apache.maven.plugins:maven-remote-resources-plugin:???:process
173                 //
174                 // groupId
175                 // artifactId
176                 // ???
177                 // goal
178                 //
179                 plugin = new Plugin();
180                 plugin.setGroupId(firstToken);
181                 plugin.setArtifactId(tok.nextToken());
182             } else {
183                 // pluginPrefix:version:goal, like remote-resources:3.5.0:process
184                 plugin = findPluginForPrefix(firstToken, session);
185                 plugin.setVersion(tok.nextToken());
186             }
187             goal = tok.nextToken();
188         } else if (numTokens <= 2) {
189             // We have a prefix and goal
190             //
191             // idea:idea
192             //
193             String prefix = tok.nextToken();
194 
195             if (numTokens == 2) {
196                 goal = tok.nextToken();
197             } else {
198                 // goal was missing - pass through to MojoNotFoundException
199                 goal = "";
200             }
201 
202             // This is the case where someone has executed a single goal from the command line
203             // of the form:
204             //
205             // mvn remote-resources:process
206             //
207             // From the metadata stored on the server which has been created as part of a standard
208             // Maven plugin deployment we will find the right PluginDescriptor from the remote
209             // repository.
210 
211             plugin = findPluginForPrefix(prefix, session);
212         }
213 
214         int executionIdx = goal.indexOf('@');
215         if (executionIdx > 0) {
216             goal = goal.substring(0, executionIdx);
217         }
218 
219         injectPluginDeclarationFromProject(plugin, project);
220 
221         // If there is no version to be found then we need to look in the repository metadata for
222         // this plugin and see what's specified as the latest release.
223         //
224         if (plugin.getVersion() == null) {
225             resolvePluginVersion(plugin, session, project);
226         }
227 
228         return pluginManager.getMojoDescriptor(
229                 plugin, goal, project.getRemotePluginRepositories(), session.getRepositorySession());
230     }
231 
232     // TODO take repo mans into account as one may be aggregating prefixes of many
233     // TODO collect at the root of the repository, read the one at the root, and fetch remote if something is missing
234     // or the user forces the issue
235 
236     public Plugin findPluginForPrefix(String prefix, MavenSession session) throws NoPluginFoundForPrefixException {
237         // [prefix]:[goal]
238 
239         if (session.getCurrentProject() != null) {
240             try {
241                 lifecyclePluginResolver.resolveMissingPluginVersions(session.getCurrentProject(), session);
242             } catch (PluginVersionResolutionException e) {
243                 // not critical here
244                 logger.debug(e.getMessage(), e);
245             }
246         }
247 
248         PluginPrefixRequest prefixRequest = new DefaultPluginPrefixRequest(prefix, session);
249         PluginPrefixResult prefixResult = pluginPrefixResolver.resolve(prefixRequest);
250 
251         Plugin plugin = new Plugin();
252         plugin.setGroupId(prefixResult.getGroupId());
253         plugin.setArtifactId(prefixResult.getArtifactId());
254 
255         return plugin;
256     }
257 
258     private void resolvePluginVersion(Plugin plugin, MavenSession session, MavenProject project)
259             throws PluginVersionResolutionException {
260         PluginVersionRequest versionRequest = new DefaultPluginVersionRequest(
261                 plugin, session.getRepositorySession(), project.getRemotePluginRepositories());
262         plugin.setVersion(pluginVersionResolver.resolve(versionRequest).getVersion());
263     }
264 
265     private void injectPluginDeclarationFromProject(Plugin plugin, MavenProject project) {
266         Plugin pluginInPom = findPlugin(plugin, project.getBuildPlugins());
267 
268         if (pluginInPom == null && project.getPluginManagement() != null) {
269             pluginInPom = findPlugin(plugin, project.getPluginManagement().getPlugins());
270         }
271 
272         if (pluginInPom != null) {
273             if (plugin.getVersion() == null) {
274                 plugin.setVersion(pluginInPom.getVersion());
275             }
276 
277             plugin.setDependencies(new ArrayList<>(pluginInPom.getDependencies()));
278         }
279     }
280 
281     private Plugin findPlugin(Plugin plugin, Collection<Plugin> plugins) {
282         return findPlugin(plugin.getGroupId(), plugin.getArtifactId(), plugins);
283     }
284 }