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.api.services;
20  
21  import java.nio.file.Path;
22  import java.util.Collection;
23  import java.util.List;
24  import java.util.Map;
25  
26  import org.apache.maven.api.RemoteRepository;
27  import org.apache.maven.api.Session;
28  import org.apache.maven.api.annotations.Experimental;
29  import org.apache.maven.api.annotations.Immutable;
30  import org.apache.maven.api.annotations.Nonnull;
31  import org.apache.maven.api.annotations.NotThreadSafe;
32  import org.apache.maven.api.annotations.Nullable;
33  import org.apache.maven.api.model.Profile;
34  
35  import static org.apache.maven.api.services.BaseRequest.nonNull;
36  
37  /**
38   * Request used to build a {@link org.apache.maven.api.Project} using
39   * the {@link ProjectBuilder} service.
40   *
41   * @since 4.0.0
42   */
43  @Experimental
44  @Immutable
45  public interface ModelBuilderRequest {
46  
47      /**
48       * The possible request types for building a model.
49       */
50      enum RequestType {
51          /**
52           * The request is for building an initial model from a POM file in a project on the filesystem.
53           */
54          BUILD_PROJECT,
55          /**
56           * The request is for rebuilding the effective POM in a project on the filesystem.
57           */
58          BUILD_EFFECTIVE,
59          /**
60           * The request is used specifically to parse the POM used as a basis for creating the consumer POM.
61           * This POM will not ungergo any profile activation.
62           */
63          BUILD_CONSUMER,
64          /**
65           * The request is for building a model from a parent POM file from a downloaded artifact.
66           */
67          CONSUMER_PARENT,
68          /**
69           * The request is for building a model from a dependency POM file from a downloaded artifact.
70           */
71          CONSUMER_DEPENDENCY
72      }
73  
74      /**
75       * The possible merge modes for combining remote repositories.
76       */
77      enum RepositoryMerging {
78  
79          /**
80           * The repositories declared in the POM have precedence over the repositories specified in the request.
81           */
82          POM_DOMINANT,
83  
84          /**
85           * The repositories specified in the request have precedence over the repositories declared in the POM.
86           */
87          REQUEST_DOMINANT,
88      }
89  
90      @Nonnull
91      Session getSession();
92  
93      @Nonnull
94      ModelSource getSource();
95  
96      @Nonnull
97      RequestType getRequestType();
98  
99      boolean isLocationTracking();
100 
101     boolean isRecursive();
102 
103     /**
104      * Defines external profiles that may be activated for the given model.
105      * Those are external profiles usually defined in {@link org.apache.maven.api.settings.Settings#getProfiles()}.
106      */
107     @Nonnull
108     Collection<Profile> getProfiles();
109 
110     /**
111      * List of profile ids that have been explicitly activated by the user.
112      */
113     @Nonnull
114     List<String> getActiveProfileIds();
115 
116     /**
117      * List of profile ids that have been explicitly deactivated by the user.
118      */
119     @Nonnull
120     List<String> getInactiveProfileIds();
121 
122     /**
123      * Provides a map of system properties.
124      */
125     @Nonnull
126     Map<String, String> getSystemProperties();
127 
128     /**
129      * Provides a map of user properties.
130      * User properties
131      */
132     @Nonnull
133     Map<String, String> getUserProperties();
134 
135     @Nonnull
136     RepositoryMerging getRepositoryMerging();
137 
138     @Nullable
139     List<RemoteRepository> getRepositories();
140 
141     @Nullable
142     ModelTransformer getLifecycleBindingsInjector();
143 
144     @Nonnull
145     static ModelBuilderRequest build(@Nonnull ModelBuilderRequest request, @Nonnull ModelSource source) {
146         return builder(nonNull(request, "request cannot be null"))
147                 .source(nonNull(source, "source cannot be null"))
148                 .build();
149     }
150 
151     @Nonnull
152     static ModelBuilderRequest build(@Nonnull Session session, @Nonnull ModelSource source) {
153         return builder()
154                 .session(nonNull(session, "session cannot be null"))
155                 .source(nonNull(source, "source cannot be null"))
156                 .build();
157     }
158 
159     @Nonnull
160     static ModelBuilderRequest build(@Nonnull Session session, @Nonnull Path path) {
161         return builder()
162                 .session(nonNull(session, "session cannot be null"))
163                 .source(ModelSource.fromPath(path))
164                 .build();
165     }
166 
167     @Nonnull
168     static ModelBuilderRequestBuilder builder() {
169         return new ModelBuilderRequestBuilder();
170     }
171 
172     @Nonnull
173     static ModelBuilderRequestBuilder builder(ModelBuilderRequest request) {
174         return new ModelBuilderRequestBuilder(request);
175     }
176 
177     @NotThreadSafe
178     class ModelBuilderRequestBuilder {
179         Session session;
180         RequestType requestType;
181         boolean locationTracking;
182         boolean recursive;
183         ModelSource source;
184         Collection<Profile> profiles;
185         List<String> activeProfileIds;
186         List<String> inactiveProfileIds;
187         Map<String, String> systemProperties;
188         Map<String, String> userProperties;
189         RepositoryMerging repositoryMerging;
190         List<RemoteRepository> repositories;
191         ModelTransformer lifecycleBindingsInjector;
192 
193         ModelBuilderRequestBuilder() {}
194 
195         ModelBuilderRequestBuilder(ModelBuilderRequest request) {
196             this.session = request.getSession();
197             this.requestType = request.getRequestType();
198             this.locationTracking = request.isLocationTracking();
199             this.recursive = request.isRecursive();
200             this.source = request.getSource();
201             this.profiles = request.getProfiles();
202             this.activeProfileIds = request.getActiveProfileIds();
203             this.inactiveProfileIds = request.getInactiveProfileIds();
204             this.systemProperties = request.getSystemProperties();
205             this.userProperties = request.getUserProperties();
206             this.repositoryMerging = request.getRepositoryMerging();
207             this.repositories = request.getRepositories();
208             this.lifecycleBindingsInjector = request.getLifecycleBindingsInjector();
209         }
210 
211         public ModelBuilderRequestBuilder session(Session session) {
212             this.session = session;
213             return this;
214         }
215 
216         public ModelBuilderRequestBuilder requestType(RequestType requestType) {
217             this.requestType = requestType;
218             return this;
219         }
220 
221         public ModelBuilderRequestBuilder locationTracking(boolean locationTracking) {
222             this.locationTracking = locationTracking;
223             return this;
224         }
225 
226         public ModelBuilderRequestBuilder recursive(boolean recursive) {
227             this.recursive = recursive;
228             return this;
229         }
230 
231         public ModelBuilderRequestBuilder source(ModelSource source) {
232             this.source = source;
233             return this;
234         }
235 
236         public ModelBuilderRequestBuilder profiles(List<Profile> profiles) {
237             this.profiles = profiles;
238             return this;
239         }
240 
241         public ModelBuilderRequestBuilder activeProfileIds(List<String> activeProfileIds) {
242             this.activeProfileIds = activeProfileIds;
243             return this;
244         }
245 
246         public ModelBuilderRequestBuilder inactiveProfileIds(List<String> inactiveProfileIds) {
247             this.inactiveProfileIds = inactiveProfileIds;
248             return this;
249         }
250 
251         public ModelBuilderRequestBuilder systemProperties(Map<String, String> systemProperties) {
252             this.systemProperties = systemProperties;
253             return this;
254         }
255 
256         public ModelBuilderRequestBuilder userProperties(Map<String, String> userProperties) {
257             this.userProperties = userProperties;
258             return this;
259         }
260 
261         public ModelBuilderRequestBuilder repositoryMerging(RepositoryMerging repositoryMerging) {
262             this.repositoryMerging = repositoryMerging;
263             return this;
264         }
265 
266         public ModelBuilderRequestBuilder repositories(List<RemoteRepository> repositories) {
267             this.repositories = repositories;
268             return this;
269         }
270 
271         public ModelBuilderRequestBuilder lifecycleBindingsInjector(ModelTransformer lifecycleBindingsInjector) {
272             this.lifecycleBindingsInjector = lifecycleBindingsInjector;
273             return this;
274         }
275 
276         public ModelBuilderRequest build() {
277             return new DefaultModelBuilderRequest(
278                     session,
279                     requestType,
280                     locationTracking,
281                     recursive,
282                     source,
283                     profiles,
284                     activeProfileIds,
285                     inactiveProfileIds,
286                     systemProperties,
287                     userProperties,
288                     repositoryMerging,
289                     repositories,
290                     lifecycleBindingsInjector);
291         }
292 
293         private static class DefaultModelBuilderRequest extends BaseRequest<Session> implements ModelBuilderRequest {
294             private final RequestType requestType;
295             private final boolean locationTracking;
296             private final boolean recursive;
297             private final ModelSource source;
298             private final Collection<Profile> profiles;
299             private final List<String> activeProfileIds;
300             private final List<String> inactiveProfileIds;
301             private final Map<String, String> systemProperties;
302             private final Map<String, String> userProperties;
303             private final RepositoryMerging repositoryMerging;
304             private final List<RemoteRepository> repositories;
305             private final ModelTransformer lifecycleBindingsInjector;
306 
307             @SuppressWarnings("checkstyle:ParameterNumber")
308             DefaultModelBuilderRequest(
309                     @Nonnull Session session,
310                     @Nonnull RequestType requestType,
311                     boolean locationTracking,
312                     boolean recursive,
313                     @Nonnull ModelSource source,
314                     Collection<Profile> profiles,
315                     List<String> activeProfileIds,
316                     List<String> inactiveProfileIds,
317                     Map<String, String> systemProperties,
318                     Map<String, String> userProperties,
319                     RepositoryMerging repositoryMerging,
320                     List<RemoteRepository> repositories,
321                     ModelTransformer lifecycleBindingsInjector) {
322                 super(session);
323                 this.requestType = nonNull(requestType, "requestType cannot be null");
324                 this.locationTracking = locationTracking;
325                 this.recursive = recursive;
326                 this.source = source;
327                 this.profiles = profiles != null ? List.copyOf(profiles) : List.of();
328                 this.activeProfileIds = activeProfileIds != null ? List.copyOf(activeProfileIds) : List.of();
329                 this.inactiveProfileIds = inactiveProfileIds != null ? List.copyOf(inactiveProfileIds) : List.of();
330                 this.systemProperties =
331                         systemProperties != null ? Map.copyOf(systemProperties) : session.getSystemProperties();
332                 this.userProperties = userProperties != null ? Map.copyOf(userProperties) : session.getUserProperties();
333                 this.repositoryMerging = repositoryMerging;
334                 this.repositories = repositories != null ? List.copyOf(repositories) : null;
335                 this.lifecycleBindingsInjector = lifecycleBindingsInjector;
336             }
337 
338             @Override
339             public RequestType getRequestType() {
340                 return requestType;
341             }
342 
343             @Override
344             public boolean isLocationTracking() {
345                 return locationTracking;
346             }
347 
348             @Override
349             public boolean isRecursive() {
350                 return recursive;
351             }
352 
353             @Nonnull
354             @Override
355             public ModelSource getSource() {
356                 return source;
357             }
358 
359             @Override
360             public Collection<Profile> getProfiles() {
361                 return profiles;
362             }
363 
364             @Override
365             public List<String> getActiveProfileIds() {
366                 return activeProfileIds;
367             }
368 
369             @Override
370             public List<String> getInactiveProfileIds() {
371                 return inactiveProfileIds;
372             }
373 
374             @Override
375             public Map<String, String> getSystemProperties() {
376                 return systemProperties;
377             }
378 
379             @Override
380             public Map<String, String> getUserProperties() {
381                 return userProperties;
382             }
383 
384             @Override
385             public RepositoryMerging getRepositoryMerging() {
386                 return repositoryMerging;
387             }
388 
389             @Override
390             public List<RemoteRepository> getRepositories() {
391                 return repositories;
392             }
393 
394             @Override
395             public ModelTransformer getLifecycleBindingsInjector() {
396                 return lifecycleBindingsInjector;
397             }
398         }
399     }
400 }