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