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.api.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 private final PathTranslator pathTranslator;
46
47 private final RootLocator rootLocator;
48
49 @Inject
50 public ProfileActivationFilePathInterpolator(PathTranslator pathTranslator, RootLocator rootLocator) {
51 this.pathTranslator = pathTranslator;
52 this.rootLocator = rootLocator;
53 }
54
55
56
57
58
59
60 public String interpolate(String path, ProfileActivationContext context) throws InterpolationException {
61 if (path == null) {
62 return null;
63 }
64
65 RegexBasedInterpolator interpolator = new RegexBasedInterpolator();
66
67 final File basedir = context.getProjectDirectory();
68
69 if (basedir != null) {
70 interpolator.addValueSource(new AbstractValueSource(false) {
71 @Override
72 public Object getValue(String expression) {
73
74
75
76
77 if ("basedir".equals(expression)) {
78 return basedir.getAbsolutePath();
79 }
80 return null;
81 }
82 });
83 } else if (path.contains("${basedir}")) {
84 return null;
85 }
86
87 interpolator.addValueSource(new AbstractValueSource(false) {
88 @Override
89 public Object getValue(String expression) {
90 if ("rootDirectory".equals(expression)) {
91 Path base = basedir != null ? basedir.toPath() : null;
92 Path root = rootLocator.findMandatoryRoot(base);
93 return root.toFile().getAbsolutePath();
94 }
95 return null;
96 }
97 });
98
99 interpolator.addValueSource(new MapBasedValueSource(context.getProjectProperties()));
100
101 interpolator.addValueSource(new MapBasedValueSource(context.getUserProperties()));
102
103 interpolator.addValueSource(new MapBasedValueSource(context.getSystemProperties()));
104
105 String absolutePath = interpolator.interpolate(path, "");
106
107 return pathTranslator.alignToBaseDirectory(absolutePath, basedir);
108 }
109 }