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 java.util.ArrayList;
22  import java.util.Collections;
23  import java.util.List;
24  import java.util.Set;
25  import java.util.function.Predicate;
26  import java.util.stream.Collectors;
27  import java.util.stream.Stream;
28  
29  import static java.util.stream.Collectors.toSet;
30  
31  /**
32   * Container for storing the request from the user to activate or deactivate certain projects and optionally fail the
33   * build if those projects do not exist.
34   */
35  public class ProjectActivation {
36      private static class ProjectActivationSettings {
37          /**
38           * The selector of a project. This can be the project directory, [groupId]:[artifactId] or :[artifactId].
39           */
40          final String selector;
41  
42          /**
43           * This describes how/when to active or deactivate the project.
44           */
45          final ActivationSettings activationSettings;
46  
47          ProjectActivationSettings(String selector, ActivationSettings activationSettings) {
48              this.selector = selector;
49              this.activationSettings = activationSettings;
50          }
51      }
52  
53      /**
54       * List of activated and deactivated projects.
55       */
56      private final List<ProjectActivationSettings> activations = new ArrayList<>();
57  
58      /**
59       * Adds a project activation to the request.
60       * @param selector The selector of the project.
61       * @param active Should the project be activated?
62       * @param optional Can the build continue if the project does not exist?
63       */
64      public void addProjectActivation(String selector, boolean active, boolean optional) {
65          final ActivationSettings settings = ActivationSettings.of(active, optional);
66          this.activations.add(new ProjectActivationSettings(selector, settings));
67      }
68  
69      private Stream<ProjectActivationSettings> getProjects(final Predicate<ActivationSettings> predicate) {
70          return this.activations.stream().filter(activation -> predicate.test(activation.activationSettings));
71      }
72  
73      private Set<String> getProjectSelectors(final Predicate<ActivationSettings> predicate) {
74          return getProjects(predicate).map(activation -> activation.selector).collect(toSet());
75      }
76  
77      /**
78       * @return Required active project selectors, never {@code null}.
79       */
80      public Set<String> getRequiredActiveProjectSelectors() {
81          return getProjectSelectors(pa -> !pa.optional && pa.active);
82      }
83  
84      /**
85       * @return Optional active project selectors, never {@code null}.
86       */
87      public Set<String> getOptionalActiveProjectSelectors() {
88          return getProjectSelectors(pa -> pa.optional && pa.active);
89      }
90  
91      /**
92       * @return Required inactive project selectors, never {@code null}.
93       */
94      public Set<String> getRequiredInactiveProjectSelectors() {
95          return getProjectSelectors(pa -> !pa.optional && !pa.active);
96      }
97  
98      /**
99       * @return Optional inactive project selectors, never {@code null}.
100      */
101     public Set<String> getOptionalInactiveProjectSelectors() {
102         return getProjectSelectors(pa -> pa.optional && !pa.active);
103     }
104 
105     /**
106      * Mimics the pre-Maven 4 "selected projects" list.
107      * @deprecated Use {@link #getRequiredActiveProjectSelectors()} and {@link #getOptionalActiveProjectSelectors()}
108      * instead.
109      */
110     @Deprecated
111     public List<String> getSelectedProjects() {
112         return Collections.unmodifiableList(new ArrayList<>(getProjectSelectors(pa -> pa.active)));
113     }
114 
115     /**
116      * Mimics the pre-Maven 4 "excluded projects" list.
117      * @deprecated Use {@link #getRequiredInactiveProjectSelectors()} and {@link #getOptionalInactiveProjectSelectors()}
118      * instead.
119      */
120     @Deprecated
121     public List<String> getExcludedProjects() {
122         return Collections.unmodifiableList(new ArrayList<>(getProjectSelectors(pa -> !pa.active)));
123     }
124 
125     /**
126      * Overwrites the active projects based on a pre-Maven 4 "active projects" list.
127      * @param activeProjectSelectors A {@link List} of project selectors that must be activated.
128      * @deprecated Use {@link #activateOptionalProject(String)} or {@link #activateRequiredProject(String)} instead.
129      */
130     @Deprecated
131     public void overwriteActiveProjects(List<String> activeProjectSelectors) {
132         List<ProjectActivationSettings> projects = getProjects(pa -> pa.active).collect(Collectors.toList());
133         this.activations.removeAll(projects);
134         activeProjectSelectors.forEach(this::activateOptionalProject);
135     }
136 
137     /**
138      * Overwrites the inactive projects based on a pre-Maven 4 "inactive projects" list.
139      * @param inactiveProjectSelectors A {@link List} of project selectors that must be deactivated.
140      * @deprecated Use {@link #deactivateOptionalProject(String)} or {@link #deactivateRequiredProject(String)} instead.
141      */
142     @Deprecated
143     public void overwriteInactiveProjects(List<String> inactiveProjectSelectors) {
144         List<ProjectActivationSettings> projects = getProjects(pa -> !pa.active).collect(Collectors.toList());
145         this.activations.removeAll(projects);
146         inactiveProjectSelectors.forEach(this::deactivateOptionalProject);
147     }
148 
149     /**
150      * Mark a project as required and activated.
151      * @param selector The selector of the project.
152      */
153     public void activateRequiredProject(String selector) {
154         this.activations.add(new ProjectActivationSettings(selector, ActivationSettings.ACTIVATION_REQUIRED));
155     }
156 
157     /**
158      * Mark a project as optional and activated.
159      * @param selector The selector of the project.
160      */
161     public void activateOptionalProject(String selector) {
162         this.activations.add(new ProjectActivationSettings(selector, ActivationSettings.ACTIVATION_OPTIONAL));
163     }
164 
165     /**
166      * Mark a project as required and deactivated.
167      * @param selector The selector of the project.
168      */
169     public void deactivateRequiredProject(String selector) {
170         this.activations.add(new ProjectActivationSettings(selector, ActivationSettings.DEACTIVATION_REQUIRED));
171     }
172 
173     /**
174      * Mark a project as optional and deactivated.
175      * @param selector The selector of the project.
176      */
177     public void deactivateOptionalProject(String selector) {
178         this.activations.add(new ProjectActivationSettings(selector, ActivationSettings.DEACTIVATION_OPTIONAL));
179     }
180 
181     public boolean isEmpty() {
182         return this.activations.isEmpty();
183     }
184 }