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.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
48
49
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
60
61
62
63
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
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
159
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 }