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