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