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 | 0 | if ( f.isDirectory() ) |
107 | |
{ |
108 | 0 | log.info( Messages.getString( "EclipseCleanMojo.deletingDirectory", f.getName() ) ); |
109 | |
} |
110 | |
else |
111 | |
{ |
112 | 0 | log.info( Messages.getString( "EclipseCleanMojo.deletingFile", f.getName() ) ); |
113 | |
} |
114 | |
|
115 | 0 | if ( f.exists() ) |
116 | |
{ |
117 | 0 | if ( !f.delete() ) |
118 | |
{ |
119 | |
try |
120 | |
{ |
121 | 0 | FileUtils.forceDelete( f ); |
122 | |
} |
123 | 0 | catch ( IOException e ) |
124 | |
{ |
125 | 0 | throw new MojoExecutionException( Messages.getString( "EclipseCleanMojo.failedtodelete", |
126 | |
new Object[] { f.getName(), |
127 | |
f.getAbsolutePath() } ) ); |
128 | 0 | } |
129 | |
} |
130 | |
} |
131 | |
else |
132 | |
{ |
133 | 0 | log.debug( Messages.getString( "EclipseCleanMojo.nofilefound", f.getName() ) ); |
134 | |
} |
135 | 0 | } |
136 | |
|
137 | |
public static String getCanonicalPath( File file ) |
138 | |
throws MojoExecutionException |
139 | |
{ |
140 | |
try |
141 | |
{ |
142 | 282 | return file.getCanonicalPath(); |
143 | |
} |
144 | 0 | catch ( IOException e ) |
145 | |
{ |
146 | 0 | 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 | 0 | String value = findCompilerPluginSettingInPlugins( project.getModel().getBuild().getPlugins(), optionName ); |
160 | 0 | if ( value == null && project.getModel().getBuild().getPluginManagement() != null ) |
161 | |
{ |
162 | 0 | value = |
163 | |
findCompilerPluginSettingInPlugins( project.getModel().getBuild().getPluginManagement().getPlugins(), |
164 | |
optionName ); |
165 | |
} |
166 | 0 | return value; |
167 | |
} |
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
public static String getCompilerSourceVersion( MavenProject project ) |
177 | |
{ |
178 | 0 | return IdeUtils.getCompilerPluginSetting( project, PROPERTY_SOURCE ); |
179 | |
} |
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
public static String getCompilerSourceEncoding( MavenProject project ) |
189 | |
{ |
190 | 0 | String value = IdeUtils.getCompilerPluginSetting( project, PROPERTY_ENCODING ); |
191 | 0 | if ( value == null ) |
192 | |
{ |
193 | 0 | project.getProperties().getProperty( "project.build.sourceEncoding" ); |
194 | |
} |
195 | 0 | return value; |
196 | |
} |
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
public static String getCompilerTargetVersion( MavenProject project ) |
206 | |
{ |
207 | 0 | 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 | 0 | for ( int j = 0; j < artifactIds.length; j++ ) |
246 | |
{ |
247 | 0 | String id = artifactIds[j]; |
248 | 0 | Iterator depIter = dependencies.iterator(); |
249 | 0 | while ( depIter.hasNext() ) |
250 | |
{ |
251 | 0 | Dependency dep = (Dependency) depIter.next(); |
252 | 0 | if ( id.equals( dep.getArtifactId() ) ) |
253 | |
{ |
254 | 0 | return StringUtils.substring( dep.getVersion(), 0, len ); |
255 | |
} |
256 | |
|
257 | 0 | } |
258 | |
} |
259 | 0 | 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 | 12 | Xpp3Dom dom = getPluginConfigurationDom( project, pluginId ); |
275 | 12 | if ( dom != null && dom.getChild( optionName ) != null ) |
276 | |
{ |
277 | 0 | return dom.getChild( optionName ).getValue(); |
278 | |
} |
279 | 12 | return defaultValue; |
280 | |
} |
281 | |
|
282 | |
|
283 | |
|
284 | |
|
285 | |
|
286 | |
|
287 | |
|
288 | |
|
289 | |
|
290 | |
public static Xpp3Dom getPluginConfigurationDom( MavenProject project, String pluginId ) |
291 | |
{ |
292 | |
|
293 | 12 | Plugin plugin = (org.apache.maven.model.Plugin) project.getBuild().getPluginsAsMap().get( pluginId ); |
294 | 12 | if ( plugin != null ) |
295 | |
{ |
296 | |
|
297 | 0 | return (Xpp3Dom) plugin.getConfiguration(); |
298 | |
} |
299 | 12 | 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 | 0 | ArrayList configurationDomList = new ArrayList(); |
314 | 0 | Xpp3Dom configuration = getPluginConfigurationDom( project, artifactId ); |
315 | 0 | if ( configuration != null ) |
316 | |
{ |
317 | 0 | configurationDomList.add( configuration ); |
318 | 0 | for ( int index = 0; !configurationDomList.isEmpty() && subConfiguration != null |
319 | 0 | && index < subConfiguration.length; index++ ) |
320 | |
{ |
321 | 0 | ArrayList newConfigurationDomList = new ArrayList(); |
322 | 0 | for ( Iterator childElement = configurationDomList.iterator(); childElement.hasNext(); ) |
323 | |
{ |
324 | 0 | Xpp3Dom child = (Xpp3Dom) childElement.next(); |
325 | 0 | Xpp3Dom[] deeperChild = child.getChildren( subConfiguration[index] ); |
326 | 0 | for ( int deeperIndex = 0; deeperIndex < deeperChild.length; deeperIndex++ ) |
327 | |
{ |
328 | 0 | if ( deeperChild[deeperIndex] != null ) |
329 | |
{ |
330 | 0 | newConfigurationDomList.add( deeperChild[deeperIndex] ); |
331 | |
} |
332 | |
} |
333 | 0 | } |
334 | 0 | configurationDomList = newConfigurationDomList; |
335 | |
} |
336 | |
} |
337 | 0 | 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 | 0 | if ( projectNameTemplate != null ) |
356 | |
{ |
357 | 0 | if ( addVersionToProjectName || addGroupIdToProjectName ) |
358 | |
{ |
359 | 0 | log.warn( "projectNameTemplate definition overrides " |
360 | |
+ "addVersionToProjectName or addGroupIdToProjectName" ); |
361 | |
} |
362 | 0 | return projectNameTemplate; |
363 | |
} |
364 | 0 | else if ( addVersionToProjectName && addGroupIdToProjectName ) |
365 | |
{ |
366 | 0 | return IdeUtils.PROJECT_NAME_WITH_GROUP_AND_VERSION_TEMPLATE; |
367 | |
} |
368 | 0 | else if ( addVersionToProjectName ) |
369 | |
{ |
370 | 0 | return IdeUtils.PROJECT_NAME_WITH_VERSION_TEMPLATE; |
371 | |
} |
372 | 0 | else if ( addGroupIdToProjectName ) |
373 | |
{ |
374 | 0 | return IdeUtils.PROJECT_NAME_WITH_GROUP_TEMPLATE; |
375 | |
} |
376 | 0 | return IdeUtils.PROJECT_NAME_DEFAULT_TEMPLATE; |
377 | |
} |
378 | |
|
379 | |
|
380 | |
|
381 | |
|
382 | |
protected static String getProjectName( String template, IdeDependency dep ) |
383 | |
{ |
384 | 24 | 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 | 0 | return getProjectName( template, artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion() ); |
397 | |
} |
398 | |
|
399 | |
public static String getProjectName( String template, MavenProject project ) |
400 | |
{ |
401 | 0 | return getProjectName( template, project.getGroupId(), project.getArtifactId(), project.getVersion() ); |
402 | |
} |
403 | |
|
404 | |
private static String getProjectName( IdeDependency dep, boolean addVersionToProjectName ) |
405 | |
{ |
406 | 0 | 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 | 0 | 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 | 0 | 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 | 0 | artifactResolver.resolve( artifact, remoteRepos, localRepository ); |
443 | |
} |
444 | 0 | catch ( ArtifactNotFoundException e ) |
445 | |
{ |
446 | |
|
447 | |
} |
448 | 0 | catch ( ArtifactResolutionException e ) |
449 | |
{ |
450 | 0 | String message = |
451 | |
Messages.getString( "IdeUtils.errorresolving", new Object[] { artifact.getClassifier(), |
452 | |
artifact.getId(), e.getMessage() } ); |
453 | |
|
454 | 0 | log.warn( message ); |
455 | 0 | } |
456 | |
|
457 | 0 | 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 | 0 | String type = null; |
478 | |
|
479 | |
|
480 | 0 | if ( "sources".equals( inClassifier ) ) |
481 | |
{ |
482 | 0 | type = "java-source"; |
483 | |
} |
484 | |
else |
485 | |
{ |
486 | 0 | type = inClassifier; |
487 | |
} |
488 | |
|
489 | 0 | String finalClassifier = null; |
490 | 0 | if ( depClassifier == null ) |
491 | |
{ |
492 | 0 | finalClassifier = inClassifier; |
493 | |
} |
494 | 0 | else if ( "sources".equals( inClassifier ) && "tests".equals( depClassifier ) ) |
495 | |
{ |
496 | |
|
497 | 0 | finalClassifier = "test-sources"; |
498 | |
} |
499 | |
else |
500 | |
{ |
501 | 0 | finalClassifier = depClassifier + "-" + inClassifier; |
502 | |
} |
503 | |
|
504 | 0 | return artifactFactory.createArtifactWithClassifier( groupId, artifactId, version, type, finalClassifier ); |
505 | |
} |
506 | |
|
507 | |
public static String resolveJavaVersion( MavenProject project ) |
508 | |
{ |
509 | 0 | String version = IdeUtils.getCompilerTargetVersion( project ); |
510 | 0 | if ( version == null ) |
511 | |
{ |
512 | 0 | version = IdeUtils.getCompilerSourceVersion( project ); |
513 | |
} |
514 | |
|
515 | 0 | if ( "1.5".equals( version ) ) |
516 | |
{ |
517 | 0 | version = IdeUtils.JAVA_5_0; |
518 | |
} |
519 | 0 | else if ( "1.6".equals( version ) ) |
520 | |
{ |
521 | 0 | version = IdeUtils.JAVA_6_0; |
522 | |
} |
523 | 0 | else if ( version != null && version.length() == 1 ) |
524 | |
{ |
525 | 0 | version = version + ".0"; |
526 | |
} |
527 | |
|
528 | 0 | 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 | 138 | if ( !fileToAdd.isAbsolute() ) |
535 | |
{ |
536 | 66 | fileToAdd = new File( basedir, fileToAdd.getPath() ); |
537 | |
} |
538 | |
|
539 | 138 | String basedirPath = getCanonicalPath( basedir ); |
540 | 138 | String absolutePath = getCanonicalPath( fileToAdd ); |
541 | |
|
542 | 138 | String relative = null; |
543 | |
|
544 | 138 | if ( absolutePath.equals( basedirPath ) ) |
545 | |
{ |
546 | 0 | relative = "."; |
547 | |
} |
548 | 138 | else if ( absolutePath.startsWith( basedirPath ) ) |
549 | |
{ |
550 | |
|
551 | |
|
552 | |
|
553 | |
|
554 | |
|
555 | |
|
556 | 132 | int length = basedirPath.length() + 1; |
557 | 132 | if ( basedirPath.endsWith( "\\" ) ) |
558 | |
{ |
559 | 6 | length--; |
560 | |
} |
561 | 132 | relative = absolutePath.substring( length ); |
562 | 132 | } |
563 | |
else |
564 | |
{ |
565 | 6 | relative = absolutePath; |
566 | |
} |
567 | |
|
568 | 138 | relative = fixSeparator( relative ); |
569 | |
|
570 | 138 | if ( replaceSlashesWithDashes ) |
571 | |
{ |
572 | 12 | relative = StringUtils.replace( relative, '/', '-' ); |
573 | 12 | relative = StringUtils.replace( relative, ':', '-' ); |
574 | |
} |
575 | |
|
576 | 138 | return relative; |
577 | |
} |
578 | |
|
579 | |
|
580 | |
|
581 | |
|
582 | |
|
583 | |
|
584 | |
|
585 | |
public static String fixSeparator( String filename ) |
586 | |
{ |
587 | 252 | 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 | 0 | 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 | 0 | String value = null; |
614 | |
|
615 | 0 | for ( Iterator it = plugins.iterator(); it.hasNext(); ) |
616 | |
{ |
617 | 0 | Plugin plugin = (Plugin) it.next(); |
618 | |
|
619 | 0 | if ( plugin.getArtifactId().equals( ARTIFACT_MAVEN_COMPILER_PLUGIN ) ) |
620 | |
{ |
621 | |
|
622 | 0 | Xpp3Dom o = (Xpp3Dom) plugin.getConfiguration(); |
623 | |
|
624 | |
|
625 | 0 | if ( o != null && o.getChild( optionName ) != null ) |
626 | |
{ |
627 | 0 | value = o.getChild( optionName ).getValue(); |
628 | |
} |
629 | |
|
630 | 0 | List executions = plugin.getExecutions(); |
631 | |
|
632 | |
|
633 | 0 | for ( Iterator iter = executions.iterator(); iter.hasNext(); ) |
634 | |
{ |
635 | 0 | PluginExecution execution = (PluginExecution) iter.next(); |
636 | |
|
637 | |
|
638 | 0 | o = (Xpp3Dom) execution.getConfiguration(); |
639 | |
|
640 | 0 | if ( o != null && o.getChild( optionName ) != null ) |
641 | |
{ |
642 | 0 | value = o.getChild( optionName ).getValue(); |
643 | |
} |
644 | 0 | } |
645 | |
} |
646 | 0 | } |
647 | 0 | return value; |
648 | |
} |
649 | |
|
650 | |
private static String getProjectName( String template, String groupId, String artifactId, String version ) |
651 | |
{ |
652 | 24 | String s = template; |
653 | 24 | s = s.replaceAll( "\\[groupId\\]", groupId ); |
654 | 24 | s = s.replaceAll( "\\[artifactId\\]", artifactId ); |
655 | 24 | s = s.replaceAll( "\\[version\\]", version ); |
656 | 24 | return s; |
657 | |
} |
658 | |
|
659 | |
private IdeUtils() |
660 | 0 | { |
661 | |
|
662 | 0 | } |
663 | |
} |