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.profile;
20  
21  import org.apache.maven.api.di.Named;
22  import org.apache.maven.api.di.Singleton;
23  import org.apache.maven.api.model.Activation;
24  import org.apache.maven.api.model.ActivationFile;
25  import org.apache.maven.api.model.Profile;
26  import org.apache.maven.api.services.BuilderProblem;
27  import org.apache.maven.api.services.MavenException;
28  import org.apache.maven.api.services.ModelProblem;
29  import org.apache.maven.api.services.ModelProblemCollector;
30  import org.apache.maven.api.services.model.ProfileActivationContext;
31  import org.apache.maven.api.services.model.ProfileActivator;
32  
33  /**
34   * Determines profile activation based on the existence/absence of some file.
35   * File name interpolation support is limited to <code>${project.basedir}</code>
36   * system properties and user properties.
37   *
38   * @see ActivationFile
39   * @see org.apache.maven.internal.impl.model.DefaultModelValidator#validateRawModel
40   */
41  @Named("file")
42  @Singleton
43  public class FileProfileActivator implements ProfileActivator {
44  
45      @Override
46      public boolean isActive(Profile profile, ProfileActivationContext context, ModelProblemCollector problems) {
47          Activation activation = profile.getActivation();
48  
49          if (activation == null) {
50              return false;
51          }
52  
53          ActivationFile file = activation.getFile();
54  
55          if (file == null) {
56              return false;
57          }
58  
59          String path;
60          boolean missing;
61  
62          boolean hasExists = file.getExists() != null && !file.getExists().isEmpty();
63          boolean hasMissing = file.getMissing() != null && !file.getMissing().isEmpty();
64          if (hasExists) {
65              if (hasMissing) {
66                  problems.add(
67                          BuilderProblem.Severity.WARNING,
68                          ModelProblem.Version.BASE,
69                          String.format(
70                                  "Profile '%s' file activation conflict: Both 'missing' (%s) and 'exists' assertions are defined. "
71                                          + "The 'missing' assertion will be ignored. Please remove one assertion to resolve this conflict.",
72                                  profile.getId(), file.getMissing()),
73                          file.getLocation("missing"));
74              }
75              path = file.getExists();
76              missing = false;
77          } else if (hasMissing) {
78              path = file.getMissing();
79              missing = true;
80          } else {
81              return false;
82          }
83  
84          boolean fileExists;
85          try {
86              fileExists = context.exists(path, false);
87          } catch (MavenException e) {
88              problems.add(
89                      BuilderProblem.Severity.ERROR,
90                      ModelProblem.Version.BASE,
91                      "Failed to check file existence " + path + " for profile " + profile.getId() + ": "
92                              + e.getMessage(),
93                      file.getLocation(missing ? "missing" : "exists"),
94                      e);
95              return false;
96          }
97  
98          return missing != fileExists;
99      }
100 
101     @Override
102     public boolean presentInConfig(Profile profile, ProfileActivationContext context, ModelProblemCollector problems) {
103         Activation activation = profile.getActivation();
104 
105         if (activation == null) {
106             return false;
107         }
108 
109         ActivationFile file = activation.getFile();
110 
111         return file != null;
112     }
113 }