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 | 0 | if ( f.isDirectory() ) |
100 | |
{ |
101 | 0 | log.info( Messages.getString( "EclipseCleanMojo.deletingDirectory", f.getName() ) ); |
102 | |
} |
103 | |
else |
104 | |
{ |
105 | 0 | log.info( Messages.getString( "EclipseCleanMojo.deletingFile", f.getName() ) ); |
106 | |
} |
107 | |
|
108 | 0 | if ( f.exists() ) |
109 | |
{ |
110 | 0 | if ( !f.delete() ) |
111 | |
{ |
112 | |
try |
113 | |
{ |
114 | 0 | FileUtils.forceDelete( f ); |
115 | |
} |
116 | 0 | catch ( IOException e ) |
117 | |
{ |
118 | 0 | throw new MojoExecutionException( Messages.getString( "EclipseCleanMojo.failedtodelete", |
119 | |
new Object[] { f.getName(), |
120 | |
f.getAbsolutePath() } ) ); |
121 | 0 | } |
122 | |
} |
123 | |
} |
124 | |
else |
125 | |
{ |
126 | 0 | log.debug( Messages.getString( "EclipseCleanMojo.nofilefound", f.getName() ) ); |
127 | |
} |
128 | 0 | } |
129 | |
|
130 | |
public static String getCanonicalPath( File file ) |
131 | |
throws MojoExecutionException |
132 | |
{ |
133 | |
try |
134 | |
{ |
135 | 180 | return file.getCanonicalPath(); |
136 | |
} |
137 | 0 | catch ( IOException e ) |
138 | |
{ |
139 | 0 | 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 | 0 | String value = findCompilerPluginSettingInPlugins( project.getModel().getBuild().getPlugins(), optionName ); |
153 | 0 | if ( value == null && project.getModel().getBuild().getPluginManagement() != null ) |
154 | |
{ |
155 | 0 | value = |
156 | |
findCompilerPluginSettingInPlugins( project.getModel().getBuild().getPluginManagement().getPlugins(), |
157 | |
optionName ); |
158 | |
} |
159 | 0 | return value; |
160 | |
} |
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
public static String getCompilerSourceVersion( MavenProject project ) |
170 | |
{ |
171 | 0 | return IdeUtils.getCompilerPluginSetting( project, PROPERTY_SOURCE ); |
172 | |
} |
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
public static String getCompilerTargetVersion( MavenProject project ) |
182 | |
{ |
183 | 0 | 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 | 0 | for ( int j = 0; j < artifactIds.length; j++ ) |
222 | |
{ |
223 | 0 | String id = artifactIds[j]; |
224 | 0 | Iterator depIter = dependencies.iterator(); |
225 | 0 | while ( depIter.hasNext() ) |
226 | |
{ |
227 | 0 | Dependency dep = (Dependency) depIter.next(); |
228 | 0 | if ( id.equals( dep.getArtifactId() ) ) |
229 | |
{ |
230 | 0 | return StringUtils.substring( dep.getVersion(), 0, len ); |
231 | |
} |
232 | |
|
233 | |
} |
234 | |
} |
235 | 0 | 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 | 0 | Xpp3Dom dom = getPluginConfigurationDom( project, pluginId ); |
251 | 0 | if ( dom != null && dom.getChild( optionName ) != null ) |
252 | |
{ |
253 | 0 | return dom.getChild( optionName ).getValue(); |
254 | |
} |
255 | 0 | return defaultValue; |
256 | |
} |
257 | |
|
258 | |
|
259 | |
|
260 | |
|
261 | |
|
262 | |
|
263 | |
|
264 | |
|
265 | |
|
266 | |
public static Xpp3Dom getPluginConfigurationDom( MavenProject project, String pluginId ) |
267 | |
{ |
268 | |
|
269 | 0 | Plugin plugin = (org.apache.maven.model.Plugin) project.getBuild().getPluginsAsMap().get( pluginId ); |
270 | 0 | if ( plugin != null ) |
271 | |
{ |
272 | |
|
273 | 0 | return (Xpp3Dom) plugin.getConfiguration(); |
274 | |
} |
275 | 0 | 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 | 0 | ArrayList configurationDomList = new ArrayList(); |
290 | 0 | Xpp3Dom configuration = getPluginConfigurationDom( project, artifactId ); |
291 | 0 | if ( configuration != null ) |
292 | |
{ |
293 | 0 | configurationDomList.add( configuration ); |
294 | 0 | for ( int index = 0; !configurationDomList.isEmpty() && subConfiguration != null |
295 | 0 | && index < subConfiguration.length; index++ ) |
296 | |
{ |
297 | 0 | ArrayList newConfigurationDomList = new ArrayList(); |
298 | 0 | for ( Iterator childElement = configurationDomList.iterator(); childElement.hasNext(); ) |
299 | |
{ |
300 | 0 | Xpp3Dom child = (Xpp3Dom) childElement.next(); |
301 | 0 | Xpp3Dom[] deeperChild = child.getChildren( subConfiguration[index] ); |
302 | 0 | for ( int deeperIndex = 0; deeperIndex < deeperChild.length; deeperIndex++ ) |
303 | |
{ |
304 | 0 | if ( deeperChild[deeperIndex] != null ) |
305 | |
{ |
306 | 0 | newConfigurationDomList.add( deeperChild[deeperIndex] ); |
307 | |
} |
308 | |
} |
309 | |
} |
310 | 0 | configurationDomList = newConfigurationDomList; |
311 | |
} |
312 | |
} |
313 | 0 | 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 | 0 | if ( projectNameTemplate != null ) |
332 | |
{ |
333 | 0 | if ( addVersionToProjectName || addGroupIdToProjectName ) |
334 | |
{ |
335 | 0 | log.warn( "projectNameTemplate definition overrides " |
336 | |
+ "addVersionToProjectName or addGroupIdToProjectName" ); |
337 | |
} |
338 | 0 | return projectNameTemplate; |
339 | |
} |
340 | 0 | else if ( addVersionToProjectName && addGroupIdToProjectName ) |
341 | |
{ |
342 | 0 | return IdeUtils.PROJECT_NAME_WITH_GROUP_AND_VERSION_TEMPLATE; |
343 | |
} |
344 | 0 | else if ( addVersionToProjectName ) |
345 | |
{ |
346 | 0 | return IdeUtils.PROJECT_NAME_WITH_VERSION_TEMPLATE; |
347 | |
} |
348 | 0 | else if ( addGroupIdToProjectName ) |
349 | |
{ |
350 | 0 | return IdeUtils.PROJECT_NAME_WITH_GROUP_TEMPLATE; |
351 | |
} |
352 | 0 | return IdeUtils.PROJECT_NAME_DEFAULT_TEMPLATE; |
353 | |
} |
354 | |
|
355 | |
|
356 | |
|
357 | |
protected static String getProjectName( String template, IdeDependency dep ) |
358 | |
{ |
359 | 16 | 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 | 0 | return getProjectName( template, artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion() ); |
372 | |
} |
373 | |
|
374 | |
public static String getProjectName( String template, MavenProject project ) |
375 | |
{ |
376 | 0 | return getProjectName( template, project.getGroupId(), project.getArtifactId(), project.getVersion() ); |
377 | |
} |
378 | |
|
379 | |
private static String getProjectName( IdeDependency dep, boolean addVersionToProjectName ) |
380 | |
{ |
381 | 0 | 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 | 0 | 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 | 0 | 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 | 0 | artifactResolver.resolve( artifact, remoteRepos, localRepository ); |
416 | |
} |
417 | 0 | catch ( ArtifactNotFoundException e ) |
418 | |
{ |
419 | |
|
420 | |
} |
421 | 0 | catch ( ArtifactResolutionException e ) |
422 | |
{ |
423 | 0 | String message = |
424 | |
Messages.getString( "IdeUtils.errorresolving", new Object[] { artifact.getClassifier(), |
425 | |
artifact.getId(), e.getMessage() } ); |
426 | |
|
427 | 0 | log.warn( message ); |
428 | 0 | } |
429 | |
|
430 | 0 | 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 | 0 | String type = null; |
451 | |
|
452 | |
|
453 | 0 | if ( "sources".equals( inClassifier ) ) |
454 | |
{ |
455 | 0 | type = "java-source"; |
456 | |
} |
457 | |
else |
458 | |
{ |
459 | 0 | type = inClassifier; |
460 | |
} |
461 | |
|
462 | 0 | String finalClassifier = null; |
463 | 0 | if ( depClassifier == null ) |
464 | |
{ |
465 | 0 | finalClassifier = inClassifier; |
466 | |
} |
467 | 0 | else if ( "sources".equals( inClassifier ) && "tests".equals( depClassifier ) ) |
468 | |
{ |
469 | |
|
470 | 0 | finalClassifier = "test-sources"; |
471 | |
} |
472 | |
else |
473 | |
{ |
474 | 0 | finalClassifier = depClassifier + "-" + inClassifier; |
475 | |
} |
476 | |
|
477 | 0 | return artifactFactory.createArtifactWithClassifier( groupId, artifactId, version, type, finalClassifier ); |
478 | |
} |
479 | |
|
480 | |
public static String resolveJavaVersion( MavenProject project ) |
481 | |
{ |
482 | 0 | String version = IdeUtils.getCompilerTargetVersion( project ); |
483 | 0 | if ( version == null ) |
484 | |
{ |
485 | 0 | version = IdeUtils.getCompilerSourceVersion( project ); |
486 | |
} |
487 | |
|
488 | 0 | if ( "1.5".equals( version ) ) |
489 | |
{ |
490 | 0 | version = IdeUtils.JAVA_5_0; |
491 | |
} |
492 | 0 | else if ( "1.6".equals( version ) ) |
493 | |
{ |
494 | 0 | version = IdeUtils.JAVA_6_0; |
495 | |
} |
496 | 0 | else if ( version != null && version.length() == 1 ) |
497 | |
{ |
498 | 0 | version = version + ".0"; |
499 | |
} |
500 | |
|
501 | 0 | 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 | 88 | if ( !fileToAdd.isAbsolute() ) |
508 | |
{ |
509 | 44 | fileToAdd = new File( basedir, fileToAdd.getPath() ); |
510 | |
} |
511 | |
|
512 | 88 | String basedirPath = getCanonicalPath( basedir ); |
513 | 88 | String absolutePath = getCanonicalPath( fileToAdd ); |
514 | |
|
515 | 88 | String relative = null; |
516 | |
|
517 | 88 | if ( absolutePath.equals( basedirPath ) ) |
518 | |
{ |
519 | 0 | relative = "."; |
520 | |
} |
521 | 88 | else if ( absolutePath.startsWith( basedirPath ) ) |
522 | |
{ |
523 | |
|
524 | |
|
525 | |
|
526 | |
|
527 | |
|
528 | |
|
529 | 84 | int length = basedirPath.length() + 1; |
530 | 84 | if ( basedirPath.endsWith( "\\" ) ) |
531 | |
{ |
532 | 4 | length--; |
533 | |
} |
534 | 84 | relative = absolutePath.substring( length ); |
535 | |
} |
536 | |
else |
537 | |
{ |
538 | 4 | relative = absolutePath; |
539 | |
} |
540 | |
|
541 | 88 | relative = fixSeparator( relative ); |
542 | |
|
543 | 88 | if ( replaceSlashesWithDashes ) |
544 | |
{ |
545 | 8 | relative = StringUtils.replace( relative, '/', '-' ); |
546 | 8 | relative = StringUtils.replace( relative, ':', '-' ); |
547 | |
} |
548 | |
|
549 | 88 | return relative; |
550 | |
} |
551 | |
|
552 | |
|
553 | |
|
554 | |
|
555 | |
|
556 | |
|
557 | |
|
558 | |
public static String fixSeparator( String filename ) |
559 | |
{ |
560 | 124 | 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 | 0 | 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 | 0 | String value = null; |
587 | |
|
588 | 0 | for ( Iterator it = plugins.iterator(); it.hasNext(); ) |
589 | |
{ |
590 | 0 | Plugin plugin = (Plugin) it.next(); |
591 | |
|
592 | 0 | if ( plugin.getArtifactId().equals( ARTIFACT_MAVEN_COMPILER_PLUGIN ) ) |
593 | |
{ |
594 | |
|
595 | 0 | Xpp3Dom o = (Xpp3Dom) plugin.getConfiguration(); |
596 | |
|
597 | |
|
598 | 0 | if ( o != null && o.getChild( optionName ) != null ) |
599 | |
{ |
600 | 0 | value = o.getChild( optionName ).getValue(); |
601 | |
} |
602 | |
|
603 | 0 | List executions = plugin.getExecutions(); |
604 | |
|
605 | |
|
606 | 0 | for ( Iterator iter = executions.iterator(); iter.hasNext(); ) |
607 | |
{ |
608 | 0 | PluginExecution execution = (PluginExecution) iter.next(); |
609 | |
|
610 | |
|
611 | 0 | o = (Xpp3Dom) execution.getConfiguration(); |
612 | |
|
613 | 0 | if ( o != null && o.getChild( optionName ) != null ) |
614 | |
{ |
615 | 0 | value = o.getChild( optionName ).getValue(); |
616 | |
} |
617 | |
} |
618 | |
} |
619 | |
} |
620 | 0 | return value; |
621 | |
} |
622 | |
|
623 | |
private static String getProjectName( String template, String groupId, String artifactId, String version ) |
624 | |
{ |
625 | 16 | String s = template; |
626 | 16 | s = s.replaceAll( "\\[groupId\\]", groupId ); |
627 | 16 | s = s.replaceAll( "\\[artifactId\\]", artifactId ); |
628 | 16 | s = s.replaceAll( "\\[version\\]", version ); |
629 | 16 | return s; |
630 | |
} |
631 | |
|
632 | |
private IdeUtils() |
633 | 0 | { |
634 | |
|
635 | 0 | } |
636 | |
} |