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.logging.Log; |
38 | |
import org.apache.maven.project.MavenProject; |
39 | |
import org.codehaus.plexus.util.StringUtils; |
40 | |
import org.codehaus.plexus.util.xml.Xpp3Dom; |
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
public class IdeUtils |
48 | |
{ |
49 | |
public static final String JAVA_1_1 = "1.1"; |
50 | |
|
51 | |
public static final String JAVA_1_2 = "1.2"; |
52 | |
|
53 | |
public static final String JAVA_1_3 = "1.3"; |
54 | |
|
55 | |
public static final String JAVA_1_4 = "1.4"; |
56 | |
|
57 | |
public static final String JAVA_5_0 = "5.0"; |
58 | |
|
59 | |
public static final String JAVA_6_0 = "6.0"; |
60 | |
|
61 | |
public static final String PROJECT_NAME_DEFAULT_TEMPLATE = "[artifactId]"; |
62 | |
|
63 | |
public static final String PROJECT_NAME_WITH_VERSION_TEMPLATE = "[artifactId]-[version]"; |
64 | |
|
65 | |
public static final String PROJECT_NAME_WITH_GROUP_TEMPLATE = "[groupId].[artifactId]"; |
66 | |
|
67 | |
public static final String PROJECT_NAME_WITH_GROUP_AND_VERSION_TEMPLATE = "[groupId].[artifactId]-[version]"; |
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
private static final String ARTIFACT_MAVEN_COMPILER_PLUGIN = "maven-compiler-plugin"; |
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
private static final String PROPERTY_SOURCE = "source"; |
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
private static final String PROPERTY_TARGET = "target"; |
83 | |
|
84 | |
public static String getCanonicalPath( File file ) |
85 | |
throws MojoExecutionException |
86 | |
{ |
87 | |
try |
88 | |
{ |
89 | 34 | return file.getCanonicalPath(); |
90 | |
} |
91 | |
catch ( IOException e ) |
92 | |
{ |
93 | |
throw new MojoExecutionException( Messages.getString( "cantcanonicalize", file |
94 | |
.getAbsolutePath() ), e ); |
95 | |
} |
96 | |
} |
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
public static String getCompilerPluginSetting( MavenProject project, String optionName ) |
105 | |
{ |
106 | |
String value = findCompilerPluginSettingInPlugins( project.getModel().getBuild().getPlugins(), optionName ); |
107 | |
if ( value == null && project.getModel().getBuild().getPluginManagement() != null ) |
108 | |
{ |
109 | |
value = |
110 | |
findCompilerPluginSettingInPlugins( project.getModel().getBuild().getPluginManagement().getPlugins(), |
111 | |
optionName ); |
112 | |
} |
113 | |
return value; |
114 | |
} |
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
public static String getCompilerSourceVersion( MavenProject project ) |
124 | |
{ |
125 | |
return IdeUtils.getCompilerPluginSetting( project, PROPERTY_SOURCE ); |
126 | |
} |
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
public static String getCompilerTargetVersion( MavenProject project ) |
136 | |
{ |
137 | |
return IdeUtils.getCompilerPluginSetting( project, PROPERTY_TARGET ); |
138 | |
} |
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
public static String getArtifactVersion( String[] artifactIds, List dependencies, int len ) |
174 | |
{ |
175 | |
for ( int j = 0; j < artifactIds.length; j++ ) |
176 | |
{ |
177 | |
String id = artifactIds[j]; |
178 | |
Iterator depIter = dependencies.iterator(); |
179 | |
while ( depIter.hasNext() ) |
180 | |
{ |
181 | |
Dependency dep = (Dependency) depIter.next(); |
182 | |
if ( id.equals( dep.getArtifactId() ) ) |
183 | |
{ |
184 | |
return StringUtils.substring( dep.getVersion(), 0, len ); |
185 | |
} |
186 | |
|
187 | |
} |
188 | |
} |
189 | |
return null; |
190 | |
} |
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
public static String getPluginSetting( MavenProject project, String pluginId, String optionName, String defaultValue ) |
203 | |
{ |
204 | 49 | Xpp3Dom dom = getPluginConfigurationDom( project, pluginId ); |
205 | 49 | if ( dom != null && dom.getChild( optionName ) != null ) |
206 | |
{ |
207 | 3 | return dom.getChild( optionName ).getValue(); |
208 | |
} |
209 | 46 | return defaultValue; |
210 | |
} |
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | |
|
217 | |
|
218 | |
|
219 | |
|
220 | |
public static Xpp3Dom getPluginConfigurationDom( MavenProject project, String pluginId ) |
221 | |
{ |
222 | |
|
223 | 49 | Plugin plugin = (org.apache.maven.model.Plugin) project.getBuild().getPluginsAsMap().get( pluginId ); |
224 | 49 | if ( plugin != null ) |
225 | |
{ |
226 | 39 | return (Xpp3Dom) plugin.getConfiguration(); |
227 | |
} |
228 | 10 | return null; |
229 | |
} |
230 | |
|
231 | |
|
232 | |
|
233 | |
|
234 | |
|
235 | |
|
236 | |
|
237 | |
|
238 | |
|
239 | |
public static Xpp3Dom[] getPluginConfigurationDom( MavenProject project, String artifactId, |
240 | |
String[] subConfiguration ) |
241 | |
{ |
242 | |
ArrayList configurationDomList = new ArrayList(); |
243 | |
Xpp3Dom configuration = getPluginConfigurationDom( project, artifactId ); |
244 | |
if ( configuration != null ) |
245 | |
{ |
246 | |
configurationDomList.add( configuration ); |
247 | |
for ( int index = 0; !configurationDomList.isEmpty() && subConfiguration != null && |
248 | |
index < subConfiguration.length; index++ ) |
249 | |
{ |
250 | |
ArrayList newConfigurationDomList = new ArrayList(); |
251 | |
for ( Iterator childElement = configurationDomList.iterator(); childElement.hasNext(); ) |
252 | |
{ |
253 | |
Xpp3Dom child = (Xpp3Dom) childElement.next(); |
254 | |
Xpp3Dom[] deeperChild = child.getChildren( subConfiguration[index] ); |
255 | |
for ( int deeperIndex = 0; deeperIndex < deeperChild.length; deeperIndex++ ) |
256 | |
{ |
257 | |
if ( deeperChild[deeperIndex] != null ) |
258 | |
{ |
259 | |
newConfigurationDomList.add( deeperChild[deeperIndex] ); |
260 | |
} |
261 | |
} |
262 | |
} |
263 | |
configurationDomList = newConfigurationDomList; |
264 | |
} |
265 | |
} |
266 | |
return (Xpp3Dom[]) configurationDomList.toArray( new Xpp3Dom[configurationDomList.size()] ); |
267 | |
} |
268 | |
|
269 | |
public static String getProjectName( String template, IdeDependency dep ) |
270 | |
{ |
271 | 4 | return getProjectName( template, dep.getGroupId(), dep.getArtifactId(), dep.getVersion() ); |
272 | |
} |
273 | |
|
274 | |
|
275 | |
|
276 | |
|
277 | |
|
278 | |
|
279 | |
|
280 | |
|
281 | |
public static String getProjectName( String template, Artifact artifact ) |
282 | |
{ |
283 | |
return getProjectName( template, artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion() ); |
284 | |
} |
285 | |
|
286 | |
public static String getProjectName( String template, MavenProject project ) |
287 | |
{ |
288 | |
return getProjectName( template, project.getGroupId(), project.getArtifactId(), project.getVersion() ); |
289 | |
} |
290 | |
|
291 | |
public static String getProjectName( IdeDependency dep, boolean addVersionToProjectName ) |
292 | |
{ |
293 | |
return getProjectName( addVersionToProjectName ? PROJECT_NAME_WITH_VERSION_TEMPLATE |
294 | |
: PROJECT_NAME_DEFAULT_TEMPLATE, dep ); |
295 | |
} |
296 | |
|
297 | |
public static String getProjectName( MavenProject project, boolean addVersionToProjectName ) |
298 | |
{ |
299 | |
return getProjectName( addVersionToProjectName ? PROJECT_NAME_WITH_VERSION_TEMPLATE |
300 | |
: PROJECT_NAME_DEFAULT_TEMPLATE, project ); |
301 | |
} |
302 | |
|
303 | |
public static Artifact resolveArtifactWithClassifier( String groupId, String artifactId, String version, |
304 | |
String classifier, ArtifactRepository localRepository, |
305 | |
ArtifactResolver artifactResolver, |
306 | |
ArtifactFactory artifactFactory, List remoteRepos, Log log ) |
307 | |
|
308 | |
{ |
309 | |
String type = classifier; |
310 | |
|
311 | |
|
312 | |
if ( "sources".equals( type ) || "test-sources".equals( type ) ) |
313 | |
{ |
314 | |
type = "java-source"; |
315 | |
} |
316 | |
|
317 | |
Artifact resolvedArtifact = |
318 | |
artifactFactory.createArtifactWithClassifier( groupId, artifactId, version, type, classifier ); |
319 | |
|
320 | |
try |
321 | |
{ |
322 | |
artifactResolver.resolve( resolvedArtifact, remoteRepos, localRepository ); |
323 | |
} |
324 | |
catch ( ArtifactNotFoundException e ) |
325 | |
{ |
326 | |
|
327 | |
} |
328 | |
catch ( ArtifactResolutionException e ) |
329 | |
{ |
330 | |
String message = Messages.getString( "errorresolving", new Object[] { |
331 | |
classifier, resolvedArtifact.getId(), e.getMessage() } ); |
332 | |
|
333 | |
log.warn( message ); |
334 | |
} |
335 | |
|
336 | |
return resolvedArtifact; |
337 | |
} |
338 | |
|
339 | |
public static String resolveJavaVersion( MavenProject project ) |
340 | |
{ |
341 | |
String version = IdeUtils.getCompilerTargetVersion( project ); |
342 | |
if ( version == null ) |
343 | |
{ |
344 | |
version = IdeUtils.getCompilerSourceVersion( project ); |
345 | |
} |
346 | |
|
347 | |
if ( "1.5".equals( version ) ) |
348 | |
{ |
349 | |
version = IdeUtils.JAVA_5_0; |
350 | |
} |
351 | |
else if ( "1.6".equals( version ) ) |
352 | |
{ |
353 | |
version = IdeUtils.JAVA_6_0; |
354 | |
} |
355 | |
else if ( version != null && version.length() == 1 ) |
356 | |
{ |
357 | |
version = version + ".0"; |
358 | |
} |
359 | |
|
360 | |
return version == null ? IdeUtils.JAVA_1_4 : version; |
361 | |
} |
362 | |
|
363 | |
public static String toRelativeAndFixSeparator( File basedir, File fileToAdd, boolean replaceSlashesWithDashes ) |
364 | |
throws MojoExecutionException |
365 | |
{ |
366 | 17 | if ( !fileToAdd.isAbsolute() ) |
367 | |
{ |
368 | 9 | fileToAdd = new File( basedir, fileToAdd.getPath() ); |
369 | |
} |
370 | |
|
371 | |
String basedirpath; |
372 | |
String absolutePath; |
373 | |
|
374 | 17 | basedirpath = getCanonicalPath( basedir ); |
375 | 17 | absolutePath = getCanonicalPath( fileToAdd ); |
376 | |
|
377 | |
String relative; |
378 | |
|
379 | 17 | if ( absolutePath.equals( basedirpath ) ) |
380 | |
{ |
381 | |
relative = "."; |
382 | |
} |
383 | 17 | else if ( absolutePath.startsWith( basedirpath ) ) |
384 | |
{ |
385 | 17 | relative = absolutePath.substring( basedirpath.length() + 1 ); |
386 | |
} |
387 | |
else |
388 | |
{ |
389 | |
relative = absolutePath; |
390 | |
} |
391 | |
|
392 | 17 | relative = StringUtils.replace( relative, '\\', '/' ); |
393 | |
|
394 | 17 | if ( replaceSlashesWithDashes ) |
395 | |
{ |
396 | 2 | relative = StringUtils.replace( relative, '/', '-' ); |
397 | 2 | relative = StringUtils.replace( relative, ':', '-' ); |
398 | |
} |
399 | |
|
400 | 17 | return relative; |
401 | |
} |
402 | |
|
403 | |
|
404 | |
|
405 | |
|
406 | |
|
407 | |
|
408 | |
|
409 | |
private static String findCompilerPluginSettingInPlugins( List plugins, String optionName ) |
410 | |
{ |
411 | |
String value = null; |
412 | |
|
413 | |
for ( Iterator it = plugins.iterator(); it.hasNext(); ) |
414 | |
{ |
415 | |
Plugin plugin = (Plugin) it.next(); |
416 | |
|
417 | |
if ( plugin.getArtifactId().equals( ARTIFACT_MAVEN_COMPILER_PLUGIN ) ) |
418 | |
{ |
419 | |
Xpp3Dom o = (Xpp3Dom) plugin.getConfiguration(); |
420 | |
|
421 | |
|
422 | |
if ( o != null && o.getChild( optionName ) != null ) |
423 | |
{ |
424 | |
value = o.getChild( optionName ).getValue(); |
425 | |
} |
426 | |
|
427 | |
List executions = plugin.getExecutions(); |
428 | |
|
429 | |
|
430 | |
for ( Iterator iter = executions.iterator(); iter.hasNext(); ) |
431 | |
{ |
432 | |
PluginExecution execution = (PluginExecution) iter.next(); |
433 | |
o = (Xpp3Dom) execution.getConfiguration(); |
434 | |
|
435 | |
if ( o != null && o.getChild( optionName ) != null ) |
436 | |
{ |
437 | |
value = o.getChild( optionName ).getValue(); |
438 | |
} |
439 | |
} |
440 | |
} |
441 | |
} |
442 | |
return value; |
443 | |
} |
444 | |
|
445 | |
private static String getProjectName( String template, String groupId, String artifactId, String version ) |
446 | |
{ |
447 | 4 | String s = template; |
448 | 4 | s = s.replaceAll( "\\[groupId\\]", groupId ); |
449 | 4 | s = s.replaceAll( "\\[artifactId\\]", artifactId ); |
450 | 4 | s = s.replaceAll( "\\[version\\]", version ); |
451 | 4 | return s; |
452 | |
} |
453 | |
|
454 | |
private IdeUtils() |
455 | |
{ |
456 | |
|
457 | |
} |
458 | |
} |