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