View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * Use a regular expression search to find and resolve expressions within the POM.
50   *
51   * @author jdcasey Created on Feb 3, 2005
52   */
53  public abstract class AbstractStringBasedModelInterpolator implements ModelInterpolator {
54  
55      /**
56       * User property for opting back into the previous behavior of interpolating
57       * repository-resolved models (built at {@link ModelBuildingRequest#VALIDATION_LEVEL_MINIMAL})
58       * against the full set of session properties (system, environment and CLI).
59       * When set to {@code "false"} (default), such models are interpolated only against
60       * their own {@code <properties>}, preventing property leaking from the requesting
61       * build into transitive POMs. When set to {@code "true"}, full interpolation is
62       * applied as in previous Maven versions.
63       * <p>
64       * In Maven 4.x this constant is promoted to
65       * {@code org.apache.maven.api.Constants.MAVEN_MODEL_DEPENDENCY_INTERPOLATION_FULL}
66       * with {@code @Config} so it appears in the auto-generated configuration documentation.
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          // MNG-1927, MNG-2124, MNG-3355:
78          // If the build section is present and the project directory is non-null, we should make
79          // sure interpolation of the directories below uses translated paths.
80          // Afterward, we'll double back and translate any paths that weren't covered during interpolation via the
81          // code below...
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         // NOTE: Order counts here!
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         // Models built without a POM file at VALIDATION_LEVEL_MINIMAL are the models Maven builds
200         // while resolving dependency, parent and BOM-import POMs from a repository, not the
201         // operator's own project. Such models interpolate only against their own properties and a
202         // small set of environment-independent expressions; everything else in the user/system
203         // property space stays uninterpolated. Operator project builds have a POM file or use a
204         // higher validation level and keep the full set of value sources, unchanged from previous
205         // behavior.
206         boolean restricted = restrictExternalModelInterpolation(config);
207 
208         ValueSource userPropertiesValueSource = new MapBasedValueSource(config.getUserProperties());
209         valueSources.add(restricted ? restrictToSafeExpressions(userPropertiesValueSource) : userPropertiesValueSource);
210 
211         // Overwrite existing values in model properties. Otherwise it's not possible
212         // to define them via command line e.g.: mvn -Drevision=6.5.7 ...
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         // last source: make sure Maven Repo Central is present (if is present anywhere else, it will prevail this one)
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      * Tells whether {@value #FULL_EXTERNAL_INTERPOLATION_PROPERTY} is set, as a system or a user property,
245      * which exempts models built below {@link ModelBuildingRequest#VALIDATION_LEVEL_MAVEN_2_0} from the
246      * restrictions that apply to models resolved from a repository.
247      *
248      * @param config the model building request
249      * @return {@code true} if the property is set to {@code true}
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      * Expressions that models built at {@link ModelBuildingRequest#VALIDATION_LEVEL_MINIMAL}
267      * may still resolve from the session properties: JVM- and Maven-defined properties, plus
268      * the CI-friendly version properties (MNG-5895). All other expressions are left literal.
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                 // Built-in keys must also be distinct from custom names with repeated project prefixes.
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 }