1 | |
package org.apache.maven.plugins.help; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import java.io.File; |
23 | |
import java.io.FileInputStream; |
24 | |
import java.io.IOException; |
25 | |
import java.io.InputStream; |
26 | |
import java.io.StringWriter; |
27 | |
import java.util.Iterator; |
28 | |
import java.util.List; |
29 | |
import java.util.Locale; |
30 | |
import java.util.Map; |
31 | |
import java.util.Properties; |
32 | |
import java.util.TreeMap; |
33 | |
import java.util.jar.JarEntry; |
34 | |
import java.util.jar.JarInputStream; |
35 | |
|
36 | |
import org.apache.commons.lang.ClassUtils; |
37 | |
import org.apache.maven.artifact.Artifact; |
38 | |
import org.apache.maven.artifact.ArtifactUtils; |
39 | |
import org.apache.maven.artifact.factory.ArtifactFactory; |
40 | |
import org.apache.maven.artifact.repository.ArtifactRepository; |
41 | |
import org.apache.maven.artifact.resolver.ArtifactNotFoundException; |
42 | |
import org.apache.maven.artifact.resolver.ArtifactResolutionException; |
43 | |
import org.apache.maven.artifact.resolver.ArtifactResolver; |
44 | |
import org.apache.maven.execution.MavenSession; |
45 | |
import org.apache.maven.model.Dependency; |
46 | |
import org.apache.maven.model.Model; |
47 | |
import org.apache.maven.model.io.xpp3.MavenXpp3Writer; |
48 | |
import org.apache.maven.plugin.AbstractMojo; |
49 | |
import org.apache.maven.plugin.MojoExecution; |
50 | |
import org.apache.maven.plugin.MojoExecutionException; |
51 | |
import org.apache.maven.plugin.MojoFailureException; |
52 | |
import org.apache.maven.plugin.PluginParameterExpressionEvaluator; |
53 | |
import org.apache.maven.plugin.descriptor.MojoDescriptor; |
54 | |
import org.apache.maven.project.MavenProject; |
55 | |
import org.apache.maven.project.MavenProjectBuilder; |
56 | |
import org.apache.maven.project.ProjectBuildingException; |
57 | |
import org.apache.maven.project.path.PathTranslator; |
58 | |
import org.apache.maven.settings.Settings; |
59 | |
import org.apache.maven.settings.io.xpp3.SettingsXpp3Writer; |
60 | |
import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException; |
61 | |
import org.codehaus.plexus.components.interactivity.InputHandler; |
62 | |
import org.codehaus.plexus.util.IOUtil; |
63 | |
import org.codehaus.plexus.util.StringUtils; |
64 | |
|
65 | |
import com.thoughtworks.xstream.XStream; |
66 | |
import com.thoughtworks.xstream.converters.MarshallingContext; |
67 | |
import com.thoughtworks.xstream.converters.collections.PropertiesConverter; |
68 | |
import com.thoughtworks.xstream.io.HierarchicalStreamWriter; |
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | 1 | public class EvaluateMojo |
80 | |
extends AbstractMojo |
81 | |
{ |
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
private ArtifactFactory artifactFactory; |
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
private InputHandler inputHandler; |
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
private MavenProjectBuilder mavenProjectBuilder; |
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
private PathTranslator pathTranslator; |
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
private ArtifactResolver resolver; |
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
private String artifact; |
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
protected ArtifactRepository localRepository; |
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
protected MavenProject project; |
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
private List remoteRepositories; |
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
protected Settings settings; |
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
private MavenSession session; |
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
private PluginParameterExpressionEvaluator evaluator; |
183 | |
|
184 | |
|
185 | |
private XStream xstream; |
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
public void execute() |
193 | |
throws MojoExecutionException, MojoFailureException |
194 | |
{ |
195 | 0 | if ( !settings.isInteractiveMode() ) |
196 | |
{ |
197 | 0 | StringBuffer msg = new StringBuffer(); |
198 | 0 | msg.append( "Maven is configured to NOT interact with the user for input. " ); |
199 | 0 | msg.append( "This Mojo requires that 'interactiveMode' in your settings file is flag to 'true'." ); |
200 | |
|
201 | 0 | getLog().error( msg.toString() ); |
202 | 0 | return; |
203 | |
} |
204 | |
|
205 | 0 | validateParameters(); |
206 | |
|
207 | 0 | if ( StringUtils.isNotEmpty( artifact ) ) |
208 | |
{ |
209 | 0 | Artifact artifactObj = getArtifact( artifact ); |
210 | |
|
211 | |
try |
212 | |
{ |
213 | 0 | project = getMavenProject( artifactObj ); |
214 | |
} |
215 | 0 | catch ( ProjectBuildingException e ) |
216 | |
{ |
217 | 0 | throw new MojoExecutionException( "Unable to get the POM for the artifact '" + artifact |
218 | |
+ "'. Verify the artifact parameter." ); |
219 | 0 | } |
220 | |
} |
221 | |
|
222 | |
while ( true ) |
223 | |
{ |
224 | 0 | getLog().info( "Enter the Maven expression i.e. ${project.groupId} or 0 to exit?:" ); |
225 | |
|
226 | |
try |
227 | |
{ |
228 | 0 | String userExpression = inputHandler.readLine(); |
229 | 0 | if ( userExpression == null || userExpression.toLowerCase( Locale.ENGLISH ).equals( "0" ) ) |
230 | |
{ |
231 | 0 | break; |
232 | |
} |
233 | |
|
234 | 0 | handleResponse( userExpression ); |
235 | |
} |
236 | 0 | catch ( IOException e ) |
237 | |
{ |
238 | 0 | throw new MojoExecutionException( "Unable to read from standard input.", e ); |
239 | 0 | } |
240 | |
} |
241 | 0 | } |
242 | |
|
243 | |
|
244 | |
|
245 | |
|
246 | |
|
247 | |
|
248 | |
|
249 | |
|
250 | |
private void validateParameters() |
251 | |
{ |
252 | 0 | if ( artifact == null ) |
253 | |
{ |
254 | |
|
255 | 0 | getLog().info( "No artifact parameter specified, using '" + project.getId() + "' as project." ); |
256 | |
} |
257 | 0 | } |
258 | |
|
259 | |
|
260 | |
|
261 | |
|
262 | |
|
263 | |
|
264 | |
private Artifact getArtifact( String artifactString ) |
265 | |
throws MojoExecutionException |
266 | |
{ |
267 | 0 | if ( StringUtils.isEmpty( artifactString ) ) |
268 | |
{ |
269 | 0 | throw new IllegalArgumentException( "artifact parameter could not be empty" ); |
270 | |
} |
271 | |
|
272 | 0 | String groupId = null; |
273 | 0 | String artifactId = null; |
274 | 0 | String version = null; |
275 | 0 | String classifier = null; |
276 | |
|
277 | 0 | String[] artifactParts = artifactString.split( ":" ); |
278 | |
|
279 | 0 | switch ( artifactParts.length ) |
280 | |
{ |
281 | |
case ( 2 ): |
282 | 0 | groupId = artifactParts[0]; |
283 | 0 | artifactId = artifactParts[1]; |
284 | 0 | version = Artifact.LATEST_VERSION; |
285 | 0 | break; |
286 | |
case ( 3 ): |
287 | 0 | groupId = artifactParts[0]; |
288 | 0 | artifactId = artifactParts[1]; |
289 | 0 | version = artifactParts[2]; |
290 | 0 | break; |
291 | |
case ( 4 ): |
292 | 0 | groupId = artifactParts[0]; |
293 | 0 | artifactId = artifactParts[1]; |
294 | 0 | version = artifactParts[2]; |
295 | 0 | classifier = artifactParts[3]; |
296 | 0 | break; |
297 | |
default: |
298 | 0 | throw new MojoExecutionException( "The artifact parameter '" + artifactString |
299 | |
+ "' should be conform to: " + "'groupId:artifactId[:version][:classifier]'." ); |
300 | |
} |
301 | |
|
302 | 0 | if ( StringUtils.isNotEmpty( classifier ) ) |
303 | |
{ |
304 | 0 | return artifactFactory.createArtifactWithClassifier( groupId, artifactId, version, "jar", classifier ); |
305 | |
} |
306 | |
|
307 | 0 | return artifactFactory.createArtifact( groupId, artifactId, version, Artifact.SCOPE_COMPILE, "jar" ); |
308 | |
} |
309 | |
|
310 | |
|
311 | |
|
312 | |
|
313 | |
|
314 | |
|
315 | |
|
316 | |
private MavenProject getMavenProject( Artifact artifactObj ) |
317 | |
throws MojoExecutionException, ProjectBuildingException |
318 | |
{ |
319 | 0 | if ( Artifact.SCOPE_SYSTEM.equals( artifactObj.getScope() ) ) |
320 | |
{ |
321 | 0 | throw new MojoExecutionException( "System artifact is not be handled." ); |
322 | |
} |
323 | |
|
324 | 0 | Artifact copyArtifact = ArtifactUtils.copyArtifact( artifactObj ); |
325 | 0 | if ( !"pom".equals( copyArtifact.getType() ) ) |
326 | |
{ |
327 | 0 | copyArtifact = |
328 | |
artifactFactory.createProjectArtifact( copyArtifact.getGroupId(), copyArtifact.getArtifactId(), |
329 | |
copyArtifact.getVersion(), copyArtifact.getScope() ); |
330 | |
} |
331 | |
|
332 | 0 | return mavenProjectBuilder.buildFromRepository( copyArtifact, remoteRepositories, localRepository ); |
333 | |
} |
334 | |
|
335 | |
|
336 | |
|
337 | |
|
338 | |
|
339 | |
|
340 | |
|
341 | |
private PluginParameterExpressionEvaluator getEvaluator() |
342 | |
throws MojoExecutionException, MojoFailureException |
343 | |
{ |
344 | 0 | if ( evaluator == null ) |
345 | |
{ |
346 | 0 | MojoDescriptor mojoDescriptor = |
347 | |
HelpUtil.getMojoDescriptor( "help:evaluate", session, project, "help:evaluate", true, false ); |
348 | 0 | MojoExecution mojoExecution = new MojoExecution( mojoDescriptor ); |
349 | 0 | evaluator = |
350 | |
new PluginParameterExpressionEvaluator( session, mojoExecution, pathTranslator, |
351 | |
session.getContainer().getLogger(), project, |
352 | |
session.getExecutionProperties() ); |
353 | |
} |
354 | |
|
355 | 0 | return evaluator; |
356 | |
} |
357 | |
|
358 | |
|
359 | |
|
360 | |
|
361 | |
|
362 | |
|
363 | |
private void handleResponse( String expression ) |
364 | |
throws MojoExecutionException, MojoFailureException |
365 | |
{ |
366 | 0 | StringBuffer response = new StringBuffer(); |
367 | |
|
368 | |
Object obj; |
369 | |
try |
370 | |
{ |
371 | 0 | obj = getEvaluator().evaluate( expression ); |
372 | |
} |
373 | 0 | catch ( ExpressionEvaluationException e ) |
374 | |
{ |
375 | 0 | throw new MojoExecutionException( "Error when evaluating the Maven expression", e ); |
376 | 0 | } |
377 | |
|
378 | 0 | if ( obj != null && expression.equals( obj.toString() ) ) |
379 | |
{ |
380 | 0 | getLog().warn( "The Maven expression was invalid. Please use a valid expression." ); |
381 | 0 | return; |
382 | |
} |
383 | |
|
384 | |
|
385 | 0 | if ( obj == null ) |
386 | |
{ |
387 | 0 | response.append( "null object or invalid expression" ); |
388 | |
} |
389 | |
|
390 | 0 | else if ( obj instanceof String ) |
391 | |
{ |
392 | 0 | response.append( obj.toString() ); |
393 | |
} |
394 | 0 | else if ( obj instanceof Boolean ) |
395 | |
{ |
396 | 0 | response.append( obj.toString() ); |
397 | |
} |
398 | 0 | else if ( obj instanceof Byte ) |
399 | |
{ |
400 | 0 | response.append( obj.toString() ); |
401 | |
} |
402 | 0 | else if ( obj instanceof Character ) |
403 | |
{ |
404 | 0 | response.append( obj.toString() ); |
405 | |
} |
406 | 0 | else if ( obj instanceof Double ) |
407 | |
{ |
408 | 0 | response.append( obj.toString() ); |
409 | |
} |
410 | 0 | else if ( obj instanceof Float ) |
411 | |
{ |
412 | 0 | response.append( obj.toString() ); |
413 | |
} |
414 | 0 | else if ( obj instanceof Integer ) |
415 | |
{ |
416 | 0 | response.append( obj.toString() ); |
417 | |
} |
418 | 0 | else if ( obj instanceof Long ) |
419 | |
{ |
420 | 0 | response.append( obj.toString() ); |
421 | |
} |
422 | 0 | else if ( obj instanceof Short ) |
423 | |
{ |
424 | 0 | response.append( obj.toString() ); |
425 | |
} |
426 | |
|
427 | 0 | else if ( obj instanceof File ) |
428 | |
{ |
429 | 0 | File f = (File) obj; |
430 | 0 | response.append( f.getAbsolutePath() ); |
431 | 0 | } |
432 | |
|
433 | 0 | else if ( obj instanceof MavenProject ) |
434 | |
{ |
435 | 0 | MavenProject projectAsked = (MavenProject) obj; |
436 | 0 | StringWriter sWriter = new StringWriter(); |
437 | 0 | MavenXpp3Writer pomWriter = new MavenXpp3Writer(); |
438 | |
try |
439 | |
{ |
440 | 0 | pomWriter.write( sWriter, projectAsked.getModel() ); |
441 | |
} |
442 | 0 | catch ( IOException e ) |
443 | |
{ |
444 | 0 | throw new MojoExecutionException( "Error when writing pom", e ); |
445 | 0 | } |
446 | |
|
447 | 0 | response.append( sWriter.toString() ); |
448 | 0 | } |
449 | |
|
450 | 0 | else if ( obj instanceof Settings ) |
451 | |
{ |
452 | 0 | Settings settingsAsked = (Settings) obj; |
453 | 0 | StringWriter sWriter = new StringWriter(); |
454 | 0 | SettingsXpp3Writer settingsWriter = new SettingsXpp3Writer(); |
455 | |
try |
456 | |
{ |
457 | 0 | settingsWriter.write( sWriter, settingsAsked ); |
458 | |
} |
459 | 0 | catch ( IOException e ) |
460 | |
{ |
461 | 0 | throw new MojoExecutionException( "Error when writing settings", e ); |
462 | 0 | } |
463 | |
|
464 | 0 | response.append( sWriter.toString() ); |
465 | 0 | } |
466 | |
else |
467 | |
{ |
468 | |
|
469 | 0 | response.append( toXML( expression, obj ) ); |
470 | |
} |
471 | |
|
472 | 0 | getLog().info( "\n" + response.toString() ); |
473 | 0 | } |
474 | |
|
475 | |
|
476 | |
|
477 | |
|
478 | |
|
479 | |
|
480 | |
private String toXML( String expression, Object obj ) |
481 | |
{ |
482 | 0 | XStream currentXStream = getXStream(); |
483 | |
|
484 | |
|
485 | 0 | if ( obj instanceof List ) |
486 | |
{ |
487 | 0 | List list = (List) obj; |
488 | 0 | if ( list.size() > 0 ) |
489 | |
{ |
490 | 0 | Object elt = list.iterator().next(); |
491 | |
|
492 | 0 | String name = StringUtils.lowercaseFirstLetter( ClassUtils.getShortClassName( elt.getClass() ) ); |
493 | 0 | currentXStream.alias( pluralize( name ), List.class ); |
494 | 0 | } |
495 | |
else |
496 | |
{ |
497 | |
|
498 | 0 | if ( expression.indexOf( "." ) != -1 ) |
499 | |
{ |
500 | 0 | String name = expression.substring( expression.indexOf( "." ) + 1, expression.indexOf( "}" ) ); |
501 | 0 | currentXStream.alias( name, List.class ); |
502 | |
} |
503 | |
} |
504 | |
} |
505 | |
|
506 | 0 | return currentXStream.toXML( obj ); |
507 | |
} |
508 | |
|
509 | |
|
510 | |
|
511 | |
|
512 | |
private XStream getXStream() |
513 | |
{ |
514 | 0 | if ( xstream == null ) |
515 | |
{ |
516 | 0 | xstream = new XStream(); |
517 | 0 | addAlias( xstream ); |
518 | |
|
519 | |
|
520 | 0 | xstream.registerConverter( new PropertiesConverter() |
521 | |
{ |
522 | |
|
523 | |
public boolean canConvert( Class type ) |
524 | |
{ |
525 | 0 | return Properties.class == type; |
526 | |
} |
527 | |
|
528 | |
|
529 | 0 | public void marshal( Object source, HierarchicalStreamWriter writer, MarshallingContext context ) |
530 | |
{ |
531 | 0 | Properties properties = (Properties) source; |
532 | 0 | Map map = new TreeMap( properties ); |
533 | 0 | for ( Iterator iterator = map.entrySet().iterator(); iterator.hasNext(); ) |
534 | |
{ |
535 | 0 | Map.Entry entry = (Map.Entry) iterator.next(); |
536 | |
|
537 | 0 | writer.startNode( entry.getKey().toString() ); |
538 | 0 | writer.setValue( entry.getValue().toString() ); |
539 | 0 | writer.endNode(); |
540 | 0 | } |
541 | 0 | } |
542 | |
} ); |
543 | |
} |
544 | |
|
545 | 0 | return xstream; |
546 | |
} |
547 | |
|
548 | |
|
549 | |
|
550 | |
|
551 | |
private void addAlias( XStream xstreamObject ) |
552 | |
{ |
553 | |
try |
554 | |
{ |
555 | 0 | addAlias( xstreamObject, getMavenModelJarFile(), "org.apache.maven.model" ); |
556 | 0 | addAlias( xstreamObject, getMavenSettingsJarFile(), "org.apache.maven.settings" ); |
557 | |
} |
558 | 0 | catch ( MojoExecutionException e ) |
559 | |
{ |
560 | 0 | if ( getLog().isDebugEnabled() ) |
561 | |
{ |
562 | 0 | getLog().debug( "MojoExecutionException: " + e.getMessage(), e ); |
563 | |
} |
564 | |
} |
565 | 0 | catch ( ArtifactResolutionException e ) |
566 | |
{ |
567 | 0 | if ( getLog().isDebugEnabled() ) |
568 | |
{ |
569 | 0 | getLog().debug( "ArtifactResolutionException: " + e.getMessage(), e ); |
570 | |
} |
571 | |
} |
572 | 0 | catch ( ArtifactNotFoundException e ) |
573 | |
{ |
574 | 0 | if ( getLog().isDebugEnabled() ) |
575 | |
{ |
576 | 0 | getLog().debug( "ArtifactNotFoundException: " + e.getMessage(), e ); |
577 | |
} |
578 | |
} |
579 | 0 | catch ( ProjectBuildingException e ) |
580 | |
{ |
581 | 0 | if ( getLog().isDebugEnabled() ) |
582 | |
{ |
583 | 0 | getLog().debug( "ProjectBuildingException: " + e.getMessage(), e ); |
584 | |
} |
585 | 0 | } |
586 | |
|
587 | |
|
588 | 0 | } |
589 | |
|
590 | |
|
591 | |
|
592 | |
|
593 | |
|
594 | |
|
595 | |
private void addAlias( XStream xstreamObject, File jarFile, String packageFilter ) |
596 | |
{ |
597 | 0 | JarInputStream jarStream = null; |
598 | |
try |
599 | |
{ |
600 | 0 | jarStream = new JarInputStream( new FileInputStream( jarFile ) ); |
601 | 0 | JarEntry jarEntry = jarStream.getNextJarEntry(); |
602 | 0 | while ( jarEntry != null ) |
603 | |
{ |
604 | 0 | if ( jarEntry == null ) |
605 | |
{ |
606 | 0 | break; |
607 | |
} |
608 | |
|
609 | 0 | if ( jarEntry.getName().toLowerCase( Locale.ENGLISH ).endsWith( ".class" ) ) |
610 | |
{ |
611 | 0 | String name = jarEntry.getName().substring( 0, jarEntry.getName().indexOf( "." ) ); |
612 | 0 | name = name.replaceAll( "/", "\\." ); |
613 | |
|
614 | 0 | if ( name.indexOf( packageFilter ) != -1 ) |
615 | |
{ |
616 | |
try |
617 | |
{ |
618 | 0 | Class clazz = ClassUtils.getClass( name ); |
619 | 0 | String alias = StringUtils.lowercaseFirstLetter( ClassUtils.getShortClassName( clazz ) ); |
620 | 0 | xstreamObject.alias( alias, clazz ); |
621 | 0 | if ( !clazz.equals( Model.class ) ) |
622 | |
{ |
623 | 0 | xstreamObject.omitField( clazz, "modelEncoding" ); |
624 | |
} |
625 | |
} |
626 | 0 | catch ( ClassNotFoundException e ) |
627 | |
{ |
628 | 0 | e.printStackTrace(); |
629 | 0 | } |
630 | |
} |
631 | |
} |
632 | |
|
633 | 0 | jarStream.closeEntry(); |
634 | 0 | jarEntry = jarStream.getNextJarEntry(); |
635 | |
} |
636 | |
} |
637 | 0 | catch ( IOException e ) |
638 | |
{ |
639 | 0 | if ( getLog().isDebugEnabled() ) |
640 | |
{ |
641 | 0 | getLog().debug( "IOException: " + e.getMessage(), e ); |
642 | |
} |
643 | |
} |
644 | |
finally |
645 | |
{ |
646 | 0 | IOUtil.close( jarStream ); |
647 | 0 | } |
648 | 0 | } |
649 | |
|
650 | |
|
651 | |
|
652 | |
|
653 | |
|
654 | |
|
655 | |
|
656 | |
|
657 | |
private File getMavenModelJarFile() |
658 | |
throws MojoExecutionException, ProjectBuildingException, ArtifactResolutionException, |
659 | |
ArtifactNotFoundException |
660 | |
{ |
661 | 0 | return getArtifactFile( true ); |
662 | |
} |
663 | |
|
664 | |
|
665 | |
|
666 | |
|
667 | |
|
668 | |
|
669 | |
|
670 | |
|
671 | |
private File getMavenSettingsJarFile() |
672 | |
throws MojoExecutionException, ProjectBuildingException, ArtifactResolutionException, |
673 | |
ArtifactNotFoundException |
674 | |
{ |
675 | 0 | return getArtifactFile( false ); |
676 | |
} |
677 | |
|
678 | |
|
679 | |
|
680 | |
|
681 | |
|
682 | |
|
683 | |
|
684 | |
|
685 | |
|
686 | |
|
687 | |
|
688 | |
|
689 | |
private File getArtifactFile( boolean isPom ) |
690 | |
throws MojoExecutionException, ProjectBuildingException, ArtifactResolutionException, |
691 | |
ArtifactNotFoundException |
692 | |
{ |
693 | 0 | for ( Iterator it = getHelpPluginPom().getDependencies().iterator(); it.hasNext(); ) |
694 | |
{ |
695 | 0 | Dependency depependency = (Dependency) it.next(); |
696 | |
|
697 | 0 | if ( !( depependency.getGroupId().equals( "org.apache.maven" ) ) ) |
698 | |
{ |
699 | 0 | continue; |
700 | |
} |
701 | |
|
702 | 0 | if ( isPom ) |
703 | |
{ |
704 | 0 | if ( !( depependency.getArtifactId().equals( "maven-model" ) ) ) |
705 | |
{ |
706 | 0 | continue; |
707 | |
} |
708 | |
} |
709 | |
else |
710 | |
{ |
711 | 0 | if ( !( depependency.getArtifactId().equals( "maven-settings" ) ) ) |
712 | |
{ |
713 | 0 | continue; |
714 | |
} |
715 | |
} |
716 | |
|
717 | 0 | Artifact mavenArtifact = |
718 | |
getArtifact( depependency.getGroupId() + ":" + depependency.getArtifactId() + ":" |
719 | |
+ depependency.getVersion() ); |
720 | 0 | resolver.resolveAlways( mavenArtifact, remoteRepositories, localRepository ); |
721 | |
|
722 | 0 | return mavenArtifact.getFile(); |
723 | |
} |
724 | |
|
725 | 0 | throw new MojoExecutionException( "Unable to find the 'org.apache.maven:" |
726 | |
+ ( isPom ? "maven-model" : "maven-settings" ) + "' artifact" ); |
727 | |
} |
728 | |
|
729 | |
|
730 | |
|
731 | |
|
732 | |
|
733 | |
|
734 | |
private MavenProject getHelpPluginPom() |
735 | |
throws MojoExecutionException, ProjectBuildingException |
736 | |
{ |
737 | 0 | String resource = "META-INF/maven/org.apache.maven.plugins/maven-help-plugin/pom.properties"; |
738 | |
|
739 | 0 | InputStream resourceAsStream = EvaluateMojo.class.getClassLoader().getResourceAsStream( resource ); |
740 | 0 | Artifact helpPluginArtifact = null; |
741 | 0 | if ( resourceAsStream != null ) |
742 | |
{ |
743 | 0 | Properties properties = new Properties(); |
744 | |
try |
745 | |
{ |
746 | 0 | properties.load( resourceAsStream ); |
747 | |
} |
748 | 0 | catch ( IOException e ) |
749 | |
{ |
750 | 0 | if ( getLog().isDebugEnabled() ) |
751 | |
{ |
752 | 0 | getLog().debug( "IOException: " + e.getMessage(), e ); |
753 | |
} |
754 | 0 | } |
755 | |
|
756 | 0 | String artifactString = |
757 | |
properties.getProperty( "groupId", "unknown" ) + ":" |
758 | |
+ properties.getProperty( "artifactId", "unknown" ) + ":" |
759 | |
+ properties.getProperty( "version", "unknown" ); |
760 | |
|
761 | 0 | helpPluginArtifact = getArtifact( artifactString ); |
762 | |
} |
763 | |
|
764 | 0 | if ( helpPluginArtifact == null ) |
765 | |
{ |
766 | 0 | throw new MojoExecutionException( "The help plugin artifact was not found." ); |
767 | |
} |
768 | |
|
769 | 0 | return getMavenProject( helpPluginArtifact ); |
770 | |
} |
771 | |
|
772 | |
|
773 | |
|
774 | |
|
775 | |
|
776 | |
private static String pluralize( String name ) |
777 | |
{ |
778 | 0 | if ( StringUtils.isEmpty( name ) ) |
779 | |
{ |
780 | 0 | throw new IllegalArgumentException( "name is required" ); |
781 | |
} |
782 | |
|
783 | 0 | if ( name.endsWith( "y" ) ) |
784 | |
{ |
785 | 0 | return name.substring( 0, name.length() - 1 ) + "ies"; |
786 | |
} |
787 | 0 | else if ( name.endsWith( "s" ) ) |
788 | |
{ |
789 | 0 | return name; |
790 | |
} |
791 | |
else |
792 | |
{ |
793 | 0 | return name + "s"; |
794 | |
} |
795 | |
} |
796 | |
} |