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