1 package org.apache.maven.plugins.assembly.interpolation;
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.plugins.assembly.AssemblerConfigurationSource;
23 import org.apache.maven.plugins.assembly.model.io.xpp3.AssemblyXpp3Reader;
24 import org.apache.maven.plugins.assembly.model.io.xpp3.ComponentXpp3Reader;
25 import org.apache.maven.plugins.assembly.resolved.AssemblyId;
26 import org.apache.maven.plugins.assembly.utils.AssemblyFileUtils;
27 import org.apache.maven.project.MavenProject;
28 import org.codehaus.plexus.interpolation.InterpolationPostProcessor;
29 import org.codehaus.plexus.interpolation.fixed.FixedStringSearchInterpolator;
30 import org.codehaus.plexus.interpolation.fixed.InterpolationState;
31 import org.codehaus.plexus.interpolation.object.FieldBasedObjectInterpolator;
32 import org.slf4j.Logger;
33
34 import java.io.File;
35 import java.io.IOException;
36 import java.util.HashSet;
37 import java.util.List;
38 import java.util.Set;
39
40
41
42
43 public class AssemblyInterpolator
44 {
45 private static final Set<String> INTERPOLATION_BLACKLIST;
46
47 static
48 {
49 final Set<String> blacklist = new HashSet<>();
50
51 blacklist.add( "outputFileNameMapping" );
52 blacklist.add( "outputDirectoryMapping" );
53 blacklist.add( "outputDirectory" );
54
55 INTERPOLATION_BLACKLIST = blacklist;
56 }
57
58 public AssemblyInterpolator()
59 throws IOException
60 {
61 }
62
63 public static AssemblyXpp3Reader.ContentTransformer assemblyInterpolator(
64 final FixedStringSearchInterpolator interpolator, final InterpolationState is, final Logger logger )
65 {
66 final Set<String> blacklistFields =
67 new HashSet<>( FieldBasedObjectInterpolator.DEFAULT_BLACKLISTED_FIELD_NAMES );
68 blacklistFields.addAll( INTERPOLATION_BLACKLIST );
69
70 return new AssemblyXpp3Reader.ContentTransformer()
71 {
72 @Override
73 public String transform( String source, String contextDescription )
74 {
75 if ( blacklistFields.contains( contextDescription ) )
76 {
77 return source;
78 }
79
80 String interpolated = interpolator.interpolate( source, is );
81 if ( !source.equals( interpolated ) && logger.isDebugEnabled() )
82 {
83 logger.debug(
84 "Field " + contextDescription + " source: " + source + " interpolated to: " + interpolated );
85 }
86 return interpolated;
87 }
88 };
89 }
90
91 public static ComponentXpp3Reader.ContentTransformer componentInterpolator(
92 final FixedStringSearchInterpolator interpolator, final InterpolationState is, final Logger logger )
93 {
94 final Set<String> blacklistFields =
95 new HashSet<>( FieldBasedObjectInterpolator.DEFAULT_BLACKLISTED_FIELD_NAMES );
96 blacklistFields.addAll( INTERPOLATION_BLACKLIST );
97
98 return new ComponentXpp3Reader.ContentTransformer()
99 {
100 @Override
101 public String transform( String source, String contextDescription )
102 {
103 if ( blacklistFields.contains( contextDescription ) )
104 {
105 return source;
106 }
107
108 String interpolated = interpolator.interpolate( source, is );
109 if ( !source.equals( interpolated ) )
110 {
111 logger.debug(
112 "Field " + contextDescription + " source: " + source + " interpolated to: " + interpolated );
113 }
114 return interpolated;
115 }
116 };
117 }
118
119
120 public static void checkErrors( AssemblyId assemblyId, InterpolationState interpolationState, Logger logger )
121 {
122 if ( interpolationState.asList() != null && interpolationState.asList().size() > 0 && logger.isDebugEnabled() )
123 {
124 final StringBuilder sb = new StringBuilder();
125
126 sb.append( "One or more minor errors occurred while interpolating the assembly with ID: " ).append(
127 assemblyId ).append( ":\n" );
128
129 @SuppressWarnings( "unchecked" ) final List<Object> warnings = interpolationState.asList();
130 for ( final Object warning : warnings )
131 {
132 sb.append( '\n' ).append( warning );
133 }
134
135 sb.append( "\n\nThese values were SKIPPED, but the assembly process will continue.\n" );
136
137 logger.debug( sb.toString() );
138 }
139 }
140
141 public static FixedStringSearchInterpolator fullInterpolator( final MavenProject project,
142 final FixedStringSearchInterpolator projectIp,
143 final AssemblerConfigurationSource configSource )
144 {
145 FixedStringSearchInterpolator fixedStringSearchInterpolator =
146 FixedStringSearchInterpolator.create( configSource.getRepositoryInterpolator(),
147 configSource.getCommandLinePropsInterpolator(),
148 configSource.getEnvInterpolator(), projectIp );
149 return fixedStringSearchInterpolator.withPostProcessor(
150 new PathTranslatingPostProcessor( project.getBasedir() ) );
151
152 }
153
154 private static final class PathTranslatingPostProcessor
155 implements InterpolationPostProcessor
156 {
157
158 private final File basedir;
159
160 PathTranslatingPostProcessor( final File basedir )
161 {
162 this.basedir = basedir;
163 }
164
165 @Override
166 public Object execute( final String expression, final Object value )
167 {
168 final String path = String.valueOf( value );
169 return AssemblyFileUtils.makePathRelativeTo( path, basedir );
170 }
171
172 }
173 }