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.Session;
27  import org.apache.maven.api.annotations.Experimental;
28  import org.apache.maven.api.annotations.Immutable;
29  import org.apache.maven.api.annotations.Nonnull;
30  import org.apache.maven.api.annotations.NotThreadSafe;
31  import org.apache.maven.api.annotations.Nullable;
32  import org.apache.maven.api.model.Profile;
33  
34  import static org.apache.maven.api.services.BaseRequest.nonNull;
35  
36  /**
37   * Request used to build a {@link org.apache.maven.api.Project} using
38   * the {@link ProjectBuilder} service.
39   *
40   * TODO: replace ModelRepositoryHolder with just the enum for the strategy
41   * TODO: replace validation level with an enum (though, we usually need just a boolean)
42   *
43   * @since 4.0.0
44   */
45  @Experimental
46  @Immutable
47  public interface ModelBuilderRequest {
48  
49      /**
50       * Denotes minimal validation of POMs. This validation level is meant for processing of POMs from repositories
51       * during metadata retrieval.
52       */
53      int VALIDATION_LEVEL_MINIMAL = 0;
54  
55      /**
56       * Denotes validation as performed by Maven 2.0. This validation level is meant as a compatibility mode to allow
57       * users to migrate their projects.
58       */
59      int VALIDATION_LEVEL_MAVEN_2_0 = 20;
60  
61      /**
62       * Denotes validation as performed by Maven 3.0. This validation level is meant for existing projects.
63       */
64      int VALIDATION_LEVEL_MAVEN_3_0 = 30;
65  
66      /**
67       * Denotes validation as performed by Maven 3.1. This validation level is meant for existing projects.
68       */
69      int VALIDATION_LEVEL_MAVEN_3_1 = 31;
70  
71      /**
72       * Denotes validation as performed by Maven 4.0. This validation level is meant for new projects.
73       */
74      int VALIDATION_LEVEL_MAVEN_4_0 = 40;
75  
76      /**
77       * Denotes strict validation as recommended by the current Maven version.
78       */
79      int VALIDATION_LEVEL_STRICT = VALIDATION_LEVEL_MAVEN_4_0;
80  
81      @Nonnull
82      Session getSession();
83  
84      @Nonnull
85      ModelSource getSource();
86  
87      int getValidationLevel();
88  
89      boolean isTwoPhaseBuilding();
90  
91      boolean isLocationTracking();
92  
93      /**
94       * Indicates if the model to be built is a local project or a dependency.
95       * In case the project is loaded externally from a remote repository (as a dependency or
96       * even as an external parent), the POM will be parsed in a lenient way.  Local POMs
97       * are parsed more strictly.
98       */
99      boolean isProjectBuild();
100 
101     /**
102      * Specifies whether plugin processing should take place for the built model.
103      * This involves merging plugins specified by the {@link org.apache.maven.api.Packaging},
104      * configuration expansion (merging configuration defined globally for a given plugin
105      * using {@link org.apache.maven.api.model.Plugin#getConfiguration()}
106      * into the configuration for each {@link org.apache.maven.api.model.PluginExecution}.
107      */
108     boolean isProcessPlugins();
109 
110     /**
111      * Defines external profiles that may be activated for the given model.
112      * Those are external profiles usually defined in {@link org.apache.maven.api.settings.Settings#getProfiles()}.
113      */
114     @Nonnull
115     Collection<Profile> getProfiles();
116 
117     /**
118      * List of profile ids that have been explicitly activated by the user.
119      */
120     @Nonnull
121     List<String> getActiveProfileIds();
122 
123     /**
124      * List of profile ids that have been explicitly deactivated by the user.
125      */
126     @Nonnull
127     List<String> getInactiveProfileIds();
128 
129     /**
130      * Provides a map of system properties.
131      */
132     @Nonnull
133     Map<String, String> getSystemProperties();
134 
135     /**
136      * Provides a map of user properties.
137      * User properties
138      */
139     @Nonnull
140     Map<String, String> getUserProperties();
141 
142     @Nonnull
143     ModelResolver getModelResolver();
144 
145     @Nonnull
146     ModelRepositoryHolder getModelRepositoryHolder();
147 
148     @Nullable
149     ModelCache getModelCache();
150 
151     @Nullable
152     Object getListener();
153 
154     @Nullable
155     ModelBuilderResult getInterimResult();
156 
157     @Nullable
158     ModelTransformerContextBuilder getTransformerContextBuilder();
159 
160     @Nonnull
161     static ModelBuilderRequest build(@Nonnull ModelBuilderRequest request, @Nonnull ModelSource source) {
162         return builder(nonNull(request, "request cannot be null"))
163                 .source(nonNull(source, "source cannot be null"))
164                 .build();
165     }
166 
167     @Nonnull
168     static ModelBuilderRequest build(@Nonnull Session session, @Nonnull ModelSource source) {
169         return builder()
170                 .session(nonNull(session, "session cannot be null"))
171                 .source(nonNull(source, "source cannot be null"))
172                 .build();
173     }
174 
175     @Nonnull
176     static ModelBuilderRequest build(@Nonnull Session session, @Nonnull Path path) {
177         return builder()
178                 .session(nonNull(session, "session cannot be null"))
179                 .source(ModelSource.fromPath(path))
180                 .build();
181     }
182 
183     @Nonnull
184     static ModelBuilderRequestBuilder builder() {
185         return new ModelBuilderRequestBuilder();
186     }
187 
188     @Nonnull
189     static ModelBuilderRequestBuilder builder(ModelBuilderRequest request) {
190         return new ModelBuilderRequestBuilder(request);
191     }
192 
193     @NotThreadSafe
194     class ModelBuilderRequestBuilder {
195         Session session;
196         int validationLevel;
197         boolean locationTracking;
198         boolean twoPhaseBuilding;
199         ModelSource source;
200         boolean projectBuild;
201         boolean processPlugins = true;
202         Collection<Profile> profiles;
203         List<String> activeProfileIds;
204         List<String> inactiveProfileIds;
205         Map<String, String> systemProperties;
206         Map<String, String> userProperties;
207         ModelResolver modelResolver;
208         ModelRepositoryHolder modelRepositoryHolder;
209         ModelCache modelCache;
210         Object listener;
211         ModelBuilderResult interimResult;
212         ModelTransformerContextBuilder transformerContextBuilder;
213 
214         ModelBuilderRequestBuilder() {}
215 
216         ModelBuilderRequestBuilder(ModelBuilderRequest request) {
217             this.session = request.getSession();
218             this.validationLevel = request.getValidationLevel();
219             this.locationTracking = request.isLocationTracking();
220             this.twoPhaseBuilding = request.isTwoPhaseBuilding();
221             this.source = request.getSource();
222             this.projectBuild = request.isProjectBuild();
223             this.processPlugins = request.isProcessPlugins();
224             this.profiles = request.getProfiles();
225             this.activeProfileIds = request.getActiveProfileIds();
226             this.inactiveProfileIds = request.getInactiveProfileIds();
227             this.systemProperties = request.getSystemProperties();
228             this.userProperties = request.getUserProperties();
229             this.modelResolver = request.getModelResolver();
230             this.modelRepositoryHolder = request.getModelRepositoryHolder();
231             this.modelCache = request.getModelCache();
232             this.listener = request.getListener();
233             this.interimResult = request.getInterimResult();
234             this.transformerContextBuilder = request.getTransformerContextBuilder();
235         }
236 
237         public ModelBuilderRequestBuilder session(Session session) {
238             this.session = session;
239             return this;
240         }
241 
242         public ModelBuilderRequestBuilder validationLevel(int validationLevel) {
243             this.validationLevel = validationLevel;
244             return this;
245         }
246 
247         public ModelBuilderRequestBuilder twoPhaseBuilding(boolean twoPhaseBuilding) {
248             this.twoPhaseBuilding = twoPhaseBuilding;
249             return this;
250         }
251 
252         public ModelBuilderRequestBuilder locationTracking(boolean locationTracking) {
253             this.locationTracking = locationTracking;
254             return this;
255         }
256 
257         public ModelBuilderRequestBuilder source(ModelSource source) {
258             this.source = source;
259             return this;
260         }
261 
262         public ModelBuilderRequestBuilder projectBuild(boolean projectBuild) {
263             this.projectBuild = projectBuild;
264             return this;
265         }
266 
267         public ModelBuilderRequestBuilder processPlugins(boolean processPlugins) {
268             this.processPlugins = processPlugins;
269             return this;
270         }
271 
272         public ModelBuilderRequestBuilder profiles(List<Profile> profiles) {
273             this.profiles = profiles;
274             return this;
275         }
276 
277         public ModelBuilderRequestBuilder activeProfileIds(List<String> activeProfileIds) {
278             this.activeProfileIds = activeProfileIds;
279             return this;
280         }
281 
282         public ModelBuilderRequestBuilder inactiveProfileIds(List<String> inactiveProfileIds) {
283             this.inactiveProfileIds = inactiveProfileIds;
284             return this;
285         }
286 
287         public ModelBuilderRequestBuilder systemProperties(Map<String, String> systemProperties) {
288             this.systemProperties = systemProperties;
289             return this;
290         }
291 
292         public ModelBuilderRequestBuilder userProperties(Map<String, String> userProperties) {
293             this.userProperties = userProperties;
294             return this;
295         }
296 
297         public ModelBuilderRequestBuilder modelResolver(ModelResolver modelResolver) {
298             this.modelResolver = modelResolver;
299             return this;
300         }
301 
302         public ModelBuilderRequestBuilder modelRepositoryHolder(ModelRepositoryHolder modelRepositoryHolder) {
303             this.modelRepositoryHolder = modelRepositoryHolder;
304             return this;
305         }
306 
307         public ModelBuilderRequestBuilder modelCache(ModelCache modelCache) {
308             this.modelCache = modelCache;
309             return this;
310         }
311 
312         public ModelBuilderRequestBuilder listener(Object listener) {
313             this.listener = listener;
314             return this;
315         }
316 
317         public ModelBuilderRequestBuilder interimResult(ModelBuilderResult interimResult) {
318             this.interimResult = interimResult;
319             return this;
320         }
321 
322         public ModelBuilderRequestBuilder transformerContextBuilder(
323                 ModelTransformerContextBuilder transformerContextBuilder) {
324             this.transformerContextBuilder = transformerContextBuilder;
325             return this;
326         }
327 
328         public ModelBuilderRequest build() {
329             return new DefaultModelBuilderRequest(
330                     session,
331                     validationLevel,
332                     locationTracking,
333                     twoPhaseBuilding,
334                     source,
335                     projectBuild,
336                     processPlugins,
337                     profiles,
338                     activeProfileIds,
339                     inactiveProfileIds,
340                     systemProperties,
341                     userProperties,
342                     modelResolver,
343                     modelRepositoryHolder,
344                     modelCache,
345                     listener,
346                     interimResult,
347                     transformerContextBuilder);
348         }
349 
350         private static class DefaultModelBuilderRequest extends BaseRequest implements ModelBuilderRequest {
351             private final int validationLevel;
352             private final boolean locationTracking;
353             private final boolean twoPhaseBuilding;
354             private final ModelSource source;
355             private final boolean projectBuild;
356             private final boolean processPlugins;
357             private final Collection<Profile> profiles;
358             private final List<String> activeProfileIds;
359             private final List<String> inactiveProfileIds;
360             private final Map<String, String> systemProperties;
361             private final Map<String, String> userProperties;
362             private final ModelResolver modelResolver;
363             private final ModelRepositoryHolder modelRepositoryHolder;
364             private final ModelCache modelCache;
365             private final Object listener;
366             private final ModelBuilderResult interimResult;
367             private final ModelTransformerContextBuilder transformerContextBuilder;
368 
369             @SuppressWarnings("checkstyle:ParameterNumber")
370             DefaultModelBuilderRequest(
371                     @Nonnull Session session,
372                     int validationLevel,
373                     boolean locationTracking,
374                     boolean twoPhaseBuilding,
375                     @Nonnull ModelSource source,
376                     boolean projectBuild,
377                     boolean processPlugins,
378                     Collection<Profile> profiles,
379                     List<String> activeProfileIds,
380                     List<String> inactiveProfileIds,
381                     Map<String, String> systemProperties,
382                     Map<String, String> userProperties,
383                     ModelResolver modelResolver,
384                     ModelRepositoryHolder modelRepositoryHolder,
385                     ModelCache modelCache,
386                     Object listener,
387                     ModelBuilderResult interimResult,
388                     ModelTransformerContextBuilder transformerContextBuilder) {
389                 super(session);
390                 this.validationLevel = validationLevel;
391                 this.locationTracking = locationTracking;
392                 this.twoPhaseBuilding = twoPhaseBuilding;
393                 this.source = source;
394                 this.projectBuild = projectBuild;
395                 this.processPlugins = processPlugins;
396                 this.profiles = profiles != null ? List.copyOf(profiles) : List.of();
397                 this.activeProfileIds = activeProfileIds != null ? List.copyOf(activeProfileIds) : List.of();
398                 this.inactiveProfileIds = inactiveProfileIds != null ? List.copyOf(inactiveProfileIds) : List.of();
399                 this.systemProperties =
400                         systemProperties != null ? Map.copyOf(systemProperties) : session.getSystemProperties();
401                 this.userProperties = userProperties != null ? Map.copyOf(userProperties) : session.getUserProperties();
402                 this.modelResolver = modelResolver;
403                 this.modelRepositoryHolder = modelRepositoryHolder;
404                 this.modelCache = modelCache;
405                 this.listener = listener;
406                 this.interimResult = interimResult;
407                 this.transformerContextBuilder = transformerContextBuilder;
408             }
409 
410             @Override
411             public int getValidationLevel() {
412                 return validationLevel;
413             }
414 
415             @Override
416             public boolean isTwoPhaseBuilding() {
417                 return twoPhaseBuilding;
418             }
419 
420             @Override
421             public boolean isLocationTracking() {
422                 return locationTracking;
423             }
424 
425             @Nonnull
426             @Override
427             public ModelSource getSource() {
428                 return source;
429             }
430 
431             public boolean isProjectBuild() {
432                 return projectBuild;
433             }
434 
435             @Override
436             public boolean isProcessPlugins() {
437                 return processPlugins;
438             }
439 
440             @Override
441             public Collection<Profile> getProfiles() {
442                 return profiles;
443             }
444 
445             @Override
446             public List<String> getActiveProfileIds() {
447                 return activeProfileIds;
448             }
449 
450             @Override
451             public List<String> getInactiveProfileIds() {
452                 return inactiveProfileIds;
453             }
454 
455             @Override
456             public Map<String, String> getSystemProperties() {
457                 return systemProperties;
458             }
459 
460             @Override
461             public Map<String, String> getUserProperties() {
462                 return userProperties;
463             }
464 
465             @Override
466             public ModelResolver getModelResolver() {
467                 return modelResolver;
468             }
469 
470             @Override
471             public ModelRepositoryHolder getModelRepositoryHolder() {
472                 return modelRepositoryHolder;
473             }
474 
475             @Override
476             public ModelCache getModelCache() {
477                 return modelCache;
478             }
479 
480             public Object getListener() {
481                 return listener;
482             }
483 
484             @Override
485             public ModelBuilderResult getInterimResult() {
486                 return interimResult;
487             }
488 
489             public ModelTransformerContextBuilder getTransformerContextBuilder() {
490                 return transformerContextBuilder;
491             }
492         }
493     }
494 }