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