View Javadoc
1   /*
2    * Licensed under the Apache License, Version 2.0 (the "License");
3    * you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at
5    *
6    * http://www.apache.org/licenses/LICENSE-2.0
7    *
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS,
10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   * See the License for the specific language governing permissions and
12   * limitations under the License.
13   */
14  
15  package org.basepom.mojo.propertyhelper.definitions;
16  
17  import static com.google.common.collect.ImmutableSet.toImmutableSet;
18  
19  import org.basepom.mojo.propertyhelper.FieldContext;
20  import org.basepom.mojo.propertyhelper.IgnoreWarnFail;
21  import org.basepom.mojo.propertyhelper.groups.PropertyGroup;
22  
23  import java.util.Arrays;
24  import java.util.Set;
25  
26  import com.google.common.annotations.VisibleForTesting;
27  import com.google.common.collect.ImmutableSet;
28  
29  public class PropertyGroupDefinition {
30  
31      /**
32       * Property group id.
33       */
34      String id;
35  
36      /**
37       * Activate the group if the current project version does not contain SNAPSHOT. Field injected by Maven.
38       */
39      boolean activeOnRelease = true;
40  
41      /**
42       * Activate the group if the current project version contains SNAPSHOT. Field injected by Maven.
43       */
44      boolean activeOnSnapshot = true;
45  
46      /**
47       * Action if this property group defines a duplicate property. Field injected by Maven.
48       */
49      private IgnoreWarnFail onDuplicateProperty = IgnoreWarnFail.FAIL;
50  
51      public void setOnDuplicateProperty(String onDuplicateProperty) {
52          this.onDuplicateProperty = IgnoreWarnFail.forString(onDuplicateProperty);
53      }
54  
55      /**
56       * Action if any property from that group could not be defined. Field injected by Maven.
57       */
58      private IgnoreWarnFail onMissingField = IgnoreWarnFail.FAIL;
59  
60      public void setOnMissingField(String onMissingField) {
61          this.onMissingField = IgnoreWarnFail.forString(onMissingField);
62      }
63  
64      /**
65       * Property definitions in this group.
66       */
67      Set<PropertyDefinition> propertyDefinitions = Set.of();
68  
69      // called by maven
70      public void setProperties(PropertyDefinition... propertyDefinitions) {
71          this.propertyDefinitions = ImmutableSet.copyOf(Arrays.asList(propertyDefinitions));
72  
73          this.propertyDefinitions.forEach(PropertyDefinition::check);
74      }
75  
76      public PropertyGroupDefinition() {
77      }
78  
79      @VisibleForTesting
80      PropertyGroupDefinition(String id) {
81          this.id = id;
82      }
83  
84      public String getId() {
85          return id;
86      }
87  
88      public boolean isActiveOnRelease() {
89          return activeOnRelease;
90      }
91  
92      public boolean isActiveOnSnapshot() {
93          return activeOnSnapshot;
94      }
95  
96      public IgnoreWarnFail getOnDuplicateProperty() {
97          return onDuplicateProperty;
98      }
99  
100     public IgnoreWarnFail getOnMissingField() {
101         return onMissingField;
102     }
103 
104     public Set<PropertyDefinition> getPropertyDefinitions() {
105         return propertyDefinitions;
106     }
107 
108     public PropertyGroup createGroup(FieldContext context) {
109         return new PropertyGroup(this, context);
110     }
111 
112     public Set<String> getPropertyNames() {
113         return propertyDefinitions.stream()
114             .map(PropertyDefinition::getName)
115             .collect(toImmutableSet());
116     }
117 }