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.profile.activation;
20  
21  import javax.inject.Named;
22  import javax.inject.Singleton;
23  
24  import org.apache.maven.model.Activation;
25  import org.apache.maven.model.ActivationProperty;
26  import org.apache.maven.model.Profile;
27  import org.apache.maven.model.building.ModelProblem.Severity;
28  import org.apache.maven.model.building.ModelProblem.Version;
29  import org.apache.maven.model.building.ModelProblemCollector;
30  import org.apache.maven.model.building.ModelProblemCollectorRequest;
31  import org.apache.maven.model.profile.ProfileActivationContext;
32  import org.codehaus.plexus.util.StringUtils;
33  
34  /**
35   * Determines profile activation based on the existence or value of some execution property.
36   *
37   * @author Benjamin Bentmann
38   * @see ActivationProperty
39   */
40  @Named("property")
41  @Singleton
42  public class PropertyProfileActivator implements ProfileActivator {
43  
44      @Override
45      public boolean isActive(Profile profile, ProfileActivationContext context, ModelProblemCollector problems) {
46          Activation activation = profile.getActivation();
47  
48          if (activation == null) {
49              return false;
50          }
51  
52          ActivationProperty property = activation.getProperty();
53  
54          if (property == null) {
55              return false;
56          }
57  
58          String name = property.getName();
59          boolean reverseName = false;
60  
61          if (name != null && name.startsWith("!")) {
62              reverseName = true;
63              name = name.substring(1);
64          }
65  
66          if (name == null || name.length() <= 0) {
67              problems.add(new ModelProblemCollectorRequest(Severity.ERROR, Version.BASE)
68                      .setMessage("The property name is required to activate the profile " + profile.getId())
69                      .setLocation(property.getLocation("")));
70              return false;
71          }
72  
73          // Lookup order: user (-D) → system (java.version, os.name, …).
74          // In external model builds the caller provides a sandboxed context
75          // (see DefaultModelBuilder.externalActivationContext()) that suppresses user properties
76          // and merges model properties into system properties, so consumer -D flags cannot
77          // activate dependency profiles while POM-declared properties still drive activation.
78          String sysValue = context.getUserProperties().get(name);
79          if (sysValue == null) {
80              sysValue = context.getSystemProperties().get(name);
81          }
82  
83          String propValue = property.getValue();
84          if (StringUtils.isNotEmpty(propValue)) {
85              boolean reverseValue = false;
86              if (propValue.startsWith("!")) {
87                  reverseValue = true;
88                  propValue = propValue.substring(1);
89              }
90  
91              // we have a value, so it has to match the system value...
92              boolean result = propValue.equals(sysValue);
93  
94              return reverseValue ? !result : result;
95          } else {
96              boolean result = StringUtils.isNotEmpty(sysValue);
97  
98              return reverseName ? !result : result;
99          }
100     }
101 
102     @Override
103     public boolean presentInConfig(Profile profile, ProfileActivationContext context, ModelProblemCollector problems) {
104         Activation activation = profile.getActivation();
105 
106         if (activation == null) {
107             return false;
108         }
109 
110         ActivationProperty property = activation.getProperty();
111 
112         if (property == null) {
113             return false;
114         }
115         return true;
116     }
117 }