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
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
36
37
38
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
74
75
76
77
78
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
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 }