1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.model.interpolation;
20
21 import javax.inject.Inject;
22
23 import java.io.File;
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.Collection;
27 import java.util.HashSet;
28 import java.util.List;
29 import java.util.Properties;
30
31 import org.apache.maven.model.Model;
32 import org.apache.maven.model.building.ModelBuildingRequest;
33 import org.apache.maven.model.building.ModelProblemCollector;
34 import org.apache.maven.model.path.PathTranslator;
35 import org.apache.maven.model.path.UrlNormalizer;
36 import org.apache.maven.model.root.RootLocator;
37 import org.codehaus.plexus.interpolation.AbstractValueSource;
38 import org.codehaus.plexus.interpolation.InterpolationPostProcessor;
39 import org.codehaus.plexus.interpolation.MapBasedValueSource;
40 import org.codehaus.plexus.interpolation.ObjectBasedValueSource;
41 import org.codehaus.plexus.interpolation.PrefixedObjectValueSource;
42 import org.codehaus.plexus.interpolation.PrefixedValueSourceWrapper;
43 import org.codehaus.plexus.interpolation.RecursionInterceptor;
44 import org.codehaus.plexus.interpolation.SingleResponseValueSource;
45 import org.codehaus.plexus.interpolation.ValueSource;
46 import org.codehaus.plexus.interpolation.util.ValueSourceUtils;
47
48
49
50
51
52
53 public abstract class AbstractStringBasedModelInterpolator implements ModelInterpolator {
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68 public static final String FULL_EXTERNAL_INTERPOLATION_PROPERTY = "maven.model.dependencyInterpolation.full";
69
70 private static final List<String> PROJECT_PREFIXES = Arrays.asList("pom.", "project.");
71
72 private static final Collection<String> TRANSLATED_PATH_EXPRESSIONS;
73
74 static {
75 Collection<String> translatedPrefixes = new HashSet<>();
76
77
78
79
80
81
82 translatedPrefixes.add("build.directory");
83 translatedPrefixes.add("build.outputDirectory");
84 translatedPrefixes.add("build.testOutputDirectory");
85 translatedPrefixes.add("build.sourceDirectory");
86 translatedPrefixes.add("build.testSourceDirectory");
87 translatedPrefixes.add("build.scriptSourceDirectory");
88 translatedPrefixes.add("reporting.outputDirectory");
89
90 TRANSLATED_PATH_EXPRESSIONS = translatedPrefixes;
91 }
92
93 @Inject
94 private PathTranslator pathTranslator;
95
96 @Inject
97 private UrlNormalizer urlNormalizer;
98
99 @Inject
100 private ModelVersionProcessor versionProcessor;
101
102 @Inject
103 private RootLocator rootLocator;
104
105 public AbstractStringBasedModelInterpolator setPathTranslator(PathTranslator pathTranslator) {
106 this.pathTranslator = pathTranslator;
107 return this;
108 }
109
110 public AbstractStringBasedModelInterpolator setUrlNormalizer(UrlNormalizer urlNormalizer) {
111 this.urlNormalizer = urlNormalizer;
112 return this;
113 }
114
115 public AbstractStringBasedModelInterpolator setVersionPropertiesProcessor(ModelVersionProcessor processor) {
116 this.versionProcessor = processor;
117 return this;
118 }
119
120 public AbstractStringBasedModelInterpolator setRootLocator(RootLocator rootLocator) {
121 this.rootLocator = rootLocator;
122 return this;
123 }
124
125 protected List<ValueSource> createValueSources(
126 final Model model,
127 final File projectDir,
128 final ModelBuildingRequest config,
129 final ModelProblemCollector problems) {
130 Properties modelProperties = model.getProperties();
131
132 ValueSource modelValueSource1 = new PrefixedObjectValueSource(PROJECT_PREFIXES, model, false);
133 if (config.getValidationLevel() >= ModelBuildingRequest.VALIDATION_LEVEL_MAVEN_2_0) {
134 modelValueSource1 = new ProblemDetectingValueSource(modelValueSource1, "pom.", "project.", problems);
135 }
136
137 ValueSource modelValueSource2 = new ObjectBasedValueSource(model);
138 if (config.getValidationLevel() >= ModelBuildingRequest.VALIDATION_LEVEL_MAVEN_2_0) {
139 modelValueSource2 = new ProblemDetectingValueSource(modelValueSource2, "", "project.", problems);
140 }
141
142
143 List<ValueSource> valueSources = new ArrayList<>(11);
144
145 if (projectDir != null) {
146 ValueSource basedirValueSource = new PrefixedValueSourceWrapper(
147 new AbstractValueSource(false) {
148 @Override
149 public Object getValue(String expression) {
150 if ("basedir".equals(expression)) {
151 return projectDir.getAbsolutePath();
152 }
153 return null;
154 }
155 },
156 PROJECT_PREFIXES,
157 true);
158 valueSources.add(basedirValueSource);
159
160 ValueSource rootDirectoryValueSource = new PrefixedValueSourceWrapper(
161 new AbstractValueSource(false) {
162 @Override
163 public Object getValue(String expression) {
164 if ("rootDirectory".equals(expression)) {
165 return rootLocator
166 .findMandatoryRoot(projectDir.toPath())
167 .toAbsolutePath()
168 .toString();
169 }
170 return null;
171 }
172 },
173 PROJECT_PREFIXES,
174 false);
175 valueSources.add(rootDirectoryValueSource);
176
177 ValueSource baseUriValueSource = new PrefixedValueSourceWrapper(
178 new AbstractValueSource(false) {
179 @Override
180 public Object getValue(String expression) {
181 if ("baseUri".equals(expression)) {
182 return projectDir
183 .getAbsoluteFile()
184 .toPath()
185 .toUri()
186 .toASCIIString();
187 }
188 return null;
189 }
190 },
191 PROJECT_PREFIXES,
192 false);
193 valueSources.add(baseUriValueSource);
194 valueSources.add(new BuildTimestampValueSource(config.getBuildStartTime(), modelProperties));
195 }
196
197 valueSources.add(modelValueSource1);
198
199
200
201
202
203
204
205
206 boolean restricted = restrictExternalModelInterpolation(config);
207
208 ValueSource userPropertiesValueSource = new MapBasedValueSource(config.getUserProperties());
209 valueSources.add(restricted ? restrictToSafeExpressions(userPropertiesValueSource) : userPropertiesValueSource);
210
211
212
213 versionProcessor.overwriteModelProperties(modelProperties, config);
214 valueSources.add(new MapBasedValueSource(modelProperties));
215
216 ValueSource systemPropertiesValueSource = new MapBasedValueSource(config.getSystemProperties());
217 valueSources.add(
218 restricted ? restrictToSafeExpressions(systemPropertiesValueSource) : systemPropertiesValueSource);
219
220 if (!restricted) {
221 valueSources.add(new AbstractValueSource(false) {
222 @Override
223 public Object getValue(String expression) {
224 return config.getSystemProperties().getProperty("env." + expression);
225 }
226 });
227 }
228
229 valueSources.add(modelValueSource2);
230
231
232 valueSources.add(new SingleResponseValueSource(MAVEN_REPO_CENTRAL_KEY, DEFAULT_MAVEN_REPO_CENTRAL_URL));
233
234 return valueSources;
235 }
236
237 private static boolean restrictExternalModelInterpolation(ModelBuildingRequest config) {
238 return config.getPomFile() == null
239 && config.getValidationLevel() < ModelBuildingRequest.VALIDATION_LEVEL_MAVEN_2_0
240 && !isFullExternalModelBuild(config);
241 }
242
243
244
245
246
247
248
249
250
251 public static boolean isFullExternalModelBuild(ModelBuildingRequest config) {
252 return Boolean.parseBoolean(config.getSystemProperties().getProperty(FULL_EXTERNAL_INTERPOLATION_PROPERTY))
253 || Boolean.parseBoolean(config.getUserProperties().getProperty(FULL_EXTERNAL_INTERPOLATION_PROPERTY));
254 }
255
256 private static ValueSource restrictToSafeExpressions(ValueSource source) {
257 return new AbstractValueSource(false) {
258 @Override
259 public Object getValue(String expression) {
260 return isSafeExternalExpression(expression) ? source.getValue(expression) : null;
261 }
262 };
263 }
264
265
266
267
268
269
270 private static boolean isSafeExternalExpression(String expression) {
271 return expression.startsWith("java.")
272 || expression.startsWith("os.")
273 || expression.startsWith("maven.")
274 || "file.separator".equals(expression)
275 || "path.separator".equals(expression)
276 || "line.separator".equals(expression)
277 || "revision".equals(expression)
278 || "changelist".equals(expression)
279 || "sha1".equals(expression);
280 }
281
282 protected List<? extends InterpolationPostProcessor> createPostProcessors(
283 final Model model, final File projectDir, final ModelBuildingRequest config) {
284 List<InterpolationPostProcessor> processors = new ArrayList<>(2);
285 if (projectDir != null) {
286 processors.add(new PathTranslatingPostProcessor(
287 PROJECT_PREFIXES, TRANSLATED_PATH_EXPRESSIONS,
288 projectDir, pathTranslator));
289 }
290 processors.add(new UrlNormalizingPostProcessor(urlNormalizer));
291 return processors;
292 }
293
294 private enum PrefixOnlyExpression {
295 ROOT_DIRECTORY("rootDirectory"),
296 BASE_URI("baseUri");
297
298 private final String expression;
299
300 PrefixOnlyExpression(String expression) {
301 this.expression = expression;
302 }
303
304 @Override
305 public String toString() {
306 return "project." + expression;
307 }
308 }
309
310 protected RecursionInterceptor createRecursionInterceptor() {
311 return new RecursionInterceptor() {
312 private final List<Object> expressions = new ArrayList<>();
313
314 private Object key(String expression) {
315 String naked = ValueSourceUtils.trimPrefix(expression, PROJECT_PREFIXES, true);
316
317 if (!naked.equals(expression)) {
318 if ("rootDirectory".equals(naked)) {
319 return PrefixOnlyExpression.ROOT_DIRECTORY;
320 }
321 if ("baseUri".equals(naked)) {
322 return PrefixOnlyExpression.BASE_URI;
323 }
324 }
325 return naked;
326 }
327
328 @Override
329 public void expressionResolutionStarted(String expression) {
330 expressions.add(key(expression));
331 }
332
333 @Override
334 public void expressionResolutionFinished(String expression) {
335 expressions.remove(expressions.size() - 1);
336 }
337
338 @Override
339 public boolean hasRecursiveExpression(String expression) {
340 return expressions.contains(key(expression));
341 }
342
343 @Override
344 public List<?> getExpressionCycle(String expression) {
345 int idx = expressions.indexOf(key(expression));
346 List<String> cycle = new ArrayList<>();
347 if (idx >= 0) {
348 for (Object key : expressions.subList(idx, expressions.size())) {
349 cycle.add(key.toString());
350 }
351 }
352 return cycle;
353 }
354
355 @Override
356 public void clear() {
357 expressions.clear();
358 }
359 };
360 }
361 }