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.model.Dependency;
34 import org.apache.maven.model.Plugin;
35 import org.apache.maven.model.PluginExecution;
36 import org.apache.maven.plugin.MojoExecutionException;
37 import org.apache.maven.plugin.eclipse.Messages;
38 import org.apache.maven.plugin.logging.Log;
39 import org.apache.maven.project.MavenProject;
40 import org.codehaus.plexus.util.FileUtils;
41 import org.codehaus.plexus.util.PropertyUtils;
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 public static String getArtifactVersion( String[] artifactIds, List dependencies, int len )
244 {
245 for ( int j = 0; j < artifactIds.length; j++ )
246 {
247 String id = artifactIds[j];
248 Iterator depIter = dependencies.iterator();
249 while ( depIter.hasNext() )
250 {
251 Dependency dep = (Dependency) depIter.next();
252 if ( id.equals( dep.getArtifactId() ) )
253 {
254 return StringUtils.substring( dep.getVersion(), 0, len );
255 }
256
257 }
258 }
259 return null;
260 }
261
262
263
264
265
266
267
268
269
270
271
272 public static String getPluginSetting( MavenProject project, String pluginId, String optionName, String defaultValue )
273 {
274 Xpp3Dom dom = getPluginConfigurationDom( project, pluginId );
275 if ( dom != null && dom.getChild( optionName ) != null )
276 {
277 return dom.getChild( optionName ).getValue();
278 }
279 return defaultValue;
280 }
281
282
283
284
285
286
287
288
289
290 public static Xpp3Dom getPluginConfigurationDom( MavenProject project, String pluginId )
291 {
292
293 Plugin plugin = (org.apache.maven.model.Plugin) project.getBuild().getPluginsAsMap().get( pluginId );
294 if ( plugin != null )
295 {
296
297 return (Xpp3Dom) plugin.getConfiguration();
298 }
299 return null;
300 }
301
302
303
304
305
306
307
308
309
310 public static Xpp3Dom[] getPluginConfigurationDom( MavenProject project, String artifactId,
311 String[] subConfiguration )
312 {
313 ArrayList configurationDomList = new ArrayList();
314 Xpp3Dom configuration = getPluginConfigurationDom( project, artifactId );
315 if ( configuration != null )
316 {
317 configurationDomList.add( configuration );
318 for ( int index = 0; !configurationDomList.isEmpty() && subConfiguration != null
319 && index < subConfiguration.length; index++ )
320 {
321 ArrayList newConfigurationDomList = new ArrayList();
322 for ( Iterator childElement = configurationDomList.iterator(); childElement.hasNext(); )
323 {
324 Xpp3Dom child = (Xpp3Dom) childElement.next();
325 Xpp3Dom[] deeperChild = child.getChildren( subConfiguration[index] );
326 for ( int deeperIndex = 0; deeperIndex < deeperChild.length; deeperIndex++ )
327 {
328 if ( deeperChild[deeperIndex] != null )
329 {
330 newConfigurationDomList.add( deeperChild[deeperIndex] );
331 }
332 }
333 }
334 configurationDomList = newConfigurationDomList;
335 }
336 }
337 return (Xpp3Dom[]) configurationDomList.toArray( new Xpp3Dom[configurationDomList.size()] );
338 }
339
340
341
342
343
344
345
346
347
348
349
350
351
352 public static String calculateProjectNameTemplate( String projectNameTemplate, boolean addVersionToProjectName,
353 boolean addGroupIdToProjectName, Log log )
354 {
355 if ( projectNameTemplate != null )
356 {
357 if ( addVersionToProjectName || addGroupIdToProjectName )
358 {
359 log.warn( "projectNameTemplate definition overrides "
360 + "addVersionToProjectName or addGroupIdToProjectName" );
361 }
362 return projectNameTemplate;
363 }
364 else if ( addVersionToProjectName && addGroupIdToProjectName )
365 {
366 return IdeUtils.PROJECT_NAME_WITH_GROUP_AND_VERSION_TEMPLATE;
367 }
368 else if ( addVersionToProjectName )
369 {
370 return IdeUtils.PROJECT_NAME_WITH_VERSION_TEMPLATE;
371 }
372 else if ( addGroupIdToProjectName )
373 {
374 return IdeUtils.PROJECT_NAME_WITH_GROUP_TEMPLATE;
375 }
376 return IdeUtils.PROJECT_NAME_DEFAULT_TEMPLATE;
377 }
378
379
380
381
382 protected static String getProjectName( String template, IdeDependency dep )
383 {
384 return getProjectName( template, dep.getGroupId(), dep.getArtifactId(), dep.getVersion() );
385 }
386
387
388
389
390
391
392
393
394 public static String getProjectName( String template, Artifact artifact )
395 {
396 return getProjectName( template, artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion() );
397 }
398
399 public static String getProjectName( String template, MavenProject project )
400 {
401 return getProjectName( template, project.getGroupId(), project.getArtifactId(), project.getVersion() );
402 }
403
404 private static String getProjectName( IdeDependency dep, boolean addVersionToProjectName )
405 {
406 return getProjectName( addVersionToProjectName ? PROJECT_NAME_WITH_VERSION_TEMPLATE
407 : PROJECT_NAME_DEFAULT_TEMPLATE, dep );
408 }
409
410 public static String getProjectName( MavenProject project, boolean addVersionToProjectName )
411 {
412 return getProjectName( addVersionToProjectName ? PROJECT_NAME_WITH_VERSION_TEMPLATE
413 : PROJECT_NAME_DEFAULT_TEMPLATE, project );
414 }
415
416
417
418
419
420 public static File getNotAvailableMarkerFile( ArtifactRepository localRepository, Artifact artifact )
421 {
422 return new File( localRepository.getBasedir(), localRepository.pathOf( artifact )
423 + NOT_AVAILABLE_MARKER_FILE_SUFFIX );
424 }
425
426
427
428
429
430
431
432
433
434
435
436 public static Artifact resolveArtifact( ArtifactResolver artifactResolver, Artifact artifact, List remoteRepos,
437 ArtifactRepository localRepository, Log log )
438
439 {
440 try
441 {
442 artifactResolver.resolve( artifact, remoteRepos, localRepository );
443 }
444 catch ( ArtifactNotFoundException e )
445 {
446
447 }
448 catch ( ArtifactResolutionException e )
449 {
450 String message =
451 Messages.getString( "IdeUtils.errorresolving", new Object[] { artifact.getClassifier(),
452 artifact.getId(), e.getMessage() } );
453
454 log.warn( message );
455 }
456
457 return artifact;
458 }
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473 public static Artifact createArtifactWithClassifier( String groupId, String artifactId, String version,
474 String depClassifier, String inClassifier,
475 ArtifactFactory artifactFactory )
476 {
477 String type = null;
478
479
480 if ( "sources".equals( inClassifier ) )
481 {
482 type = "java-source";
483 }
484 else
485 {
486 type = inClassifier;
487 }
488
489 String finalClassifier = null;
490 if ( depClassifier == null )
491 {
492 finalClassifier = inClassifier;
493 }
494 else if ( "sources".equals( inClassifier ) && "tests".equals( depClassifier ) )
495 {
496
497 finalClassifier = "test-sources";
498 }
499 else
500 {
501 finalClassifier = depClassifier + "-" + inClassifier;
502 }
503
504 return artifactFactory.createArtifactWithClassifier( groupId, artifactId, version, type, finalClassifier );
505 }
506
507 public static String resolveJavaVersion( MavenProject project )
508 {
509 String version = IdeUtils.getCompilerTargetVersion( project );
510 if ( version == null )
511 {
512 version = IdeUtils.getCompilerSourceVersion( project );
513 }
514
515 if ( "1.5".equals( version ) )
516 {
517 version = IdeUtils.JAVA_5_0;
518 }
519 else if ( "1.6".equals( version ) )
520 {
521 version = IdeUtils.JAVA_6_0;
522 }
523 else if ( version != null && version.length() == 1 )
524 {
525 version = version + ".0";
526 }
527
528 return version == null ? IdeUtils.JAVA_1_4 : version;
529 }
530
531 public static String toRelativeAndFixSeparator( File basedir, File fileToAdd, boolean replaceSlashesWithDashes )
532 throws MojoExecutionException
533 {
534 if ( !fileToAdd.isAbsolute() )
535 {
536 fileToAdd = new File( basedir, fileToAdd.getPath() );
537 }
538
539 String basedirPath = getCanonicalPath( basedir );
540 String absolutePath = getCanonicalPath( fileToAdd );
541
542 String relative = null;
543
544 if ( absolutePath.equals( basedirPath ) )
545 {
546 relative = ".";
547 }
548 else if ( absolutePath.startsWith( basedirPath ) )
549 {
550
551
552
553
554
555
556 int length = basedirPath.length() + 1;
557 if ( basedirPath.endsWith( "\\" ) )
558 {
559 length--;
560 }
561 relative = absolutePath.substring( length );
562 }
563 else
564 {
565 relative = absolutePath;
566 }
567
568 relative = fixSeparator( relative );
569
570 if ( replaceSlashesWithDashes )
571 {
572 relative = StringUtils.replace( relative, '/', '-' );
573 relative = StringUtils.replace( relative, ':', '-' );
574 }
575
576 return relative;
577 }
578
579
580
581
582
583
584
585 public static String fixSeparator( String filename )
586 {
587 return StringUtils.replace( filename, '\\', '/' );
588 }
589
590
591
592
593
594
595
596
597
598
599
600 public static String fixWindowsDriveURI( String input )
601 {
602 return input.replaceAll( "file:([a-zA-Z]):", "file:/$1:" );
603 }
604
605
606
607
608
609
610
611 private static String findCompilerPluginSettingInPlugins( List plugins, String optionName )
612 {
613 String value = null;
614
615 for ( Iterator it = plugins.iterator(); it.hasNext(); )
616 {
617 Plugin plugin = (Plugin) it.next();
618
619 if ( plugin.getArtifactId().equals( ARTIFACT_MAVEN_COMPILER_PLUGIN ) )
620 {
621
622 Xpp3Dom o = (Xpp3Dom) plugin.getConfiguration();
623
624
625 if ( o != null && o.getChild( optionName ) != null )
626 {
627 value = o.getChild( optionName ).getValue();
628 }
629
630 List executions = plugin.getExecutions();
631
632
633 for ( Iterator iter = executions.iterator(); iter.hasNext(); )
634 {
635 PluginExecution execution = (PluginExecution) iter.next();
636
637
638 o = (Xpp3Dom) execution.getConfiguration();
639
640 if ( o != null && o.getChild( optionName ) != null )
641 {
642 value = o.getChild( optionName ).getValue();
643 }
644 }
645 }
646 }
647 return value;
648 }
649
650 private static String getProjectName( String template, String groupId, String artifactId, String version )
651 {
652 String s = template;
653 s = s.replaceAll( "\\[groupId\\]", groupId );
654 s = s.replaceAll( "\\[artifactId\\]", artifactId );
655 s = s.replaceAll( "\\[version\\]", version );
656 return s;
657 }
658
659 private IdeUtils()
660 {
661
662 }
663 }