1 | |
package org.apache.maven.plugin.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.execution.MavenSession; |
23 | |
import org.apache.maven.plugin.assembly.AssemblerConfigurationSource; |
24 | |
import org.apache.maven.plugin.assembly.model.Assembly; |
25 | |
import org.apache.maven.plugin.assembly.utils.AssemblyFileUtils; |
26 | |
import org.apache.maven.plugin.assembly.utils.InterpolationConstants; |
27 | |
import org.apache.maven.project.MavenProject; |
28 | |
import org.codehaus.plexus.interpolation.InterpolationException; |
29 | |
import org.codehaus.plexus.interpolation.InterpolationPostProcessor; |
30 | |
import org.codehaus.plexus.interpolation.Interpolator; |
31 | |
import org.codehaus.plexus.interpolation.PrefixAwareRecursionInterceptor; |
32 | |
import org.codehaus.plexus.interpolation.PrefixedObjectValueSource; |
33 | |
import org.codehaus.plexus.interpolation.PrefixedPropertiesValueSource; |
34 | |
import org.codehaus.plexus.interpolation.PropertiesBasedValueSource; |
35 | |
import org.codehaus.plexus.interpolation.RecursionInterceptor; |
36 | |
import org.codehaus.plexus.interpolation.StringSearchInterpolator; |
37 | |
import org.codehaus.plexus.interpolation.object.FieldBasedObjectInterpolator; |
38 | |
import org.codehaus.plexus.interpolation.object.ObjectInterpolationWarning; |
39 | |
import org.codehaus.plexus.logging.AbstractLogEnabled; |
40 | |
import org.codehaus.plexus.logging.Logger; |
41 | |
import org.codehaus.plexus.logging.console.ConsoleLogger; |
42 | |
import org.codehaus.plexus.util.cli.CommandLineUtils; |
43 | |
|
44 | |
import java.io.File; |
45 | |
import java.io.IOException; |
46 | |
import java.util.Collections; |
47 | |
import java.util.HashSet; |
48 | |
import java.util.Iterator; |
49 | |
import java.util.List; |
50 | |
import java.util.Properties; |
51 | |
import java.util.Set; |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
public class AssemblyInterpolator |
57 | |
extends AbstractLogEnabled |
58 | |
{ |
59 | |
private static final Set<String> INTERPOLATION_BLACKLIST; |
60 | |
|
61 | |
private static final Properties ENVIRONMENT_VARIABLES; |
62 | |
|
63 | |
static |
64 | |
{ |
65 | 3 | final Set<String> blacklist = new HashSet<String>(); |
66 | |
|
67 | 3 | blacklist.add( "outputFileNameMapping" ); |
68 | 3 | blacklist.add( "outputDirectoryMapping" ); |
69 | 3 | blacklist.add( "outputDirectory" ); |
70 | |
|
71 | 3 | INTERPOLATION_BLACKLIST = blacklist; |
72 | |
|
73 | |
Properties environmentVariables; |
74 | |
try |
75 | |
{ |
76 | 3 | environmentVariables = CommandLineUtils.getSystemEnvVars( false ); |
77 | |
} |
78 | 0 | catch ( final IOException e ) |
79 | |
{ |
80 | 0 | environmentVariables = new Properties(); |
81 | 3 | } |
82 | |
|
83 | 3 | ENVIRONMENT_VARIABLES = environmentVariables; |
84 | 3 | } |
85 | |
|
86 | |
public AssemblyInterpolator() |
87 | |
throws IOException |
88 | 90 | { |
89 | 90 | } |
90 | |
|
91 | |
public Assembly interpolate( final Assembly assembly, final MavenProject project, |
92 | |
final AssemblerConfigurationSource configSource ) |
93 | |
throws AssemblyInterpolationException |
94 | |
{ |
95 | |
@SuppressWarnings( "unchecked" ) |
96 | 75 | final Set<String> blacklistFields = |
97 | |
new HashSet<String>( FieldBasedObjectInterpolator.DEFAULT_BLACKLISTED_FIELD_NAMES ); |
98 | 75 | blacklistFields.addAll( INTERPOLATION_BLACKLIST ); |
99 | |
|
100 | |
@SuppressWarnings( "unchecked" ) |
101 | 75 | final Set<String> blacklistPkgs = FieldBasedObjectInterpolator.DEFAULT_BLACKLISTED_PACKAGE_PREFIXES; |
102 | |
|
103 | 75 | final FieldBasedObjectInterpolator objectInterpolator = |
104 | |
new FieldBasedObjectInterpolator( blacklistFields, blacklistPkgs ); |
105 | 75 | final Interpolator interpolator = buildInterpolator( project, configSource ); |
106 | |
|
107 | |
|
108 | |
|
109 | 75 | final RecursionInterceptor interceptor = |
110 | |
new PrefixAwareRecursionInterceptor( InterpolationConstants.PROJECT_PREFIXES, true ); |
111 | |
|
112 | |
try |
113 | |
{ |
114 | 75 | objectInterpolator.interpolate( assembly, interpolator, interceptor ); |
115 | |
} |
116 | 0 | catch ( final InterpolationException e ) |
117 | |
{ |
118 | 0 | throw new AssemblyInterpolationException( "Failed to interpolate assembly with ID: " + assembly.getId() |
119 | |
+ ". Reason: " + e.getMessage(), e ); |
120 | |
} |
121 | |
finally |
122 | |
{ |
123 | 75 | interpolator.clearAnswers(); |
124 | 75 | } |
125 | |
|
126 | 75 | if ( objectInterpolator.hasWarnings() && getLogger().isDebugEnabled() ) |
127 | |
{ |
128 | 0 | final StringBuilder sb = new StringBuilder(); |
129 | |
|
130 | 0 | sb.append( "One or more minor errors occurred while interpolating the assembly with ID: " |
131 | |
+ assembly.getId() + ":\n" ); |
132 | |
|
133 | |
@SuppressWarnings( "unchecked" ) |
134 | 0 | final List<ObjectInterpolationWarning> warnings = objectInterpolator.getWarnings(); |
135 | 0 | for ( final Iterator<ObjectInterpolationWarning> it = warnings.iterator(); it.hasNext(); ) |
136 | |
{ |
137 | 0 | final ObjectInterpolationWarning warning = it.next(); |
138 | |
|
139 | 0 | sb.append( '\n' ).append( warning ); |
140 | 0 | } |
141 | |
|
142 | 0 | sb.append( "\n\nThese values were SKIPPED, but the assembly process will continue.\n" ); |
143 | |
|
144 | 0 | getLogger().debug( sb.toString() ); |
145 | |
} |
146 | |
|
147 | 75 | return assembly; |
148 | |
} |
149 | |
|
150 | |
public static Interpolator buildInterpolator( final MavenProject project, |
151 | |
final AssemblerConfigurationSource configSource ) |
152 | |
{ |
153 | 150 | final StringSearchInterpolator interpolator = new StringSearchInterpolator(); |
154 | 150 | interpolator.setCacheAnswers( true ); |
155 | |
|
156 | 150 | final MavenSession session = configSource.getMavenSession(); |
157 | |
|
158 | 150 | if ( session != null ) |
159 | |
{ |
160 | 6 | Properties userProperties = null; |
161 | |
try |
162 | |
{ |
163 | 6 | userProperties = session.getExecutionProperties(); |
164 | |
} |
165 | 0 | catch ( final NoSuchMethodError nsmer ) |
166 | |
{ |
167 | |
|
168 | 6 | } |
169 | |
|
170 | 6 | if ( userProperties != null ) |
171 | |
{ |
172 | |
|
173 | 6 | interpolator.addValueSource( new PropertiesBasedValueSource( userProperties ) ); |
174 | |
} |
175 | |
} |
176 | |
|
177 | 150 | interpolator.addValueSource( new PrefixedPropertiesValueSource( |
178 | |
InterpolationConstants.PROJECT_PROPERTIES_PREFIXES, |
179 | |
project.getProperties(), true ) ); |
180 | 150 | interpolator.addValueSource( new PrefixedObjectValueSource( InterpolationConstants.PROJECT_PREFIXES, project, |
181 | |
true ) ); |
182 | |
|
183 | 150 | final Properties settingsProperties = new Properties(); |
184 | 150 | if ( configSource.getLocalRepository() != null ) |
185 | |
{ |
186 | 120 | settingsProperties.setProperty( "localRepository", configSource.getLocalRepository().getBasedir() ); |
187 | 120 | settingsProperties.setProperty( "settings.localRepository", configSource.getLocalRepository().getBasedir() ); |
188 | |
} |
189 | 30 | else if ( session != null && session.getSettings() != null ) |
190 | |
{ |
191 | 0 | settingsProperties.setProperty( "localRepository", session.getSettings().getLocalRepository() ); |
192 | 0 | settingsProperties.setProperty( "settings.localRepository", configSource.getLocalRepository().getBasedir() ); |
193 | |
} |
194 | |
|
195 | 150 | interpolator.addValueSource( new PropertiesBasedValueSource( settingsProperties ) ); |
196 | |
|
197 | 150 | Properties commandLineProperties = System.getProperties(); |
198 | 150 | if ( session != null ) |
199 | |
{ |
200 | 6 | commandLineProperties = new Properties(); |
201 | 6 | if ( session.getExecutionProperties() != null ) |
202 | |
{ |
203 | 6 | commandLineProperties.putAll( session.getExecutionProperties() ); |
204 | |
} |
205 | |
|
206 | 6 | if ( session.getUserProperties() != null ) |
207 | |
{ |
208 | 6 | commandLineProperties.putAll( session.getUserProperties() ); |
209 | |
} |
210 | |
} |
211 | |
|
212 | |
|
213 | 150 | interpolator.addValueSource( new PropertiesBasedValueSource( commandLineProperties ) ); |
214 | 150 | interpolator.addValueSource( new PrefixedPropertiesValueSource( Collections.singletonList( "env." ), |
215 | |
ENVIRONMENT_VARIABLES, true ) ); |
216 | |
|
217 | 150 | interpolator.addPostProcessor( new PathTranslatingPostProcessor( project.getBasedir() ) ); |
218 | 150 | return interpolator; |
219 | |
} |
220 | |
|
221 | |
@Override |
222 | |
protected Logger getLogger() |
223 | |
{ |
224 | 0 | Logger logger = super.getLogger(); |
225 | |
|
226 | 0 | if ( logger == null ) |
227 | |
{ |
228 | 0 | logger = new ConsoleLogger( Logger.LEVEL_INFO, "interpolator-internal" ); |
229 | |
|
230 | 0 | enableLogging( logger ); |
231 | |
} |
232 | |
|
233 | 0 | return logger; |
234 | |
} |
235 | |
|
236 | |
private static final class PathTranslatingPostProcessor |
237 | |
implements InterpolationPostProcessor |
238 | |
{ |
239 | |
|
240 | |
private final File basedir; |
241 | |
|
242 | |
public PathTranslatingPostProcessor( final File basedir ) |
243 | 150 | { |
244 | 150 | this.basedir = basedir; |
245 | 150 | } |
246 | |
|
247 | |
public Object execute( final String expression, final Object value ) |
248 | |
{ |
249 | 30 | final String path = String.valueOf( value ); |
250 | 30 | return AssemblyFileUtils.makePathRelativeTo( path, basedir ); |
251 | |
} |
252 | |
|
253 | |
} |
254 | |
} |