1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.internal.impl.model;
20
21 import java.nio.file.Path;
22
23 import org.apache.maven.api.di.Inject;
24 import org.apache.maven.api.di.Named;
25 import org.apache.maven.api.di.Singleton;
26 import org.apache.maven.api.model.ActivationFile;
27 import org.apache.maven.api.services.model.PathTranslator;
28 import org.apache.maven.api.services.model.ProfileActivationContext;
29 import org.apache.maven.api.services.model.RootLocator;
30 import org.codehaus.plexus.interpolation.AbstractValueSource;
31 import org.codehaus.plexus.interpolation.InterpolationException;
32 import org.codehaus.plexus.interpolation.MapBasedValueSource;
33 import org.codehaus.plexus.interpolation.RegexBasedInterpolator;
34
35
36
37
38
39 @Named
40 @Singleton
41 public class ProfileActivationFilePathInterpolator {
42
43 private final PathTranslator pathTranslator;
44
45 private final RootLocator rootLocator;
46
47 @Inject
48 public ProfileActivationFilePathInterpolator(PathTranslator pathTranslator, RootLocator rootLocator) {
49 this.pathTranslator = pathTranslator;
50 this.rootLocator = rootLocator;
51 }
52
53
54
55
56
57
58 public String interpolate(String path, ProfileActivationContext context) throws InterpolationException {
59 if (path == null) {
60 return null;
61 }
62
63 RegexBasedInterpolator interpolator = new RegexBasedInterpolator();
64
65 Path basedir = context.getProjectDirectory();
66
67 if (basedir != null) {
68 interpolator.addValueSource(new AbstractValueSource(false) {
69 @Override
70 public Object getValue(String expression) {
71 if ("basedir".equals(expression) || "project.basedir".equals(expression)) {
72 return basedir.toAbsolutePath().toString();
73 }
74 return null;
75 }
76 });
77 } else if (path.contains("${basedir}")) {
78 return null;
79 }
80
81 interpolator.addValueSource(new AbstractValueSource(false) {
82 @Override
83 public Object getValue(String expression) {
84 if ("project.rootDirectory".equals(expression)) {
85 Path root = rootLocator.findMandatoryRoot(basedir);
86 return root.toFile().getAbsolutePath();
87 }
88 return null;
89 }
90 });
91
92 interpolator.addValueSource(new MapBasedValueSource(context.getProjectProperties()));
93 interpolator.addValueSource(new MapBasedValueSource(context.getUserProperties()));
94 interpolator.addValueSource(new MapBasedValueSource(context.getSystemProperties()));
95
96 String absolutePath = interpolator.interpolate(path, "");
97
98 return pathTranslator.alignToBaseDirectory(absolutePath, basedir);
99 }
100 }