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