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.codehaus.plexus.interpolation.AbstractValueSource;
37  import org.codehaus.plexus.interpolation.InterpolationPostProcessor;
38  import org.codehaus.plexus.interpolation.MapBasedValueSource;
39  import org.codehaus.plexus.interpolation.ObjectBasedValueSource;
40  import org.codehaus.plexus.interpolation.PrefixAwareRecursionInterceptor;
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.ValueSource;
45  
46  /**
47   * Use a regular expression search to find and resolve expressions within the POM.
48   *
49   * @author jdcasey Created on Feb 3, 2005
50   */
51  public abstract class AbstractStringBasedModelInterpolator implements ModelInterpolator {
52      private static final List<String> PROJECT_PREFIXES = Arrays.asList("pom.", "project.");
53  
54      private static final Collection<String> TRANSLATED_PATH_EXPRESSIONS;
55  
56      static {
57          Collection<String> translatedPrefixes = new HashSet<>();
58  
59          // MNG-1927, MNG-2124, MNG-3355:
60          // If the build section is present and the project directory is non-null, we should make
61          // sure interpolation of the directories below uses translated paths.
62          // Afterward, we'll double back and translate any paths that weren't covered during interpolation via the
63          // code below...
64          translatedPrefixes.add("build.directory");
65          translatedPrefixes.add("build.outputDirectory");
66          translatedPrefixes.add("build.testOutputDirectory");
67          translatedPrefixes.add("build.sourceDirectory");
68          translatedPrefixes.add("build.testSourceDirectory");
69          translatedPrefixes.add("build.scriptSourceDirectory");
70          translatedPrefixes.add("reporting.outputDirectory");
71  
72          TRANSLATED_PATH_EXPRESSIONS = translatedPrefixes;
73      }
74  
75      @Inject
76      private PathTranslator pathTranslator;
77  
78      @Inject
79      private UrlNormalizer urlNormalizer;
80  
81      @Inject
82      private ModelVersionProcessor versionProcessor;
83  
84      public AbstractStringBasedModelInterpolator setPathTranslator(PathTranslator pathTranslator) {
85          this.pathTranslator = pathTranslator;
86          return this;
87      }
88  
89      public AbstractStringBasedModelInterpolator setUrlNormalizer(UrlNormalizer urlNormalizer) {
90          this.urlNormalizer = urlNormalizer;
91          return this;
92      }
93  
94      public AbstractStringBasedModelInterpolator setVersionPropertiesProcessor(ModelVersionProcessor processor) {
95          this.versionProcessor = processor;
96          return this;
97      }
98  
99      protected List<ValueSource> createValueSources(
100             final Model model,
101             final File projectDir,
102             final ModelBuildingRequest config,
103             final ModelProblemCollector problems) {
104         Properties modelProperties = model.getProperties();
105 
106         ValueSource modelValueSource1 = new PrefixedObjectValueSource(PROJECT_PREFIXES, model, false);
107         if (config.getValidationLevel() >= ModelBuildingRequest.VALIDATION_LEVEL_MAVEN_2_0) {
108             modelValueSource1 = new ProblemDetectingValueSource(modelValueSource1, "pom.", "project.", problems);
109         }
110 
111         ValueSource modelValueSource2 = new ObjectBasedValueSource(model);
112         if (config.getValidationLevel() >= ModelBuildingRequest.VALIDATION_LEVEL_MAVEN_2_0) {
113             modelValueSource2 = new ProblemDetectingValueSource(modelValueSource2, "", "project.", problems);
114         }
115 
116         // NOTE: Order counts here!
117         List<ValueSource> valueSources = new ArrayList<>(9);
118 
119         if (projectDir != null) {
120             ValueSource basedirValueSource = new PrefixedValueSourceWrapper(
121                     new AbstractValueSource(false) {
122                         @Override
123                         public Object getValue(String expression) {
124                             if ("basedir".equals(expression)) {
125                                 return projectDir.getAbsolutePath();
126                             }
127                             return null;
128                         }
129                     },
130                     PROJECT_PREFIXES,
131                     true);
132             valueSources.add(basedirValueSource);
133 
134             ValueSource baseUriValueSource = new PrefixedValueSourceWrapper(
135                     new AbstractValueSource(false) {
136                         @Override
137                         public Object getValue(String expression) {
138                             if ("baseUri".equals(expression)) {
139                                 return projectDir
140                                         .getAbsoluteFile()
141                                         .toPath()
142                                         .toUri()
143                                         .toASCIIString();
144                             }
145                             return null;
146                         }
147                     },
148                     PROJECT_PREFIXES,
149                     false);
150             valueSources.add(baseUriValueSource);
151             valueSources.add(new BuildTimestampValueSource(config.getBuildStartTime(), modelProperties));
152         }
153 
154         valueSources.add(modelValueSource1);
155 
156         valueSources.add(new MapBasedValueSource(config.getUserProperties()));
157 
158         // Overwrite existing values in model properties. Otherwise it's not possible
159         // to define them via command line e.g.: mvn -Drevision=6.5.7 ...
160         versionProcessor.overwriteModelProperties(modelProperties, config);
161         valueSources.add(new MapBasedValueSource(modelProperties));
162 
163         valueSources.add(new MapBasedValueSource(config.getSystemProperties()));
164 
165         valueSources.add(new AbstractValueSource(false) {
166             @Override
167             public Object getValue(String expression) {
168                 return config.getSystemProperties().getProperty("env." + expression);
169             }
170         });
171 
172         valueSources.add(modelValueSource2);
173 
174         return valueSources;
175     }
176 
177     protected List<? extends InterpolationPostProcessor> createPostProcessors(
178             final Model model, final File projectDir, final ModelBuildingRequest config) {
179         List<InterpolationPostProcessor> processors = new ArrayList<>(2);
180         if (projectDir != null) {
181             processors.add(new PathTranslatingPostProcessor(
182                     PROJECT_PREFIXES, TRANSLATED_PATH_EXPRESSIONS,
183                     projectDir, pathTranslator));
184         }
185         processors.add(new UrlNormalizingPostProcessor(urlNormalizer));
186         return processors;
187     }
188 
189     protected RecursionInterceptor createRecursionInterceptor() {
190         return new PrefixAwareRecursionInterceptor(PROJECT_PREFIXES);
191     }
192 }