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   * @see ActivationProperty
38   * @deprecated use {@code org.apache.maven.api.services.ModelBuilder} instead
39   */
40  @Named("property")
41  @Singleton
42  @Deprecated(since = "4.0.0")
43  public class PropertyProfileActivator 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          ActivationProperty property = activation.getProperty();
54  
55          if (property == null) {
56              return false;
57          }
58  
59          String name = property.getName();
60          boolean reverseName = false;
61  
62          if (name != null && name.startsWith("!")) {
63              reverseName = true;
64              name = name.substring(1);
65          }
66  
67          if (name == null || name.length() <= 0) {
68              problems.add(new ModelProblemCollectorRequest(Severity.ERROR, Version.BASE)
69                      .setMessage("The property name is required to activate the profile " + profile.getId())
70                      .setLocation(property.getLocation("")));
71              return false;
72          }
73  
74          // Lookup order: user (-D) → system (java.version, os.name, …).
75          // In external model builds the caller provides a sandboxed context
76          // (see DefaultModelBuilder.externalActivationContext()) that suppresses user properties
77          // and merges model properties into system properties, so consumer -D flags cannot
78          // activate dependency profiles while POM-declared properties still drive activation.
79          String sysValue = context.getUserProperties().get(name);
80          if (sysValue == null) {
81              sysValue = context.getSystemProperties().get(name);
82          }
83  
84          String propValue = property.getValue();
85          if (StringUtils.isNotEmpty(propValue)) {
86              boolean reverseValue = false;
87              if (propValue.startsWith("!")) {
88                  reverseValue = true;
89                  propValue = propValue.substring(1);
90              }
91  
92              // we have a value, so it has to match the system value...
93              boolean result = propValue.equals(sysValue);
94  
95              return reverseValue ? !result : result;
96          } else {
97              boolean result = StringUtils.isNotEmpty(sysValue);
98  
99              return reverseName ? !result : result;
100         }
101     }
102 
103     @Override
104     public boolean presentInConfig(Profile profile, ProfileActivationContext context, ModelProblemCollector problems) {
105         Activation activation = profile.getActivation();
106 
107         if (activation == null) {
108             return false;
109         }
110 
111         ActivationProperty property = activation.getProperty();
112 
113         if (property == null) {
114             return false;
115         }
116         return true;
117     }
118 }