View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * Finds an absolute path for {@link ActivationFile#getExists()} or {@link ActivationFile#getMissing()}
38   *
39   * @deprecated use {@code org.apache.maven.api.services.ModelBuilder} instead
40   */
41  @Named
42  @Singleton
43  @Deprecated
44  public class ProfileActivationFilePathInterpolator {
45  
46      @Inject
47      private PathTranslator pathTranslator;
48  
49      @Inject
50      private RootLocator rootLocator;
51  
52      public ProfileActivationFilePathInterpolator setPathTranslator(PathTranslator pathTranslator) {
53          this.pathTranslator = pathTranslator;
54          return this;
55      }
56  
57      public ProfileActivationFilePathInterpolator setRootLocator(RootLocator rootLocator) {
58          this.rootLocator = rootLocator;
59          return this;
60      }
61  
62      /**
63       * Interpolates given {@code path}.
64       *
65       * @return absolute path or {@code null} if the input was {@code null}
66       */
67      public String interpolate(String path, ProfileActivationContext context) throws InterpolationException {
68          if (path == null) {
69              return null;
70          }
71  
72          RegexBasedInterpolator interpolator = new RegexBasedInterpolator();
73  
74          final File basedir = context.getProjectDirectory();
75  
76          if (basedir != null) {
77              interpolator.addValueSource(new AbstractValueSource(false) {
78                  @Override
79                  public Object getValue(String expression) {
80                      if ("basedir".equals(expression) || "project.basedir".equals(expression)) {
81                          return basedir.getAbsolutePath();
82                      }
83                      return null;
84                  }
85              });
86          } else if (path.contains("${basedir}")) {
87              return null;
88          }
89  
90          interpolator.addValueSource(new AbstractValueSource(false) {
91              @Override
92              public Object getValue(String expression) {
93                  if ("project.rootDirectory".equals(expression)) {
94                      Path base = basedir != null ? basedir.toPath() : null;
95                      Path root = rootLocator.findMandatoryRoot(base);
96                      return root.toFile().getAbsolutePath();
97                  }
98                  return null;
99              }
100         });
101 
102         interpolator.addValueSource(new MapBasedValueSource(context.getProjectProperties()));
103 
104         interpolator.addValueSource(new MapBasedValueSource(context.getUserProperties()));
105 
106         interpolator.addValueSource(new MapBasedValueSource(context.getSystemProperties()));
107 
108         String absolutePath = interpolator.interpolate(path, "");
109 
110         return pathTranslator.alignToBaseDirectory(absolutePath, basedir);
111     }
112 }