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