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