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