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.internal.impl.model;
20  
21  import java.nio.file.Path;
22  
23  import org.apache.maven.api.di.Inject;
24  import org.apache.maven.api.di.Named;
25  import org.apache.maven.api.di.Singleton;
26  import org.apache.maven.api.model.ActivationFile;
27  import org.apache.maven.api.services.Interpolator;
28  import org.apache.maven.api.services.InterpolatorException;
29  import org.apache.maven.api.services.model.PathTranslator;
30  import org.apache.maven.api.services.model.ProfileActivationContext;
31  import org.apache.maven.api.services.model.RootLocator;
32  
33  /**
34   * Finds an absolute path for {@link ActivationFile#getExists()} or {@link ActivationFile#getMissing()}
35   */
36  @Named
37  @Singleton
38  public class ProfileActivationFilePathInterpolator {
39  
40      private final PathTranslator pathTranslator;
41  
42      private final RootLocator rootLocator;
43  
44      private final Interpolator interpolator;
45  
46      @Inject
47      public ProfileActivationFilePathInterpolator(
48              PathTranslator pathTranslator, RootLocator rootLocator, Interpolator interpolator) {
49          this.pathTranslator = pathTranslator;
50          this.rootLocator = rootLocator;
51          this.interpolator = interpolator;
52      }
53  
54      /**
55       * Interpolates given {@code path}.
56       *
57       * @return absolute path or {@code null} if the input was {@code null}
58       */
59      public String interpolate(String path, ProfileActivationContext context) throws InterpolatorException {
60          if (path == null) {
61              return null;
62          }
63  
64          Path basedir = context.getProjectDirectory();
65  
66          String absolutePath = interpolator.interpolate(path, s -> {
67              if ("basedir".equals(s) || "project.basedir".equals(s)) {
68                  return basedir != null ? basedir.toFile().getAbsolutePath() : null;
69              }
70              if ("project.rootDirectory".equals(s)) {
71                  Path root = rootLocator.findMandatoryRoot(basedir);
72                  return root.toFile().getAbsolutePath();
73              }
74              String r = context.getProjectProperties().get(s);
75              if (r == null) {
76                  r = context.getUserProperties().get(s);
77              }
78              if (r == null) {
79                  r = context.getSystemProperties().get(s);
80              }
81              return r;
82          });
83  
84          return pathTranslator.alignToBaseDirectory(absolutePath, basedir);
85      }
86  }