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