View Javadoc

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