1 | |
package org.apache.maven.plugin.assembly.utils; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import org.apache.maven.artifact.Artifact; |
23 | |
import org.apache.maven.execution.MavenSession; |
24 | |
import org.apache.maven.plugin.assembly.AssemblerConfigurationSource; |
25 | |
import org.apache.maven.plugin.assembly.format.AssemblyFormattingException; |
26 | |
import org.apache.maven.plugin.assembly.model.Assembly; |
27 | |
import org.apache.maven.project.MavenProject; |
28 | |
import org.codehaus.plexus.interpolation.InterpolationException; |
29 | |
import org.codehaus.plexus.interpolation.PrefixedObjectValueSource; |
30 | |
import org.codehaus.plexus.interpolation.PrefixedPropertiesValueSource; |
31 | |
import org.codehaus.plexus.interpolation.PropertiesBasedValueSource; |
32 | |
import org.codehaus.plexus.interpolation.StringSearchInterpolator; |
33 | |
import org.codehaus.plexus.util.StringUtils; |
34 | |
|
35 | |
import java.io.IOException; |
36 | |
import java.util.ArrayList; |
37 | |
import java.util.Arrays; |
38 | |
import java.util.Collections; |
39 | |
import java.util.List; |
40 | |
import java.util.ListIterator; |
41 | |
import java.util.Properties; |
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
public final class AssemblyFormatUtils |
47 | |
{ |
48 | |
|
49 | |
private AssemblyFormatUtils() |
50 | 0 | { |
51 | 0 | } |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
public static String getDistributionName( final Assembly assembly, final AssemblerConfigurationSource configSource ) |
60 | |
{ |
61 | 2 | final String finalName = configSource.getFinalName(); |
62 | 2 | final boolean appendAssemblyId = configSource.isAssemblyIdAppended(); |
63 | 2 | final String classifier = configSource.getClassifier(); |
64 | |
|
65 | 2 | String distributionName = finalName; |
66 | 2 | if ( appendAssemblyId ) |
67 | |
{ |
68 | 0 | if ( !StringUtils.isEmpty( assembly.getId() ) ) |
69 | |
{ |
70 | 0 | distributionName = finalName + "-" + assembly.getId(); |
71 | |
} |
72 | |
} |
73 | 2 | else if ( classifier != null ) |
74 | |
{ |
75 | 1 | distributionName = finalName + "-" + classifier; |
76 | |
} |
77 | |
|
78 | 2 | return distributionName; |
79 | |
} |
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
@Deprecated |
87 | |
public static String getOutputDirectory( final String output, final MavenProject mainProject, |
88 | |
final MavenProject artifactProject, final String finalName ) |
89 | |
throws AssemblyFormattingException |
90 | |
{ |
91 | 0 | return getOutputDirectory( output, mainProject, null, artifactProject, finalName, null ); |
92 | |
} |
93 | |
|
94 | |
public static String getOutputDirectory( final String output, final MavenProject mainProject, |
95 | |
final MavenProject artifactProject, final String finalName, |
96 | |
final AssemblerConfigurationSource configSource ) |
97 | |
throws AssemblyFormattingException |
98 | |
{ |
99 | 12 | return getOutputDirectory( output, mainProject, null, artifactProject, finalName, configSource ); |
100 | |
} |
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
public static String getOutputDirectory( final String output, final MavenProject mainProject, |
129 | |
final MavenProject moduleProject, final MavenProject artifactProject, |
130 | |
final String finalName, final AssemblerConfigurationSource configSource ) |
131 | |
throws AssemblyFormattingException |
132 | |
{ |
133 | 71 | String value = output; |
134 | 71 | if ( value == null ) |
135 | |
{ |
136 | 9 | value = ""; |
137 | |
} |
138 | |
|
139 | 71 | final StringSearchInterpolator interpolator = new StringSearchInterpolator(); |
140 | |
|
141 | 71 | final Properties specialExpressionOverrides = new Properties(); |
142 | |
|
143 | 71 | if ( finalName != null ) |
144 | |
{ |
145 | 43 | specialExpressionOverrides.setProperty( "finalName", finalName ); |
146 | 43 | specialExpressionOverrides.setProperty( "build.finalName", finalName ); |
147 | |
} |
148 | |
|
149 | |
|
150 | 71 | interpolator.addValueSource( new PropertiesBasedValueSource( specialExpressionOverrides ) ); |
151 | |
|
152 | 71 | if ( moduleProject != null ) |
153 | |
{ |
154 | |
|
155 | 45 | interpolator.addValueSource( new PrefixedObjectValueSource( "module.", moduleProject ) ); |
156 | 45 | interpolator.addValueSource( new PrefixedPropertiesValueSource( "module.properties.", |
157 | |
moduleProject.getProperties() ) ); |
158 | 45 | if ( moduleProject.getArtifact() != null ) |
159 | |
{ |
160 | 12 | interpolator.addValueSource( new PrefixedObjectValueSource( "module.", moduleProject.getArtifact() ) ); |
161 | |
} |
162 | |
} |
163 | |
|
164 | 71 | if ( artifactProject != null ) |
165 | |
{ |
166 | |
|
167 | 55 | interpolator.addValueSource( new PrefixedObjectValueSource( "artifact.", artifactProject ) ); |
168 | 55 | interpolator.addValueSource( new PrefixedPropertiesValueSource( "artifact.properties.", |
169 | |
artifactProject.getProperties() ) ); |
170 | 55 | if ( artifactProject.getArtifact() != null ) |
171 | |
{ |
172 | 14 | interpolator.addValueSource( new PrefixedObjectValueSource( "artifact.", artifactProject.getArtifact() ) ); |
173 | |
} |
174 | |
} |
175 | |
|
176 | 71 | MavenSession session = null; |
177 | |
|
178 | 71 | if ( configSource != null ) |
179 | |
{ |
180 | 71 | session = configSource.getMavenSession(); |
181 | |
|
182 | 71 | if ( session != null ) |
183 | |
{ |
184 | 32 | Properties userProperties = null; |
185 | |
try |
186 | |
{ |
187 | 32 | userProperties = session.getExecutionProperties(); |
188 | |
} |
189 | 0 | catch ( final NoSuchMethodError nsmer ) |
190 | |
{ |
191 | |
|
192 | 32 | } |
193 | |
|
194 | 32 | if ( userProperties != null ) |
195 | |
{ |
196 | |
|
197 | 32 | interpolator.addValueSource( new PropertiesBasedValueSource( userProperties ) ); |
198 | |
} |
199 | |
} |
200 | |
} |
201 | |
|
202 | 71 | if ( mainProject != null ) |
203 | |
{ |
204 | |
|
205 | 60 | interpolator.addValueSource( new PrefixedObjectValueSource( InterpolationConstants.PROJECT_PREFIXES, |
206 | |
mainProject, true ) ); |
207 | |
|
208 | |
|
209 | 60 | interpolator.addValueSource( new PrefixedPropertiesValueSource( |
210 | |
InterpolationConstants.PROJECT_PROPERTIES_PREFIXES, |
211 | |
mainProject.getProperties(), true ) ); |
212 | |
} |
213 | |
|
214 | 71 | Properties commandLineProperties = System.getProperties(); |
215 | 71 | if ( session != null ) |
216 | |
{ |
217 | 32 | commandLineProperties = new Properties(); |
218 | 32 | if ( session.getExecutionProperties() != null ) |
219 | |
{ |
220 | 32 | commandLineProperties.putAll( session.getExecutionProperties() ); |
221 | |
} |
222 | |
|
223 | 32 | if ( session.getUserProperties() != null ) |
224 | |
{ |
225 | 32 | commandLineProperties.putAll( session.getUserProperties() ); |
226 | |
} |
227 | |
} |
228 | |
|
229 | |
|
230 | 71 | interpolator.addValueSource( new PropertiesBasedValueSource( commandLineProperties ) ); |
231 | |
|
232 | |
try |
233 | |
{ |
234 | |
|
235 | 71 | interpolator.addValueSource( new PrefixedPropertiesValueSource( Collections.singletonList( "env." ), |
236 | |
CommandLineUtils.getSystemEnvVars( false ), |
237 | |
true ) ); |
238 | |
} |
239 | 0 | catch ( final IOException e ) |
240 | |
{ |
241 | 0 | throw new AssemblyFormattingException( "Failed to retrieve OS environment variables. Reason: " |
242 | |
+ e.getMessage(), e ); |
243 | 71 | } |
244 | |
|
245 | |
try |
246 | |
{ |
247 | 71 | value = interpolator.interpolate( value ); |
248 | |
} |
249 | 0 | catch ( final InterpolationException e ) |
250 | |
{ |
251 | 0 | throw new AssemblyFormattingException( "Failed to interpolate output directory. Reason: " + e.getMessage(), |
252 | |
e ); |
253 | 71 | } |
254 | |
|
255 | 71 | if ( ( value.length() > 0 ) && !value.endsWith( "/" ) && !value.endsWith( "\\" ) ) |
256 | |
{ |
257 | 46 | value += "/"; |
258 | |
} |
259 | |
|
260 | 71 | if ( ( value.length() > 0 ) && ( value.startsWith( "/" ) || value.startsWith( "\\" ) ) ) |
261 | |
{ |
262 | 5 | value = value.substring( 1 ); |
263 | |
} |
264 | |
|
265 | 71 | value = StringUtils.replace( value, "//", "/" ); |
266 | 71 | value = StringUtils.replace( value, "\\\\", "\\" ); |
267 | 71 | value = fixRelativeRefs( value ); |
268 | |
|
269 | 71 | return value; |
270 | |
} |
271 | |
|
272 | |
|
273 | |
|
274 | |
|
275 | |
|
276 | |
|
277 | |
@Deprecated |
278 | |
public static String evaluateFileNameMapping( final String expression, final Artifact artifact, |
279 | |
final MavenProject mainProject, final MavenProject artifactProject ) |
280 | |
throws AssemblyFormattingException |
281 | |
{ |
282 | 0 | return evaluateFileNameMapping( expression, artifact, mainProject, null, null, artifactProject, null ); |
283 | |
} |
284 | |
|
285 | |
public static String evaluateFileNameMapping( final String expression, final Artifact artifact, |
286 | |
final MavenProject mainProject, final MavenProject artifactProject, |
287 | |
final AssemblerConfigurationSource configSource ) |
288 | |
throws AssemblyFormattingException |
289 | |
{ |
290 | 1 | return evaluateFileNameMapping( expression, artifact, mainProject, null, null, artifactProject, configSource ); |
291 | |
} |
292 | |
|
293 | |
|
294 | |
|
295 | |
|
296 | |
|
297 | |
|
298 | |
|
299 | |
|
300 | |
|
301 | |
|
302 | |
|
303 | |
|
304 | |
|
305 | |
|
306 | |
|
307 | |
|
308 | |
|
309 | |
|
310 | |
|
311 | |
|
312 | |
|
313 | |
|
314 | |
|
315 | |
|
316 | |
|
317 | |
|
318 | |
|
319 | |
|
320 | |
|
321 | |
|
322 | |
|
323 | |
|
324 | |
|
325 | |
|
326 | |
|
327 | |
public static String evaluateFileNameMapping( final String expression, final Artifact artifact, |
328 | |
final MavenProject mainProject, final MavenProject moduleProject, |
329 | |
final Artifact moduleArtifact, final MavenProject artifactProject, |
330 | |
final AssemblerConfigurationSource configSource ) |
331 | |
throws AssemblyFormattingException |
332 | |
{ |
333 | 46 | String value = expression; |
334 | |
|
335 | |
|
336 | |
|
337 | 46 | artifact.isSnapshot(); |
338 | |
|
339 | 46 | final StringSearchInterpolator interpolator = new StringSearchInterpolator(); |
340 | |
|
341 | 46 | if ( moduleArtifact != null ) |
342 | |
{ |
343 | |
|
344 | 39 | interpolator.addValueSource( new PrefixedObjectValueSource( "module.", moduleArtifact ) ); |
345 | |
|
346 | |
|
347 | 39 | interpolator.addValueSource( new PrefixedObjectValueSource( "module.", moduleArtifact.getArtifactHandler() ) ); |
348 | 39 | interpolator.addValueSource( new PrefixedObjectValueSource( "module.handler.", |
349 | |
moduleArtifact.getArtifactHandler() ) ); |
350 | |
} |
351 | |
|
352 | |
|
353 | 46 | if ( moduleProject != null ) |
354 | |
{ |
355 | 39 | interpolator.addValueSource( new PrefixedObjectValueSource( "module.", moduleProject ) ); |
356 | 39 | interpolator.addValueSource( new PrefixedPropertiesValueSource( "module.properties.", |
357 | |
moduleProject.getProperties() ) ); |
358 | 39 | if ( moduleProject.getArtifact() != null ) |
359 | |
{ |
360 | 11 | interpolator.addValueSource( new PrefixedObjectValueSource( "module.", moduleProject.getArtifact() ) ); |
361 | |
} |
362 | |
} |
363 | |
|
364 | |
|
365 | 46 | interpolator.addValueSource( new PrefixedObjectValueSource( "artifact.", artifact ) ); |
366 | |
|
367 | |
|
368 | 46 | interpolator.addValueSource( new PrefixedObjectValueSource( "artifact.", artifact.getArtifactHandler() ) ); |
369 | 46 | interpolator.addValueSource( new PrefixedObjectValueSource( "artifact.handler.", artifact.getArtifactHandler() ) ); |
370 | |
|
371 | |
|
372 | 46 | if ( artifactProject != null ) |
373 | |
{ |
374 | 45 | interpolator.addValueSource( new PrefixedObjectValueSource( "artifact.", artifactProject ) ); |
375 | 45 | interpolator.addValueSource( new PrefixedPropertiesValueSource( "artifact.properties.", |
376 | |
artifactProject.getProperties() ) ); |
377 | 45 | if ( artifactProject.getArtifact() != null ) |
378 | |
{ |
379 | 14 | interpolator.addValueSource( new PrefixedObjectValueSource( "artifact.", artifactProject.getArtifact() ) ); |
380 | |
} |
381 | |
} |
382 | |
|
383 | 46 | if ( mainProject != null ) |
384 | |
{ |
385 | |
|
386 | |
|
387 | 38 | interpolator.addValueSource( new PrefixedObjectValueSource( InterpolationConstants.PROJECT_PREFIXES, |
388 | |
mainProject, true ) ); |
389 | |
} |
390 | |
|
391 | 46 | final Properties specialRules = new Properties(); |
392 | |
|
393 | 46 | final String classifier = artifact.getClassifier(); |
394 | 46 | if ( classifier != null ) |
395 | |
{ |
396 | 3 | specialRules.setProperty( "dashClassifier?", "-" + classifier ); |
397 | 3 | specialRules.setProperty( "dashClassifier", "-" + classifier ); |
398 | |
} |
399 | |
else |
400 | |
{ |
401 | 43 | specialRules.setProperty( "dashClassifier?", "" ); |
402 | 43 | specialRules.setProperty( "dashClassifier", "" ); |
403 | |
} |
404 | |
|
405 | |
|
406 | 46 | interpolator.addValueSource( new PropertiesBasedValueSource( specialRules ) ); |
407 | |
|
408 | 46 | MavenSession session = null; |
409 | 46 | if ( configSource != null ) |
410 | |
{ |
411 | 46 | session = configSource.getMavenSession(); |
412 | |
|
413 | 46 | if ( session != null ) |
414 | |
{ |
415 | 28 | Properties userProperties = null; |
416 | |
try |
417 | |
{ |
418 | 28 | userProperties = session.getExecutionProperties(); |
419 | |
} |
420 | 0 | catch ( final NoSuchMethodError nsmer ) |
421 | |
{ |
422 | |
|
423 | 28 | } |
424 | |
|
425 | 28 | if ( userProperties != null ) |
426 | |
{ |
427 | |
|
428 | 27 | interpolator.addValueSource( new PropertiesBasedValueSource( userProperties ) ); |
429 | |
} |
430 | |
} |
431 | |
} |
432 | |
|
433 | 46 | if ( mainProject != null ) |
434 | |
{ |
435 | |
|
436 | 38 | interpolator.addValueSource( new PrefixedPropertiesValueSource( |
437 | |
InterpolationConstants.PROJECT_PROPERTIES_PREFIXES, |
438 | |
mainProject.getProperties(), true ) ); |
439 | |
} |
440 | |
|
441 | 46 | Properties commandLineProperties = System.getProperties(); |
442 | 46 | if ( session != null ) |
443 | |
{ |
444 | 28 | commandLineProperties = new Properties(); |
445 | 28 | if ( session.getExecutionProperties() != null ) |
446 | |
{ |
447 | 27 | commandLineProperties.putAll( session.getExecutionProperties() ); |
448 | |
} |
449 | |
|
450 | 28 | if ( session.getUserProperties() != null ) |
451 | |
{ |
452 | 28 | commandLineProperties.putAll( session.getUserProperties() ); |
453 | |
} |
454 | |
} |
455 | |
|
456 | |
|
457 | 46 | interpolator.addValueSource( new PropertiesBasedValueSource( commandLineProperties ) ); |
458 | |
|
459 | |
try |
460 | |
{ |
461 | |
|
462 | 46 | interpolator.addValueSource( new PrefixedPropertiesValueSource( Collections.singletonList( "env." ), |
463 | |
CommandLineUtils.getSystemEnvVars( false ), |
464 | |
true ) ); |
465 | |
} |
466 | 0 | catch ( final IOException e ) |
467 | |
{ |
468 | 0 | throw new AssemblyFormattingException( "Failed to retrieve OS environment variables. Reason: " |
469 | |
+ e.getMessage(), e ); |
470 | 46 | } |
471 | |
|
472 | |
try |
473 | |
{ |
474 | 46 | value = interpolator.interpolate( value ); |
475 | |
} |
476 | 0 | catch ( final InterpolationException e ) |
477 | |
{ |
478 | 0 | throw new AssemblyFormattingException( "Failed to interpolate output filename mapping. Reason: " |
479 | |
+ e.getMessage(), e ); |
480 | 46 | } |
481 | |
|
482 | 46 | value = StringUtils.replace( value, "//", "/" ); |
483 | 46 | value = StringUtils.replace( value, "\\\\", "\\" ); |
484 | 46 | value = fixRelativeRefs( value ); |
485 | |
|
486 | 46 | return value; |
487 | |
} |
488 | |
|
489 | |
public static String fixRelativeRefs( String src ) |
490 | |
{ |
491 | 127 | String value = src; |
492 | |
|
493 | 127 | String[] separators = { |
494 | |
"/", "\\" |
495 | |
}; |
496 | |
|
497 | 127 | String finalSep = null; |
498 | 381 | for ( String sep : separators ) |
499 | |
{ |
500 | 254 | if ( value.endsWith( sep ) ) |
501 | |
{ |
502 | 66 | finalSep = sep; |
503 | |
} |
504 | |
|
505 | 254 | if ( value.indexOf( "." + sep ) > -1 ) |
506 | |
{ |
507 | 12 | List<String> parts = new ArrayList<String>(); |
508 | 12 | parts.addAll( Arrays.asList( value.split( sep ) ) ); |
509 | |
|
510 | 12 | for ( ListIterator<String> it = parts.listIterator(); it.hasNext(); ) |
511 | |
{ |
512 | 30 | String part = it.next(); |
513 | 30 | if ( ".".equals( part ) ) |
514 | |
{ |
515 | 6 | it.remove(); |
516 | |
} |
517 | 24 | else if ( "..".equals( part ) ) |
518 | |
{ |
519 | 6 | it.remove(); |
520 | 6 | if ( it.hasPrevious() ) |
521 | |
{ |
522 | 3 | it.previous(); |
523 | 3 | it.remove(); |
524 | |
} |
525 | |
} |
526 | 30 | } |
527 | |
|
528 | 12 | value = StringUtils.join( parts.iterator(), sep ); |
529 | |
} |
530 | |
} |
531 | |
|
532 | 127 | if ( finalSep != null && value.length() > 0 && !value.endsWith( finalSep ) ) |
533 | |
{ |
534 | 12 | value += finalSep; |
535 | |
} |
536 | |
|
537 | 127 | return value; |
538 | |
} |
539 | |
} |