View Javadoc
1   package org.apache.maven.execution;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.ArrayList;
23  import java.util.Collections;
24  import java.util.HashMap;
25  import java.util.List;
26  import java.util.Map;
27  import java.util.Set;
28  import java.util.function.Predicate;
29  
30  import static java.util.stream.Collectors.toSet;
31  
32  /**
33   * Container for storing the request from the user to activate or de-activate certain profiles and optionally fail the
34   * build if those profiles do not exist.
35   */
36  public class ProfileActivation
37  {
38      private final Map<String, ActivationSettings> activations = new HashMap<>();
39  
40      /**
41       * Mimics the pre-Maven 4 "active profiles" list.
42       * @deprecated Use {@link #getRequiredActiveProfileIds()} and {@link #getOptionalActiveProfileIds()} instead.
43       */
44      @Deprecated
45      public List<String> getActiveProfiles()
46      {
47          return Collections.unmodifiableList( new ArrayList<>( getProfileIds( pa -> pa.active ) ) );
48      }
49  
50      /**
51       * Mimics the pre-Maven 4 "inactive profiles" list.
52       * @deprecated Use {@link #getRequiredInactiveProfileIds()} and {@link #getOptionalInactiveProfileIds()} instead.
53       */
54      @Deprecated
55      public List<String> getInactiveProfiles()
56      {
57          return Collections.unmodifiableList( new ArrayList<>( getProfileIds( pa -> !pa.active ) ) );
58      }
59  
60      /**
61       * Overwrites the active profiles based on a pre-Maven 4 "active profiles" list.
62       * @param activeProfileIds A {@link List} of profile IDs that must be activated.
63       * @deprecated Use {@link #activateOptionalProfile(String)} or {@link #activateRequiredProfile(String)} instead.
64       */
65      @Deprecated
66      public void overwriteActiveProfiles( List<String> activeProfileIds )
67      {
68          getActiveProfiles().forEach( this.activations::remove );
69          activeProfileIds.forEach( this::activateOptionalProfile );
70      }
71  
72      /**
73       * Overwrites the inactive profiles based on a pre-Maven 4 "inactive profiles" list.
74       * @param inactiveProfileIds A {@link List} of profile IDs that must be deactivated.
75       * @deprecated Use {@link #deactivateOptionalProfile(String)} or {@link #deactivateRequiredProfile(String)} instead.
76       */
77      @Deprecated
78      public void overwriteInactiveProfiles( List<String> inactiveProfileIds )
79      {
80          getInactiveProfiles().forEach( this.activations::remove );
81          inactiveProfileIds.forEach( this::deactivateOptionalProfile );
82      }
83  
84      /**
85       * Mark a profile as required and activated.
86       * @param id The identifier of the profile.
87       */
88      public void activateRequiredProfile( String id )
89      {
90          this.activations.put( id, ActivationSettings.ACTIVATION_REQUIRED );
91      }
92  
93      /**
94       * Mark a profile as optional and activated.
95       * @param id The identifier of the profile.
96       */
97      public void activateOptionalProfile( String id )
98      {
99          this.activations.put( id, ActivationSettings.ACTIVATION_OPTIONAL );
100     }
101 
102     /**
103      * Mark a profile as required and deactivated.
104      * @param id The identifier of the profile.
105      */
106     public void deactivateRequiredProfile( String id )
107     {
108         this.activations.put( id, ActivationSettings.DEACTIVATION_REQUIRED );
109     }
110 
111     /**
112      * Mark a profile as optional and deactivated.
113      * @param id The identifier of the profile.
114      */
115     public void deactivateOptionalProfile( String id )
116     {
117         this.activations.put( id, ActivationSettings.DEACTIVATION_OPTIONAL );
118     }
119 
120     /**
121      * Adds a profile activation to the request.
122      * @param id The identifier of the profile.
123      * @param active Should the profile be activated?
124      * @param optional Can the build continue if the profile does not exist?
125      */
126     public void addProfileActivation( String id, boolean active, boolean optional )
127     {
128         final ActivationSettings settings = ActivationSettings.of( active, optional );
129         this.activations.put( id, settings );
130     }
131 
132     private Set<String> getProfileIds( final Predicate<ActivationSettings> predicate )
133     {
134         return this.activations.entrySet().stream()
135                 .filter( e -> predicate.test( e.getValue() ) )
136                 .map( Map.Entry::getKey )
137                 .collect( toSet() );
138     }
139 
140     /**
141      * @return Required active profile identifiers, never {@code null}.
142      */
143     public Set<String> getRequiredActiveProfileIds()
144     {
145         return getProfileIds( pa -> !pa.optional && pa.active );
146     }
147 
148     /**
149      * @return Optional active profile identifiers, never {@code null}.
150      */
151     public Set<String> getOptionalActiveProfileIds()
152     {
153         return getProfileIds( pa -> pa.optional && pa.active );
154     }
155 
156     /**
157      * @return Required inactive profile identifiers, never {@code null}.
158      */
159     public Set<String> getRequiredInactiveProfileIds()
160     {
161         return getProfileIds( pa -> !pa.optional && !pa.active );
162     }
163 
164     /**
165      * @return Optional inactive profile identifiers, never {@code null}.
166      */
167     public Set<String> getOptionalInactiveProfileIds()
168     {
169         return getProfileIds( pa -> pa.optional && !pa.active );
170     }
171 }