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 RepositoryAwareRequest {
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     ModelTransformer getLifecycleBindingsInjector();
138 
139     @Nonnull
140     static ModelBuilderRequest build(@Nonnull ModelBuilderRequest request, @Nonnull ModelSource source) {
141         return builder(requireNonNull(request, "request cannot be null"))
142                 .source(requireNonNull(source, "source cannot be null"))
143                 .build();
144     }
145 
146     @Nonnull
147     static ModelBuilderRequest build(@Nonnull Session session, @Nonnull ModelSource source) {
148         return builder()
149                 .session(requireNonNull(session, "session cannot be null"))
150                 .source(requireNonNull(source, "source cannot be null"))
151                 .build();
152     }
153 
154     @Nonnull
155     static ModelBuilderRequest build(@Nonnull Session session, @Nonnull Path path) {
156         return builder()
157                 .session(requireNonNull(session, "session cannot be null"))
158                 .source(Sources.buildSource(path))
159                 .build();
160     }
161 
162     @Nonnull
163     static ModelBuilderRequestBuilder builder() {
164         return new ModelBuilderRequestBuilder();
165     }
166 
167     @Nonnull
168     static ModelBuilderRequestBuilder builder(ModelBuilderRequest request) {
169         return new ModelBuilderRequestBuilder(request);
170     }
171 
172     @NotThreadSafe
173     class ModelBuilderRequestBuilder {
174         Session session;
175         RequestTrace trace;
176         RequestType requestType;
177         boolean locationTracking;
178         boolean recursive;
179         ModelSource source;
180         Collection<Profile> profiles;
181         List<String> activeProfileIds;
182         List<String> inactiveProfileIds;
183         Map<String, String> systemProperties;
184         Map<String, String> userProperties;
185         RepositoryMerging repositoryMerging;
186         List<RemoteRepository> repositories;
187         ModelTransformer lifecycleBindingsInjector;
188 
189         ModelBuilderRequestBuilder() {}
190 
191         ModelBuilderRequestBuilder(ModelBuilderRequest request) {
192             this.session = request.getSession();
193             this.trace = request.getTrace();
194             this.requestType = request.getRequestType();
195             this.locationTracking = request.isLocationTracking();
196             this.recursive = request.isRecursive();
197             this.source = request.getSource();
198             this.profiles = request.getProfiles();
199             this.activeProfileIds = request.getActiveProfileIds();
200             this.inactiveProfileIds = request.getInactiveProfileIds();
201             this.systemProperties = request.getSystemProperties();
202             this.userProperties = request.getUserProperties();
203             this.repositoryMerging = request.getRepositoryMerging();
204             this.repositories = request.getRepositories();
205             this.lifecycleBindingsInjector = request.getLifecycleBindingsInjector();
206         }
207 
208         public ModelBuilderRequestBuilder session(Session session) {
209             this.session = session;
210             return this;
211         }
212 
213         public ModelBuilderRequestBuilder trace(RequestTrace trace) {
214             this.trace = trace;
215             return this;
216         }
217 
218         public ModelBuilderRequestBuilder requestType(RequestType requestType) {
219             this.requestType = requestType;
220             return this;
221         }
222 
223         public ModelBuilderRequestBuilder locationTracking(boolean locationTracking) {
224             this.locationTracking = locationTracking;
225             return this;
226         }
227 
228         public ModelBuilderRequestBuilder recursive(boolean recursive) {
229             this.recursive = recursive;
230             return this;
231         }
232 
233         public ModelBuilderRequestBuilder source(ModelSource source) {
234             this.source = source;
235             return this;
236         }
237 
238         public ModelBuilderRequestBuilder profiles(List<Profile> profiles) {
239             this.profiles = profiles;
240             return this;
241         }
242 
243         public ModelBuilderRequestBuilder activeProfileIds(List<String> activeProfileIds) {
244             this.activeProfileIds = activeProfileIds;
245             return this;
246         }
247 
248         public ModelBuilderRequestBuilder inactiveProfileIds(List<String> inactiveProfileIds) {
249             this.inactiveProfileIds = inactiveProfileIds;
250             return this;
251         }
252 
253         public ModelBuilderRequestBuilder systemProperties(Map<String, String> systemProperties) {
254             this.systemProperties = systemProperties;
255             return this;
256         }
257 
258         public ModelBuilderRequestBuilder userProperties(Map<String, String> userProperties) {
259             this.userProperties = userProperties;
260             return this;
261         }
262 
263         public ModelBuilderRequestBuilder repositoryMerging(RepositoryMerging repositoryMerging) {
264             this.repositoryMerging = repositoryMerging;
265             return this;
266         }
267 
268         public ModelBuilderRequestBuilder repositories(List<RemoteRepository> repositories) {
269             this.repositories = repositories;
270             return this;
271         }
272 
273         public ModelBuilderRequestBuilder lifecycleBindingsInjector(ModelTransformer lifecycleBindingsInjector) {
274             this.lifecycleBindingsInjector = lifecycleBindingsInjector;
275             return this;
276         }
277 
278         public ModelBuilderRequest build() {
279             return new DefaultModelBuilderRequest(
280                     session,
281                     trace,
282                     requestType,
283                     locationTracking,
284                     recursive,
285                     source,
286                     profiles,
287                     activeProfileIds,
288                     inactiveProfileIds,
289                     systemProperties,
290                     userProperties,
291                     repositoryMerging,
292                     repositories,
293                     lifecycleBindingsInjector);
294         }
295 
296         private static class DefaultModelBuilderRequest extends BaseRequest<Session> implements ModelBuilderRequest {
297             private final RequestType requestType;
298             private final boolean locationTracking;
299             private final boolean recursive;
300             private final ModelSource source;
301             private final Collection<Profile> profiles;
302             private final List<String> activeProfileIds;
303             private final List<String> inactiveProfileIds;
304             private final Map<String, String> systemProperties;
305             private final Map<String, String> userProperties;
306             private final RepositoryMerging repositoryMerging;
307             private final List<RemoteRepository> repositories;
308             private final ModelTransformer lifecycleBindingsInjector;
309 
310             @SuppressWarnings("checkstyle:ParameterNumber")
311             DefaultModelBuilderRequest(
312                     @Nonnull Session session,
313                     @Nullable RequestTrace trace,
314                     @Nonnull RequestType requestType,
315                     boolean locationTracking,
316                     boolean recursive,
317                     @Nonnull ModelSource source,
318                     Collection<Profile> profiles,
319                     List<String> activeProfileIds,
320                     List<String> inactiveProfileIds,
321                     Map<String, String> systemProperties,
322                     Map<String, String> userProperties,
323                     RepositoryMerging repositoryMerging,
324                     List<RemoteRepository> repositories,
325                     ModelTransformer lifecycleBindingsInjector) {
326                 super(session, trace);
327                 this.requestType = requireNonNull(requestType, "requestType cannot be null");
328                 this.locationTracking = locationTracking;
329                 this.recursive = recursive;
330                 this.source = source;
331                 this.profiles = profiles != null ? List.copyOf(profiles) : List.of();
332                 this.activeProfileIds = activeProfileIds != null ? List.copyOf(activeProfileIds) : List.of();
333                 this.inactiveProfileIds = inactiveProfileIds != null ? List.copyOf(inactiveProfileIds) : List.of();
334                 this.systemProperties =
335                         systemProperties != null ? Map.copyOf(systemProperties) : session.getSystemProperties();
336                 this.userProperties = userProperties != null ? Map.copyOf(userProperties) : session.getUserProperties();
337                 this.repositoryMerging = repositoryMerging;
338                 this.repositories = repositories != null ? List.copyOf(validate(repositories)) : null;
339                 this.lifecycleBindingsInjector = lifecycleBindingsInjector;
340             }
341 
342             @Override
343             public RequestType getRequestType() {
344                 return requestType;
345             }
346 
347             @Override
348             public boolean isLocationTracking() {
349                 return locationTracking;
350             }
351 
352             @Override
353             public boolean isRecursive() {
354                 return recursive;
355             }
356 
357             @Nonnull
358             @Override
359             public ModelSource getSource() {
360                 return source;
361             }
362 
363             @Override
364             public Collection<Profile> getProfiles() {
365                 return profiles;
366             }
367 
368             @Override
369             public List<String> getActiveProfileIds() {
370                 return activeProfileIds;
371             }
372 
373             @Override
374             public List<String> getInactiveProfileIds() {
375                 return inactiveProfileIds;
376             }
377 
378             @Override
379             public Map<String, String> getSystemProperties() {
380                 return systemProperties;
381             }
382 
383             @Override
384             public Map<String, String> getUserProperties() {
385                 return userProperties;
386             }
387 
388             @Override
389             public RepositoryMerging getRepositoryMerging() {
390                 return repositoryMerging;
391             }
392 
393             @Override
394             public List<RemoteRepository> getRepositories() {
395                 return repositories;
396             }
397 
398             @Override
399             public ModelTransformer getLifecycleBindingsInjector() {
400                 return lifecycleBindingsInjector;
401             }
402 
403             @Override
404             public boolean equals(Object o) {
405                 return o instanceof DefaultModelBuilderRequest that
406                         && locationTracking == that.locationTracking
407                         && recursive == that.recursive
408                         && requestType == that.requestType
409                         && Objects.equals(source, that.source)
410                         && Objects.equals(profiles, that.profiles)
411                         && Objects.equals(activeProfileIds, that.activeProfileIds)
412                         && Objects.equals(inactiveProfileIds, that.inactiveProfileIds)
413                         && Objects.equals(systemProperties, that.systemProperties)
414                         && Objects.equals(userProperties, that.userProperties)
415                         && repositoryMerging == that.repositoryMerging
416                         && Objects.equals(repositories, that.repositories)
417                         && Objects.equals(lifecycleBindingsInjector, that.lifecycleBindingsInjector);
418             }
419 
420             @Override
421             public int hashCode() {
422                 return Objects.hash(
423                         requestType,
424                         locationTracking,
425                         recursive,
426                         source,
427                         profiles,
428                         activeProfileIds,
429                         inactiveProfileIds,
430                         systemProperties,
431                         userProperties,
432                         repositoryMerging,
433                         repositories,
434                         lifecycleBindingsInjector);
435             }
436 
437             @Override
438             public String toString() {
439                 return "ModelBuilderRequest[" + "requestType="
440                         + requestType + ", locationTracking="
441                         + locationTracking + ", recursive="
442                         + recursive + ", source="
443                         + source + ", profiles="
444                         + profiles + ", activeProfileIds="
445                         + activeProfileIds + ", inactiveProfileIds="
446                         + inactiveProfileIds + ", systemProperties="
447                         + systemProperties + ", userProperties="
448                         + userProperties + ", repositoryMerging="
449                         + repositoryMerging + ", repositories="
450                         + repositories + ", lifecycleBindingsInjector="
451                         + lifecycleBindingsInjector + ']';
452             }
453         }
454     }
455 }