1 package org.apache.maven.plugins.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.plugins.assembly.AssemblerConfigurationSource;
25 import org.apache.maven.plugins.assembly.format.AssemblyFormattingException;
26 import org.apache.maven.plugins.assembly.model.Assembly;
27 import org.apache.maven.project.MavenProject;
28 import org.apache.maven.shared.utils.Os;
29 import org.codehaus.plexus.interpolation.fixed.FixedStringSearchInterpolator;
30 import org.codehaus.plexus.interpolation.fixed.PrefixedObjectValueSource;
31 import org.codehaus.plexus.interpolation.fixed.PrefixedPropertiesValueSource;
32 import org.codehaus.plexus.interpolation.fixed.PropertiesBasedValueSource;
33 import org.codehaus.plexus.logging.Logger;
34 import org.codehaus.plexus.util.StringUtils;
35
36 import javax.annotation.Nonnull;
37 import javax.annotation.Nullable;
38 import java.util.ArrayList;
39 import java.util.Arrays;
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 {
52 }
53
54
55
56
57
58
59
60 public static String getDistributionName( final Assembly assembly, final AssemblerConfigurationSource configSource )
61 {
62 final String finalName = configSource.getFinalName();
63 final boolean appendAssemblyId = configSource.isAssemblyIdAppended();
64
65 String distributionName = finalName;
66 if ( appendAssemblyId )
67 {
68 distributionName = finalName + "-" + assembly.getId();
69 }
70
71 return distributionName;
72 }
73
74
75 @Nonnull
76 public static FixedStringSearchInterpolator finalNameInterpolator( String finalName )
77 {
78 final Properties specialExpressionOverrides = new Properties();
79
80 if ( finalName != null )
81 {
82 specialExpressionOverrides.setProperty( "finalName", finalName );
83 specialExpressionOverrides.setProperty( "build.finalName", finalName );
84 }
85 else
86 {
87 return FixedStringSearchInterpolator.empty();
88 }
89
90 return FixedStringSearchInterpolator.create( new PropertiesBasedValueSource( specialExpressionOverrides ) );
91 }
92
93 @Nonnull
94 public static FixedStringSearchInterpolator moduleProjectInterpolator( final MavenProject moduleProject )
95 {
96 if ( moduleProject != null )
97 {
98 return FixedStringSearchInterpolator.createWithPermittedNulls(
99 new PrefixedObjectValueSource( "module.", moduleProject ),
100 new PrefixedPropertiesValueSource( "module.properties.", moduleProject.getProperties() ),
101 moduleProject.getArtifact() != null
102 ? new PrefixedObjectValueSource( "module.", moduleProject.getArtifact() )
103 : null );
104 }
105 else
106 {
107 return FixedStringSearchInterpolator.empty();
108 }
109
110 }
111
112 public static FixedStringSearchInterpolator moduleArtifactInterpolator( Artifact moduleArtifact )
113 {
114 if ( moduleArtifact != null )
115 {
116
117 return FixedStringSearchInterpolator.create( new PrefixedObjectValueSource( "module.", moduleArtifact ),
118 new PrefixedObjectValueSource( "module.",
119 moduleArtifact
120 .getArtifactHandler() ),
121 new PrefixedObjectValueSource( "module.handler.",
122 moduleArtifact
123 .getArtifactHandler() ) );
124
125 }
126 else
127 {
128 return FixedStringSearchInterpolator.empty();
129 }
130
131 }
132
133 @Nonnull
134 public static FixedStringSearchInterpolator artifactProjectInterpolator( final MavenProject artifactProject )
135 {
136 if ( artifactProject != null )
137 {
138 PrefixedObjectValueSource vs = null;
139 if ( artifactProject.getArtifact() != null )
140 {
141 vs = new PrefixedObjectValueSource( "artifact.", artifactProject.getArtifact() );
142 }
143
144 return FixedStringSearchInterpolator.createWithPermittedNulls(
145 new PrefixedObjectValueSource( "artifact.", artifactProject ),
146 new PrefixedPropertiesValueSource( "artifact.properties.", artifactProject.getProperties() ), vs );
147
148
149 }
150 else
151 {
152 return FixedStringSearchInterpolator.empty();
153 }
154
155
156 }
157
158 @Nonnull
159 public static FixedStringSearchInterpolator artifactInterpolator( @Nonnull final Artifact artifact )
160 {
161 return FixedStringSearchInterpolator.create( new PrefixedObjectValueSource( "artifact.", artifact ),
162 new PrefixedObjectValueSource( "artifact.",
163 artifact.getArtifactHandler() ),
164 new PrefixedObjectValueSource( "artifact.handler.",
165 artifact.getArtifactHandler() ) );
166 }
167
168
169 @Nonnull
170 public static FixedStringSearchInterpolator classifierRules( final Artifact artifact )
171 {
172 final Properties specialRules = new Properties();
173
174 final String classifier = ProjectUtils.getClassifier( artifact );
175 if ( classifier != null )
176 {
177 specialRules.setProperty( "dashClassifier?", "-" + classifier );
178 specialRules.setProperty( "dashClassifier", "-" + classifier );
179 }
180 else
181 {
182 specialRules.setProperty( "dashClassifier?", "" );
183 specialRules.setProperty( "dashClassifier", "" );
184 }
185
186 return FixedStringSearchInterpolator.create( new PropertiesBasedValueSource( specialRules ) );
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 public static String getOutputDirectory( final String output, final MavenProject artifactProject,
217 final String finalName, final AssemblerConfigurationSource configSource )
218 throws AssemblyFormattingException
219 {
220 return getOutputDirectory( output, finalName, configSource, moduleProjectInterpolator( null ),
221 artifactProjectInterpolator( artifactProject ) );
222 }
223
224
225 private static FixedStringSearchInterpolator executionPropertiesInterpolator(
226 AssemblerConfigurationSource configSource )
227 {
228 MavenSession session;
229
230 if ( configSource != null )
231 {
232 session = configSource.getMavenSession();
233
234 if ( session != null )
235 {
236 Properties userProperties = session.getExecutionProperties();
237
238 if ( userProperties != null )
239 {
240 return FixedStringSearchInterpolator.create( new PropertiesBasedValueSource( userProperties ) );
241 }
242 }
243 }
244 return FixedStringSearchInterpolator.empty();
245 }
246
247 private static FixedStringSearchInterpolator mainProjectOnlyInterpolator( MavenProject mainProject )
248 {
249 if ( mainProject != null )
250 {
251
252 return FixedStringSearchInterpolator.create(
253 new org.codehaus.plexus.interpolation.fixed.PrefixedObjectValueSource(
254 InterpolationConstants.PROJECT_PREFIXES, mainProject, true ) );
255 }
256 else
257 {
258 return FixedStringSearchInterpolator.empty();
259 }
260 }
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299 @Nonnull
300 public static String fixRelativeRefs( @Nonnull String src )
301 {
302 String value = src;
303
304 String[] separators = { "/", "\\" };
305
306 String finalSep = null;
307 for ( String sep : separators )
308 {
309 if ( value.endsWith( sep ) )
310 {
311 finalSep = sep;
312 }
313
314 if ( value.contains( "." + sep ) )
315 {
316 List<String> parts = new ArrayList<String>();
317 parts.addAll( Arrays.asList( value.split( sep.replace( "\\", "\\\\" ) ) ) );
318
319 for ( ListIterator<String> it = parts.listIterator(); it.hasNext(); )
320 {
321 String part = it.next();
322 if ( ".".equals( part ) )
323 {
324 it.remove();
325 }
326 else if ( "..".equals( part ) )
327 {
328 it.remove();
329 if ( it.hasPrevious() )
330 {
331 it.previous();
332 it.remove();
333 }
334 }
335 }
336
337 value = StringUtils.join( parts.iterator(), sep );
338 }
339 }
340
341 if ( finalSep != null && value.length() > 0 && !value.endsWith( finalSep ) )
342 {
343 value += finalSep;
344 }
345
346 return value;
347 }
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384 public static String evaluateFileNameMapping( final String expression, @Nonnull final Artifact artifact,
385 @Nullable final MavenProject mainProject,
386 @Nullable final Artifact moduleArtifact,
387 @Nonnull final AssemblerConfigurationSource configSource,
388 FixedStringSearchInterpolator moduleProjectInterpolator,
389 FixedStringSearchInterpolator artifactProjectInterpolator )
390 throws AssemblyFormattingException
391 {
392 String value = expression;
393
394 final FixedStringSearchInterpolator interpolator =
395 FixedStringSearchInterpolator.create( moduleArtifactInterpolator( moduleArtifact ),
396 moduleProjectInterpolator, artifactInterpolator( artifact ),
397 artifactProjectInterpolator,
398 mainProjectOnlyInterpolator( mainProject ),
399 classifierRules( artifact ),
400 executionPropertiesInterpolator( configSource ),
401 configSource.getMainProjectInterpolator(),
402 configSource.getCommandLinePropsInterpolator(),
403 configSource.getEnvInterpolator() );
404
405 value = interpolator.interpolate( value );
406
407 value = StringUtils.replace( value, "//", "/" );
408 value = StringUtils.replace( value, "\\\\", "\\" );
409 value = fixRelativeRefs( value );
410
411 return value;
412 }
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440 public static String getOutputDirectory( final String output, final String finalName,
441 final AssemblerConfigurationSource configSource,
442 FixedStringSearchInterpolator moduleProjectIntrpolator,
443 FixedStringSearchInterpolator artifactProjectInterpolator )
444 throws AssemblyFormattingException
445 {
446 String value = output;
447 if ( value == null )
448 {
449 value = "";
450 }
451
452 final FixedStringSearchInterpolator interpolator =
453 FixedStringSearchInterpolator.create( finalNameInterpolator( finalName ), moduleProjectIntrpolator,
454 artifactProjectInterpolator,
455 executionPropertiesInterpolator( configSource ),
456 configSource.getMainProjectInterpolator(),
457 configSource.getCommandLinePropsInterpolator(),
458 configSource.getEnvInterpolator() );
459
460 value = interpolator.interpolate( value );
461
462 if ( ( value.length() > 0 ) && !value.endsWith( "/" ) && !value.endsWith( "\\" ) )
463 {
464 value += "/";
465 }
466
467 if ( ( value.length() > 0 ) && ( value.startsWith( "/" ) || value.startsWith( "\\" ) ) )
468 {
469 value = value.substring( 1 );
470 }
471
472 value = StringUtils.replace( value, "//", "/" );
473 value = StringUtils.replace( value, "\\\\", "\\" );
474 value = fixRelativeRefs( value );
475
476 return value;
477 }
478
479 public static void warnForPlatformSpecifics( Logger logger, String destDirectory )
480 {
481 if ( Os.isFamily( Os.FAMILY_WINDOWS ) )
482 {
483 if ( isLinuxRootReference( destDirectory ) )
484 {
485 logger.error( "OS=Windows and the assembly descriptor contains a *nix-specific root-relative-reference"
486 + " (starting with slash) " + destDirectory );
487 }
488 else if ( isWindowsPath( destDirectory ) )
489 {
490 logger.warn( "The assembly descriptor contains a *nix-specific root-relative-reference"
491 + " (starting with slash). This is non-portable and will fail on windows "
492 + destDirectory );
493 }
494 }
495 else
496 {
497 if ( isWindowsPath( destDirectory ) )
498 {
499 logger.error(
500 "OS=Non-Windows and the assembly descriptor contains a windows-specific directory reference"
501 + " (with a drive letter) " + destDirectory );
502 }
503 else if ( isLinuxRootReference( destDirectory ) )
504 {
505 logger.warn( "The assembly descriptor contains a filesystem-root relative reference,"
506 + " which is not cross platform compatible " + destDirectory );
507 }
508 }
509 }
510
511 static boolean isWindowsPath( String destDirectory )
512 {
513 return ( destDirectory != null && destDirectory.length() >= 2 && destDirectory.charAt( 1 ) == ':' );
514 }
515
516 static boolean isLinuxRootReference( String destDirectory )
517 {
518 return ( destDirectory != null && destDirectory.startsWith( "/" ) );
519 }
520
521
522 }