1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.model.profile.activation;
20
21 import javax.inject.Named;
22 import javax.inject.Singleton;
23 import org.apache.maven.model.Activation;
24 import org.apache.maven.model.ActivationProperty;
25 import org.apache.maven.model.Profile;
26 import org.apache.maven.model.building.ModelProblem.Severity;
27 import org.apache.maven.model.building.ModelProblem.Version;
28 import org.apache.maven.model.building.ModelProblemCollector;
29 import org.apache.maven.model.building.ModelProblemCollectorRequest;
30 import org.apache.maven.model.profile.ProfileActivationContext;
31 import org.codehaus.plexus.util.StringUtils;
32
33
34
35
36
37
38
39 @Named("property")
40 @Singleton
41 public class PropertyProfileActivator implements ProfileActivator {
42
43 @Override
44 public boolean isActive(Profile profile, ProfileActivationContext context, ModelProblemCollector problems) {
45 Activation activation = profile.getActivation();
46
47 if (activation == null) {
48 return false;
49 }
50
51 ActivationProperty property = activation.getProperty();
52
53 if (property == null) {
54 return false;
55 }
56
57 String name = property.getName();
58 boolean reverseName = false;
59
60 if (name != null && name.startsWith("!")) {
61 reverseName = true;
62 name = name.substring(1);
63 }
64
65 if (name == null || name.length() <= 0) {
66 problems.add(new ModelProblemCollectorRequest(Severity.ERROR, Version.BASE)
67 .setMessage("The property name is required to activate the profile " + profile.getId())
68 .setLocation(property.getLocation("")));
69 return false;
70 }
71
72 String sysValue = context.getUserProperties().get(name);
73 if (sysValue == null) {
74 sysValue = context.getSystemProperties().get(name);
75 }
76
77 String propValue = property.getValue();
78 if (StringUtils.isNotEmpty(propValue)) {
79 boolean reverseValue = false;
80 if (propValue.startsWith("!")) {
81 reverseValue = true;
82 propValue = propValue.substring(1);
83 }
84
85
86 return reverseValue != propValue.equals(sysValue);
87 } else {
88 return reverseName != StringUtils.isNotEmpty(sysValue);
89 }
90 }
91
92 @Override
93 public boolean presentInConfig(Profile profile, ProfileActivationContext context, ModelProblemCollector problems) {
94 Activation activation = profile.getActivation();
95
96 if (activation == null) {
97 return false;
98 }
99
100 ActivationProperty property = activation.getProperty();
101
102 return property != null;
103 }
104 }