1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugin.ide;
20
21 import java.io.File;
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.Iterator;
25 import java.util.List;
26
27 import org.apache.maven.artifact.Artifact;
28 import org.apache.maven.artifact.factory.ArtifactFactory;
29 import org.apache.maven.artifact.repository.ArtifactRepository;
30 import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
31 import org.apache.maven.artifact.resolver.ArtifactResolutionException;
32 import org.apache.maven.artifact.resolver.ArtifactResolver;
33 import org.apache.maven.artifact.versioning.ArtifactVersion;
34 import org.apache.maven.artifact.versioning.VersionRange;
35 import org.apache.maven.model.Dependency;
36 import org.apache.maven.model.Plugin;
37 import org.apache.maven.model.PluginExecution;
38 import org.apache.maven.plugin.MojoExecutionException;
39 import org.apache.maven.plugin.eclipse.Messages;
40 import org.apache.maven.plugin.logging.Log;
41 import org.apache.maven.project.MavenProject;
42 import org.codehaus.plexus.util.FileUtils;
43 import org.codehaus.plexus.util.StringUtils;
44 import org.codehaus.plexus.util.xml.Xpp3Dom;
45
46
47
48
49
50
51 public class IdeUtils
52 {
53 public static final String JAVA_1_1 = "1.1";
54
55 public static final String JAVA_1_2 = "1.2";
56
57 public static final String JAVA_1_3 = "1.3";
58
59 public static final String JAVA_1_4 = "1.4";
60
61 public static final String JAVA_5_0 = "5.0";
62
63 public static final String JAVA_6_0 = "6.0";
64
65 public static final String PROJECT_NAME_DEFAULT_TEMPLATE = "[artifactId]";
66
67 public static final String PROJECT_NAME_WITH_VERSION_TEMPLATE = "[artifactId]-[version]";
68
69 public static final String PROJECT_NAME_WITH_GROUP_TEMPLATE = "[groupId].[artifactId]";
70
71 public static final String PROJECT_NAME_WITH_GROUP_AND_VERSION_TEMPLATE = "[groupId].[artifactId]-[version]";
72
73
74
75
76 private static final String ARTIFACT_MAVEN_COMPILER_PLUGIN = "maven-compiler-plugin";
77
78
79
80
81 private static final String PROPERTY_SOURCE = "source";
82
83
84
85
86 private static final String PROPERTY_ENCODING = "encoding";
87
88
89
90
91 private static final String PROPERTY_TARGET = "target";
92
93
94
95
96 public static final String NOT_AVAILABLE_MARKER_FILE_SUFFIX = "-not-available";
97
98
99
100
101
102
103
104 public static void delete( File f, Log log )
105 throws MojoExecutionException
106 {
107 if ( f.isDirectory() )
108 {
109 log.info( Messages.getString( "EclipseCleanMojo.deletingDirectory", f.getName() ) );
110 }
111 else
112 {
113 log.info( Messages.getString( "EclipseCleanMojo.deletingFile", f.getName() ) );
114 }
115
116 if ( f.exists() )
117 {
118 if ( !f.delete() )
119 {
120 try
121 {
122 FileUtils.forceDelete( f );
123 }
124 catch ( IOException e )
125 {
126 throw new MojoExecutionException( Messages.getString( "EclipseCleanMojo.failedtodelete",
127 new Object[] { f.getName(),
128 f.getAbsolutePath() } ) );
129 }
130 }
131 }
132 else
133 {
134 log.debug( Messages.getString( "EclipseCleanMojo.nofilefound", f.getName() ) );
135 }
136 }
137
138 public static String getCanonicalPath( File file )
139 throws MojoExecutionException
140 {
141 try
142 {
143 return file.getCanonicalPath();
144 }
145 catch ( IOException e )
146 {
147 throw new MojoExecutionException( Messages.getString( "EclipsePlugin.cantcanonicalize", file
148 .getAbsolutePath() ), e );
149 }
150 }
151
152
153
154
155
156
157
158 public static String getCompilerPluginSetting( MavenProject project, String optionName )
159 {
160 String value = findCompilerPluginSettingInPlugins( project.getModel().getBuild().getPlugins(), optionName );
161 if ( value == null && project.getModel().getBuild().getPluginManagement() != null )
162 {
163 value =
164 findCompilerPluginSettingInPlugins( project.getModel().getBuild().getPluginManagement().getPlugins(),
165 optionName );
166 }
167 return value;
168 }
169
170
171
172
173
174
175
176
177 public static String getCompilerSourceVersion( MavenProject project )
178 {
179 return IdeUtils.getCompilerPluginSetting( project, PROPERTY_SOURCE );
180 }
181
182
183
184
185
186
187
188
189 public static String getCompilerSourceEncoding( MavenProject project )
190 {
191 String value = IdeUtils.getCompilerPluginSetting( project, PROPERTY_ENCODING );
192 if ( value == null )
193 {
194 project.getProperties().getProperty( "project.build.sourceEncoding" );
195 }
196 return value;
197 }
198
199
200
201
202
203
204
205
206 public static String getCompilerTargetVersion( MavenProject project )
207 {
208 return IdeUtils.getCompilerPluginSetting( project, PROPERTY_TARGET );
209 }
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251 public static String getArtifactVersion( String[] artifactIds, List dependencies, int len )
252 {
253 String version = null;
254 ArtifactVersion artifactVersion = getArtifactVersion( artifactIds, dependencies );
255 if ( artifactVersion != null )
256 {
257 StringBuffer versionBuffer = new StringBuffer();
258 if( len >= 1 )
259 {
260 versionBuffer.append( artifactVersion.getMajorVersion() );
261 }
262 if( len >= 2 )
263 {
264 versionBuffer.append( '.' );
265 }
266 if( len >= 3 )
267 {
268 versionBuffer.append( artifactVersion.getMinorVersion() );
269 }
270 if( len >= 4 )
271 {
272 versionBuffer.append( '.' );
273 }
274 if( len >= 5 )
275 {
276 versionBuffer.append( artifactVersion.getIncrementalVersion() );
277 }
278 version = versionBuffer.toString();
279 }
280 return version;
281 }
282
283
284
285
286
287
288
289 public static ArtifactVersion getArtifactVersion( String[] artifactIds, List
290 {
291 for ( int j = 0; j < artifactIds.length; j++ )
292 {
293 String id = artifactIds[j];
294 Iterator depIter = dependencies.iterator();
295 while ( depIter.hasNext() )
296 {
297 Dependency dep = (Dependency) depIter.next();
298 if ( id.equals( dep.getArtifactId() ) )
299 {
300 return VersionRange.createFromVersion( dep.getVersion() ).getRecommendedVersion();
301 }
302
303 }
304 }
305 return null;
306 }
307
308
309
310
311
312
313
314
315
316
317
318 public static String getPluginSetting( MavenProject project, String pluginId, String optionName, String defaultValue )
319 {
320 Xpp3Dom dom = getPluginConfigurationDom( project, pluginId );
321 if ( dom != null && dom.getChild( optionName ) != null )
322 {
323 return dom.getChild( optionName ).getValue();
324 }
325 return defaultValue;
326 }
327
328
329
330
331
332
333
334
335
336 public static Xpp3Dom getPluginConfigurationDom( MavenProject project, String pluginId )
337 {
338
339 Plugin plugin = (org.apache.maven.model.Plugin) project.getBuild().getPluginsAsMap().get( pluginId );
340 if ( plugin != null )
341 {
342
343 return (Xpp3Dom) plugin.getConfiguration();
344 }
345 return null;
346 }
347
348
349
350
351
352
353
354
355
356 public static Xpp3Dom[] getPluginConfigurationDom( MavenProject project, String artifactId,
357 String[] subConfiguration )
358 {
359 ArrayList configurationDomList = new ArrayList();
360 Xpp3Dom configuration = getPluginConfigurationDom( project, artifactId );
361 if ( configuration != null )
362 {
363 configurationDomList.add( configuration );
364 for ( int index = 0; !configurationDomList.isEmpty() && subConfiguration != null
365 && index < subConfiguration.length; index++ )
366 {
367 ArrayList newConfigurationDomList = new ArrayList();
368 for ( Iterator childElement = configurationDomList.iterator(); childElement.hasNext(); )
369 {
370 Xpp3Dom child = (Xpp3Dom) childElement.next();
371 Xpp3Dom[] deeperChild = child.getChildren( subConfiguration[index] );
372 for ( int deeperIndex = 0; deeperIndex < deeperChild.length; deeperIndex++ )
373 {
374 if ( deeperChild[deeperIndex] != null )
375 {
376 newConfigurationDomList.add( deeperChild[deeperIndex] );
377 }
378 }
379 }
380 configurationDomList = newConfigurationDomList;
381 }
382 }
383 return (Xpp3Dom[]) configurationDomList.toArray( new Xpp3Dom[configurationDomList.size()] );
384 }
385
386
387
388
389
390
391
392
393
394
395
396
397
398 public static String calculateProjectNameTemplate( String projectNameTemplate, boolean addVersionToProjectName,
399 boolean addGroupIdToProjectName, Log log )
400 {
401 if ( projectNameTemplate != null )
402 {
403 if ( addVersionToProjectName || addGroupIdToProjectName )
404 {
405 log.warn( "projectNameTemplate definition overrides "
406 + "addVersionToProjectName or addGroupIdToProjectName" );
407 }
408 return projectNameTemplate;
409 }
410 else if ( addVersionToProjectName && addGroupIdToProjectName )
411 {
412 return IdeUtils.PROJECT_NAME_WITH_GROUP_AND_VERSION_TEMPLATE;
413 }
414 else if ( addVersionToProjectName )
415 {
416 return IdeUtils.PROJECT_NAME_WITH_VERSION_TEMPLATE;
417 }
418 else if ( addGroupIdToProjectName )
419 {
420 return IdeUtils.PROJECT_NAME_WITH_GROUP_TEMPLATE;
421 }
422 return IdeUtils.PROJECT_NAME_DEFAULT_TEMPLATE;
423 }
424
425
426
427
428 protected static String getProjectName( String template, IdeDependency dep )
429 {
430 return getProjectName( template, dep.getGroupId(), dep.getArtifactId(), dep.getVersion() );
431 }
432
433
434
435
436
437
438
439
440 public static String getProjectName( String template, Artifact artifact )
441 {
442 return getProjectName( template, artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion() );
443 }
444
445 public static String getProjectName( String template, MavenProject project )
446 {
447 return getProjectName( template, project.getGroupId(), project.getArtifactId(), project.getVersion() );
448 }
449
450 public static String getProjectName( MavenProject project, boolean addVersionToProjectName )
451 {
452 return getProjectName( addVersionToProjectName ? PROJECT_NAME_WITH_VERSION_TEMPLATE
453 : PROJECT_NAME_DEFAULT_TEMPLATE, project );
454 }
455
456
457
458
459
460 public static File getNotAvailableMarkerFile( ArtifactRepository localRepository, Artifact artifact )
461 {
462 return new File( localRepository.getBasedir(), localRepository.pathOf( artifact )
463 + NOT_AVAILABLE_MARKER_FILE_SUFFIX );
464 }
465
466
467
468
469
470
471
472
473
474
475
476 public static Artifact resolveArtifact( ArtifactResolver artifactResolver, Artifact artifact, List remoteRepos,
477 ArtifactRepository localRepository, Log log )
478
479 {
480 try
481 {
482 artifactResolver.resolve( artifact, remoteRepos, localRepository );
483 }
484 catch ( ArtifactNotFoundException e )
485 {
486
487
488
489
490
491
492
493
494 if ( artifact.getFile() != null && artifact.getFile().isFile() )
495 {
496 artifact.setResolved( true );
497 }
498 }
499 catch ( ArtifactResolutionException e )
500 {
501 String message =
502 Messages.getString( "IdeUtils.errorresolving", new Object[] { artifact.getClassifier(),
503 artifact.getId(), e.getMessage() } );
504
505 log.warn( message );
506 }
507
508 return artifact;
509 }
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524 public static Artifact createArtifactWithClassifier( String groupId, String artifactId, String version,
525 String depClassifier, String inClassifier,
526 ArtifactFactory artifactFactory )
527 {
528 String type = null;
529
530
531 if ( "sources".equals( inClassifier ) )
532 {
533 type = "java-source";
534 }
535 else
536 {
537 type = inClassifier;
538 }
539
540 String finalClassifier = null;
541 if ( depClassifier == null )
542 {
543 finalClassifier = inClassifier;
544 }
545 else if ( "sources".equals( inClassifier ) && "tests".equals( depClassifier ) )
546 {
547
548 finalClassifier = "test-sources";
549 }
550 else
551 {
552 finalClassifier = depClassifier + "-" + inClassifier;
553 }
554
555 return artifactFactory.createArtifactWithClassifier( groupId, artifactId, version, type, finalClassifier );
556 }
557
558 public static String resolveJavaVersion( MavenProject project )
559 {
560 String version = IdeUtils.getCompilerTargetVersion( project );
561 if ( version == null )
562 {
563 version = IdeUtils.getCompilerSourceVersion( project );
564 }
565
566 if ( "1.5".equals( version ) )
567 {
568 version = IdeUtils.JAVA_5_0;
569 }
570 else if ( "1.6".equals( version ) )
571 {
572 version = IdeUtils.JAVA_6_0;
573 }
574 else if ( version != null && version.length() == 1 )
575 {
576 version = version + ".0";
577 }
578
579 return version == null ? IdeUtils.JAVA_1_4 : version;
580 }
581
582 public static String toRelativeAndFixSeparator( File basedir, File fileToAdd, boolean replaceSlashesWithDashes )
583 throws MojoExecutionException
584 {
585 if ( !fileToAdd.isAbsolute() )
586 {
587 fileToAdd = new File( basedir, fileToAdd.getPath() );
588 }
589
590 String basedirPath = getCanonicalPath( basedir );
591 String absolutePath = getCanonicalPath( fileToAdd );
592
593 String relative = null;
594
595 if ( absolutePath.equals( basedirPath ) )
596 {
597 relative = ".";
598 }
599 else if ( absolutePath.startsWith( basedirPath ) )
600 {
601
602
603
604
605
606
607 int length = basedirPath.length() + 1;
608 if ( basedirPath.endsWith( "\\" ) )
609 {
610 length--;
611 }
612 relative = absolutePath.substring( length );
613 }
614 else
615 {
616 relative = absolutePath;
617 }
618
619 relative = fixSeparator( relative );
620
621 if ( replaceSlashesWithDashes )
622 {
623 relative = StringUtils.replace( relative, '/', '-' );
624 relative = StringUtils.replace( relative, ':', '-' );
625 }
626
627 return relative;
628 }
629
630
631
632
633
634
635
636 public static String fixSeparator( String filename )
637 {
638 return StringUtils.replace( filename, '\\', '/' );
639 }
640
641
642
643
644
645
646
647
648
649
650
651 public static String fixWindowsDriveURI( String input )
652 {
653 return input.replaceAll( "file:([a-zA-Z]):", "file:/$1:" );
654 }
655
656
657
658
659
660
661
662 private static String findCompilerPluginSettingInPlugins( List plugins, String optionName )
663 {
664 String value = null;
665
666 for ( Iterator it = plugins.iterator(); it.hasNext(); )
667 {
668 Plugin plugin = (Plugin) it.next();
669
670 if ( plugin.getArtifactId().equals( ARTIFACT_MAVEN_COMPILER_PLUGIN ) )
671 {
672
673 Xpp3Dom o = (Xpp3Dom) plugin.getConfiguration();
674
675
676 if ( o != null && o.getChild( optionName ) != null )
677 {
678 value = o.getChild( optionName ).getValue();
679 }
680
681 List executions = plugin.getExecutions();
682
683
684 for ( Iterator iter = executions.iterator(); iter.hasNext(); )
685 {
686 PluginExecution execution = (PluginExecution) iter.next();
687
688
689 o = (Xpp3Dom) execution.getConfiguration();
690
691 if ( o != null && o.getChild( optionName ) != null )
692 {
693 value = o.getChild( optionName ).getValue();
694 }
695 }
696 }
697 }
698 return value;
699 }
700
701 private static String getProjectName( String template, String groupId, String artifactId, String version )
702 {
703 String s = template;
704 s = s.replaceAll( "\\[groupId\\]", groupId );
705 s = s.replaceAll( "\\[artifactId\\]", artifactId );
706 s = s.replaceAll( "\\[version\\]", version );
707 return s;
708 }
709
710 private IdeUtils()
711 {
712
713 }
714 }