1 package org.apache.maven.lifecycle.internal;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.execution.MavenSession;
23 import org.apache.maven.model.Plugin;
24 import org.apache.maven.plugin.BuildPluginManager;
25 import org.apache.maven.plugin.InvalidPluginDescriptorException;
26 import org.apache.maven.plugin.MojoNotFoundException;
27 import org.apache.maven.plugin.PluginDescriptorParsingException;
28 import org.apache.maven.plugin.PluginNotFoundException;
29 import org.apache.maven.plugin.PluginResolutionException;
30 import org.apache.maven.plugin.descriptor.MojoDescriptor;
31 import org.apache.maven.plugin.prefix.DefaultPluginPrefixRequest;
32 import org.apache.maven.plugin.prefix.NoPluginFoundForPrefixException;
33 import org.apache.maven.plugin.prefix.PluginPrefixRequest;
34 import org.apache.maven.plugin.prefix.PluginPrefixResolver;
35 import org.apache.maven.plugin.prefix.PluginPrefixResult;
36 import org.apache.maven.plugin.version.DefaultPluginVersionRequest;
37 import org.apache.maven.plugin.version.PluginVersionRequest;
38 import org.apache.maven.plugin.version.PluginVersionResolutionException;
39 import org.apache.maven.plugin.version.PluginVersionResolver;
40 import org.apache.maven.project.MavenProject;
41 import org.codehaus.plexus.component.annotations.Component;
42 import org.codehaus.plexus.component.annotations.Requirement;
43 import org.codehaus.plexus.configuration.PlexusConfiguration;
44 import org.codehaus.plexus.logging.Logger;
45 import org.codehaus.plexus.util.xml.Xpp3Dom;
46
47 import java.util.ArrayList;
48 import java.util.Collection;
49 import java.util.StringTokenizer;
50
51
52
53
54
55
56
57
58
59
60
61
62
63 @Component( role = MojoDescriptorCreator.class )
64 public class MojoDescriptorCreator
65 {
66
67 @Requirement
68 private Logger logger;
69
70 @Requirement
71 private PluginVersionResolver pluginVersionResolver;
72
73 @Requirement
74 private BuildPluginManager pluginManager;
75
76 @Requirement
77 private PluginPrefixResolver pluginPrefixResolver;
78
79 @Requirement
80 private LifecyclePluginResolver lifecyclePluginResolver;
81
82 public MojoDescriptorCreator()
83 {
84 }
85
86 public MojoDescriptorCreator( PluginVersionResolver pluginVersionResolver, BuildPluginManager pluginManager,
87 PluginPrefixResolver pluginPrefixResolver,
88 LifecyclePluginResolver lifecyclePluginResolver )
89 {
90 this.pluginVersionResolver = pluginVersionResolver;
91 this.pluginManager = pluginManager;
92 this.pluginPrefixResolver = pluginPrefixResolver;
93 this.lifecyclePluginResolver = lifecyclePluginResolver;
94 }
95
96 private Plugin findPlugin( String groupId, String artifactId, Collection<Plugin> plugins )
97 {
98 for ( Plugin plugin : plugins )
99 {
100 if ( artifactId.equals( plugin.getArtifactId() ) && groupId.equals( plugin.getGroupId() ) )
101 {
102 return plugin;
103 }
104 }
105
106 return null;
107 }
108
109 public static Xpp3Dom convert( MojoDescriptor mojoDescriptor )
110 {
111 Xpp3Dom dom = new Xpp3Dom( "configuration" );
112
113 PlexusConfiguration c = mojoDescriptor.getMojoConfiguration();
114
115 PlexusConfiguration[] ces = c.getChildren();
116
117 if ( ces != null )
118 {
119 for ( PlexusConfiguration ce : ces )
120 {
121 String value = ce.getValue( null );
122 String defaultValue = ce.getAttribute( "default-value", null );
123 if ( value != null || defaultValue != null )
124 {
125 Xpp3Dom e = new Xpp3Dom( ce.getName() );
126 e.setValue( value );
127 if ( defaultValue != null )
128 {
129 e.setAttribute( "default-value", defaultValue );
130 }
131 dom.addChild( e );
132 }
133 }
134 }
135
136 return dom;
137 }
138
139
140
141 public MojoDescriptor getMojoDescriptor( String task, MavenSession session, MavenProject project )
142 throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException,
143 MojoNotFoundException, NoPluginFoundForPrefixException, InvalidPluginDescriptorException,
144 PluginVersionResolutionException
145 {
146 String goal = null;
147
148 Plugin plugin = null;
149
150 StringTokenizer tok = new StringTokenizer( task, ":" );
151
152 int numTokens = tok.countTokens();
153
154 if ( numTokens >= 4 )
155 {
156
157
158
159
160
161
162
163
164
165 plugin = new Plugin();
166 plugin.setGroupId( tok.nextToken() );
167 plugin.setArtifactId( tok.nextToken() );
168 plugin.setVersion( tok.nextToken() );
169 goal = tok.nextToken();
170
171
172 while ( tok.hasMoreTokens() )
173 {
174 goal += ":" + tok.nextToken();
175 }
176 }
177 else if ( numTokens == 3 )
178 {
179
180
181
182
183
184
185
186
187
188 plugin = new Plugin();
189 plugin.setGroupId( tok.nextToken() );
190 plugin.setArtifactId( tok.nextToken() );
191 goal = tok.nextToken();
192 }
193 else if ( numTokens <= 2 )
194 {
195
196
197
198
199 String prefix = tok.nextToken();
200
201 if ( numTokens == 2 )
202 {
203 goal = tok.nextToken();
204 }
205 else
206 {
207
208 goal = "";
209 }
210
211
212
213
214
215
216
217
218
219
220 plugin = findPluginForPrefix( prefix, session );
221 }
222
223 int executionIdx = goal.indexOf( '@' );
224 if ( executionIdx > 0 )
225 {
226 goal = goal.substring( 0, executionIdx );
227 }
228
229 injectPluginDeclarationFromProject( plugin, project );
230
231
232
233
234 if ( plugin.getVersion() == null )
235 {
236 resolvePluginVersion( plugin, session, project );
237 }
238
239 return pluginManager.getMojoDescriptor( plugin, goal, project.getRemotePluginRepositories(),
240 session.getRepositorySession() );
241 }
242
243
244
245
246
247 public Plugin findPluginForPrefix( String prefix, MavenSession session )
248 throws NoPluginFoundForPrefixException
249 {
250
251
252 if ( session.getCurrentProject() != null )
253 {
254 try
255 {
256 lifecyclePluginResolver.resolveMissingPluginVersions( session.getCurrentProject(), session );
257 }
258 catch ( PluginVersionResolutionException e )
259 {
260
261 logger.debug( e.getMessage(), e );
262 }
263 }
264
265 PluginPrefixRequest prefixRequest = new DefaultPluginPrefixRequest( prefix, session );
266 PluginPrefixResult prefixResult = pluginPrefixResolver.resolve( prefixRequest );
267
268 Plugin plugin = new Plugin();
269 plugin.setGroupId( prefixResult.getGroupId() );
270 plugin.setArtifactId( prefixResult.getArtifactId() );
271
272 return plugin;
273 }
274
275 private void resolvePluginVersion( Plugin plugin, MavenSession session, MavenProject project )
276 throws PluginVersionResolutionException
277 {
278 PluginVersionRequest versionRequest =
279 new DefaultPluginVersionRequest( plugin, session.getRepositorySession(),
280 project.getRemotePluginRepositories() );
281 plugin.setVersion( pluginVersionResolver.resolve( versionRequest ).getVersion() );
282 }
283
284 private void injectPluginDeclarationFromProject( Plugin plugin, MavenProject project )
285 {
286 Plugin pluginInPom = findPlugin( plugin, project.getBuildPlugins() );
287
288 if ( pluginInPom == null && project.getPluginManagement() != null )
289 {
290 pluginInPom = findPlugin( plugin, project.getPluginManagement().getPlugins() );
291 }
292
293 if ( pluginInPom != null )
294 {
295 if ( plugin.getVersion() == null )
296 {
297 plugin.setVersion( pluginInPom.getVersion() );
298 }
299
300 plugin.setDependencies( new ArrayList<>( pluginInPom.getDependencies() ) );
301 }
302 }
303
304 private Plugin findPlugin( Plugin plugin, Collection<Plugin> plugins )
305 {
306 return findPlugin( plugin.getGroupId(), plugin.getArtifactId(), plugins );
307 }
308
309 }