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.execution;
20  
21  import static java.util.stream.Collectors.toSet;
22  
23  import java.util.ArrayList;
24  import java.util.Collections;
25  import java.util.HashMap;
26  import java.util.List;
27  import java.util.Map;
28  import java.util.Set;
29  import java.util.function.Predicate;
30  
31  /**
32   * Container for storing the request from the user to activate or de-activate certain profiles and optionally fail the
33   * build if those profiles do not exist.
34   */
35  public class ProfileActivation {
36      private final Map<String, ActivationSettings> activations = new HashMap<>();
37  
38      /**
39       * Mimics the pre-Maven 4 "active profiles" list.
40       * @deprecated Use {@link #getRequiredActiveProfileIds()} and {@link #getOptionalActiveProfileIds()} instead.
41       */
42      @Deprecated
43      public List<String> getActiveProfiles() {
44          return Collections.unmodifiableList(new ArrayList<>(getProfileIds(pa -> pa.active)));
45      }
46  
47      /**
48       * Mimics the pre-Maven 4 "inactive profiles" list.
49       * @deprecated Use {@link #getRequiredInactiveProfileIds()} and {@link #getOptionalInactiveProfileIds()} instead.
50       */
51      @Deprecated
52      public List<String> getInactiveProfiles() {
53          return Collections.unmodifiableList(new ArrayList<>(getProfileIds(pa -> !pa.active)));
54      }
55  
56      /**
57       * Overwrites the active profiles based on a pre-Maven 4 "active profiles" list.
58       * @param activeProfileIds A {@link List} of profile IDs that must be activated.
59       * @deprecated Use {@link #activateOptionalProfile(String)} or {@link #activateRequiredProfile(String)} instead.
60       */
61      @Deprecated
62      public void overwriteActiveProfiles(List<String> activeProfileIds) {
63          getActiveProfiles().forEach(this.activations::remove);
64          activeProfileIds.forEach(this::activateOptionalProfile);
65      }
66  
67      /**
68       * Overwrites the inactive profiles based on a pre-Maven 4 "inactive profiles" list.
69       * @param inactiveProfileIds A {@link List} of profile IDs that must be deactivated.
70       * @deprecated Use {@link #deactivateOptionalProfile(String)} or {@link #deactivateRequiredProfile(String)} instead.
71       */
72      @Deprecated
73      public void overwriteInactiveProfiles(List<String> inactiveProfileIds) {
74          getInactiveProfiles().forEach(this.activations::remove);
75          inactiveProfileIds.forEach(this::deactivateOptionalProfile);
76      }
77  
78      /**
79       * Mark a profile as required and activated.
80       * @param id The identifier of the profile.
81       */
82      public void activateRequiredProfile(String id) {
83          this.activations.put(id, ActivationSettings.ACTIVATION_REQUIRED);
84      }
85  
86      /**
87       * Mark a profile as optional and activated.
88       * @param id The identifier of the profile.
89       */
90      public void activateOptionalProfile(String id) {
91          this.activations.put(id, ActivationSettings.ACTIVATION_OPTIONAL);
92      }
93  
94      /**
95       * Mark a profile as required and deactivated.
96       * @param id The identifier of the profile.
97       */
98      public void deactivateRequiredProfile(String id) {
99          this.activations.put(id, ActivationSettings.DEACTIVATION_REQUIRED);
100     }
101 
102     /**
103      * Mark a profile as optional and deactivated.
104      * @param id The identifier of the profile.
105      */
106     public void deactivateOptionalProfile(String id) {
107         this.activations.put(id, ActivationSettings.DEACTIVATION_OPTIONAL);
108     }
109 
110     /**
111      * Adds a profile activation to the request.
112      * @param id The identifier of the profile.
113      * @param active Should the profile be activated?
114      * @param optional Can the build continue if the profile does not exist?
115      */
116     public void addProfileActivation(String id, boolean active, boolean optional) {
117         final ActivationSettings settings = ActivationSettings.of(active, optional);
118         this.activations.put(id, settings);
119     }
120 
121     private Set<String> getProfileIds(final Predicate<ActivationSettings> predicate) {
122         return this.activations.entrySet().stream()
123                 .filter(e -> predicate.test(e.getValue()))
124                 .map(Map.Entry::getKey)
125                 .collect(toSet());
126     }
127 
128     /**
129      * @return Required active profile identifiers, never {@code null}.
130      */
131     public Set<String> getRequiredActiveProfileIds() {
132         return getProfileIds(pa -> !pa.optional && pa.active);
133     }
134 
135     /**
136      * @return Optional active profile identifiers, never {@code null}.
137      */
138     public Set<String> getOptionalActiveProfileIds() {
139         return getProfileIds(pa -> pa.optional && pa.active);
140     }
141 
142     /**
143      * @return Required inactive profile identifiers, never {@code null}.
144      */
145     public Set<String> getRequiredInactiveProfileIds() {
146         return getProfileIds(pa -> !pa.optional && !pa.active);
147     }
148 
149     /**
150      * @return Optional inactive profile identifiers, never {@code null}.
151      */
152     public Set<String> getOptionalInactiveProfileIds() {
153         return getProfileIds(pa -> pa.optional && !pa.active);
154     }
155 }