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.model.building;
20  
21  import java.util.Arrays;
22  
23  import org.apache.maven.api.Version;
24  import org.apache.maven.api.VersionConstraint;
25  import org.apache.maven.api.VersionRange;
26  import org.apache.maven.api.spi.ModelParser;
27  import org.apache.maven.model.Model;
28  import org.apache.maven.model.composition.DefaultDependencyManagementImporter;
29  import org.apache.maven.model.composition.DependencyManagementImporter;
30  import org.apache.maven.model.inheritance.DefaultInheritanceAssembler;
31  import org.apache.maven.model.inheritance.InheritanceAssembler;
32  import org.apache.maven.model.interpolation.DefaultModelVersionProcessor;
33  import org.apache.maven.model.interpolation.ModelInterpolator;
34  import org.apache.maven.model.interpolation.ModelVersionProcessor;
35  import org.apache.maven.model.interpolation.StringVisitorModelInterpolator;
36  import org.apache.maven.model.io.DefaultModelReader;
37  import org.apache.maven.model.io.ModelReader;
38  import org.apache.maven.model.locator.DefaultModelLocator;
39  import org.apache.maven.model.locator.ModelLocator;
40  import org.apache.maven.model.management.DefaultDependencyManagementInjector;
41  import org.apache.maven.model.management.DefaultPluginManagementInjector;
42  import org.apache.maven.model.management.DependencyManagementInjector;
43  import org.apache.maven.model.management.PluginManagementInjector;
44  import org.apache.maven.model.normalization.DefaultModelNormalizer;
45  import org.apache.maven.model.normalization.ModelNormalizer;
46  import org.apache.maven.model.path.DefaultModelPathTranslator;
47  import org.apache.maven.model.path.DefaultModelUrlNormalizer;
48  import org.apache.maven.model.path.DefaultPathTranslator;
49  import org.apache.maven.model.path.DefaultUrlNormalizer;
50  import org.apache.maven.model.path.ModelPathTranslator;
51  import org.apache.maven.model.path.ModelUrlNormalizer;
52  import org.apache.maven.model.path.PathTranslator;
53  import org.apache.maven.model.path.ProfileActivationFilePathInterpolator;
54  import org.apache.maven.model.path.UrlNormalizer;
55  import org.apache.maven.model.plugin.DefaultPluginConfigurationExpander;
56  import org.apache.maven.model.plugin.DefaultReportConfigurationExpander;
57  import org.apache.maven.model.plugin.DefaultReportingConverter;
58  import org.apache.maven.model.plugin.LifecycleBindingsInjector;
59  import org.apache.maven.model.plugin.PluginConfigurationExpander;
60  import org.apache.maven.model.plugin.ReportConfigurationExpander;
61  import org.apache.maven.model.plugin.ReportingConverter;
62  import org.apache.maven.model.profile.DefaultProfileInjector;
63  import org.apache.maven.model.profile.DefaultProfileSelector;
64  import org.apache.maven.model.profile.ProfileInjector;
65  import org.apache.maven.model.profile.ProfileSelector;
66  import org.apache.maven.model.profile.activation.FileProfileActivator;
67  import org.apache.maven.model.profile.activation.JdkVersionProfileActivator;
68  import org.apache.maven.model.profile.activation.OperatingSystemProfileActivator;
69  import org.apache.maven.model.profile.activation.ProfileActivator;
70  import org.apache.maven.model.profile.activation.PropertyProfileActivator;
71  import org.apache.maven.model.root.DefaultRootLocator;
72  import org.apache.maven.model.root.RootLocator;
73  import org.apache.maven.model.superpom.DefaultSuperPomProvider;
74  import org.apache.maven.model.superpom.SuperPomProvider;
75  import org.apache.maven.model.validation.DefaultModelValidator;
76  import org.apache.maven.model.validation.ModelValidator;
77  import org.apache.maven.model.version.ModelVersionParser;
78  
79  import static java.util.Objects.requireNonNull;
80  
81  /**
82   * A factory to create model builder instances when no dependency injection is available. <em>Note:</em> This class is
83   * only meant as a utility for developers that want to employ the model builder outside the Maven build system, Maven
84   * plugins should always acquire model builder instances via dependency injection. Developers might want to subclass
85   * this factory to provide custom implementations for some of the components used by the model builder, or use the
86   * builder API to inject custom instances.
87   *
88   */
89  public class DefaultModelBuilderFactory {
90  
91      private ModelProcessor modelProcessor;
92      private ModelValidator modelValidator;
93      private ModelNormalizer modelNormalizer;
94      private ModelInterpolator modelInterpolator;
95      private ModelPathTranslator modelPathTranslator;
96      private ModelUrlNormalizer modelUrlNormalizer;
97      private SuperPomProvider superPomProvider;
98      private InheritanceAssembler inheritanceAssembler;
99      private ProfileSelector profileSelector;
100     private ProfileInjector profileInjector;
101     private PluginManagementInjector pluginManagementInjector;
102     private DependencyManagementInjector dependencyManagementInjector;
103     private DependencyManagementImporter dependencyManagementImporter;
104     private LifecycleBindingsInjector lifecycleBindingsInjector;
105     private PluginConfigurationExpander pluginConfigurationExpander;
106     private ReportConfigurationExpander reportConfigurationExpander;
107     private ProfileActivationFilePathInterpolator profileActivationFilePathInterpolator;
108     private ModelVersionProcessor versionProcessor;
109     private ModelSourceTransformer transformer;
110     private ModelVersionParser versionParser;
111 
112     public DefaultModelBuilderFactory setModelProcessor(ModelProcessor modelProcessor) {
113         this.modelProcessor = modelProcessor;
114         return this;
115     }
116 
117     public DefaultModelBuilderFactory setModelValidator(ModelValidator modelValidator) {
118         this.modelValidator = modelValidator;
119         return this;
120     }
121 
122     public DefaultModelBuilderFactory setModelNormalizer(ModelNormalizer modelNormalizer) {
123         this.modelNormalizer = modelNormalizer;
124         return this;
125     }
126 
127     public DefaultModelBuilderFactory setModelInterpolator(ModelInterpolator modelInterpolator) {
128         this.modelInterpolator = modelInterpolator;
129         return this;
130     }
131 
132     public DefaultModelBuilderFactory setModelPathTranslator(ModelPathTranslator modelPathTranslator) {
133         this.modelPathTranslator = modelPathTranslator;
134         return this;
135     }
136 
137     public DefaultModelBuilderFactory setModelUrlNormalizer(ModelUrlNormalizer modelUrlNormalizer) {
138         this.modelUrlNormalizer = modelUrlNormalizer;
139         return this;
140     }
141 
142     public DefaultModelBuilderFactory setSuperPomProvider(SuperPomProvider superPomProvider) {
143         this.superPomProvider = superPomProvider;
144         return this;
145     }
146 
147     public DefaultModelBuilderFactory setInheritanceAssembler(InheritanceAssembler inheritanceAssembler) {
148         this.inheritanceAssembler = inheritanceAssembler;
149         return this;
150     }
151 
152     public DefaultModelBuilderFactory setProfileSelector(ProfileSelector profileSelector) {
153         this.profileSelector = profileSelector;
154         return this;
155     }
156 
157     public DefaultModelBuilderFactory setProfileInjector(ProfileInjector profileInjector) {
158         this.profileInjector = profileInjector;
159         return this;
160     }
161 
162     public DefaultModelBuilderFactory setPluginManagementInjector(PluginManagementInjector pluginManagementInjector) {
163         this.pluginManagementInjector = pluginManagementInjector;
164         return this;
165     }
166 
167     public DefaultModelBuilderFactory setDependencyManagementInjector(
168             DependencyManagementInjector dependencyManagementInjector) {
169         this.dependencyManagementInjector = dependencyManagementInjector;
170         return this;
171     }
172 
173     public DefaultModelBuilderFactory setDependencyManagementImporter(
174             DependencyManagementImporter dependencyManagementImporter) {
175         this.dependencyManagementImporter = dependencyManagementImporter;
176         return this;
177     }
178 
179     public DefaultModelBuilderFactory setLifecycleBindingsInjector(
180             LifecycleBindingsInjector lifecycleBindingsInjector) {
181         this.lifecycleBindingsInjector = lifecycleBindingsInjector;
182         return this;
183     }
184 
185     public DefaultModelBuilderFactory setPluginConfigurationExpander(
186             PluginConfigurationExpander pluginConfigurationExpander) {
187         this.pluginConfigurationExpander = pluginConfigurationExpander;
188         return this;
189     }
190 
191     public DefaultModelBuilderFactory setReportConfigurationExpander(
192             ReportConfigurationExpander reportConfigurationExpander) {
193         this.reportConfigurationExpander = reportConfigurationExpander;
194         return this;
195     }
196 
197     @Deprecated
198     public DefaultModelBuilderFactory setReportingConverter(ReportingConverter reportingConverter) {
199         return this;
200     }
201 
202     public DefaultModelBuilderFactory setProfileActivationFilePathInterpolator(
203             ProfileActivationFilePathInterpolator profileActivationFilePathInterpolator) {
204         this.profileActivationFilePathInterpolator = profileActivationFilePathInterpolator;
205         return this;
206     }
207 
208     public DefaultModelBuilderFactory setVersionProcessor(ModelVersionProcessor versionProcessor) {
209         this.versionProcessor = versionProcessor;
210         return this;
211     }
212 
213     public DefaultModelBuilderFactory setTransformer(ModelSourceTransformer transformer) {
214         this.transformer = transformer;
215         return this;
216     }
217 
218     public DefaultModelBuilderFactory setModelVersionParser(ModelVersionParser versionParser) {
219         this.versionParser = versionParser;
220         return this;
221     }
222 
223     protected ModelProcessor newModelProcessor() {
224         return new DefaultModelProcessor(Arrays.asList(newModelParsers()), newModelLocator(), newModelReader());
225     }
226 
227     protected ModelParser[] newModelParsers() {
228         return new ModelParser[0];
229     }
230 
231     protected ModelLocator newModelLocator() {
232         return new DefaultModelLocator();
233     }
234 
235     protected ModelReader newModelReader() {
236         return new DefaultModelReader(newModelSourceTransformer());
237     }
238 
239     protected ProfileSelector newProfileSelector() {
240         return new DefaultProfileSelector(Arrays.asList(newProfileActivators()));
241     }
242 
243     protected ProfileActivator[] newProfileActivators() {
244         return new ProfileActivator[] {
245             new JdkVersionProfileActivator(),
246             new OperatingSystemProfileActivator(),
247             new PropertyProfileActivator(),
248             new FileProfileActivator(newProfileActivationFilePathInterpolator())
249         };
250     }
251 
252     protected ProfileActivationFilePathInterpolator newProfileActivationFilePathInterpolator() {
253         return new ProfileActivationFilePathInterpolator(newPathTranslator(), newRootLocator());
254     }
255 
256     protected UrlNormalizer newUrlNormalizer() {
257         return new DefaultUrlNormalizer();
258     }
259 
260     protected PathTranslator newPathTranslator() {
261         return new DefaultPathTranslator();
262     }
263 
264     protected RootLocator newRootLocator() {
265         return new DefaultRootLocator();
266     }
267 
268     protected ModelInterpolator newModelInterpolator() {
269         UrlNormalizer normalizer = newUrlNormalizer();
270         PathTranslator pathTranslator = newPathTranslator();
271         RootLocator rootLocator = newRootLocator();
272         return new StringVisitorModelInterpolator(pathTranslator, normalizer, rootLocator);
273     }
274 
275     protected ModelVersionProcessor newModelVersionPropertiesProcessor() {
276         return new DefaultModelVersionProcessor();
277     }
278 
279     protected ModelValidator newModelValidator() {
280         ModelVersionProcessor processor = newModelVersionPropertiesProcessor();
281         return new DefaultModelValidator(processor);
282     }
283 
284     protected ModelNormalizer newModelNormalizer() {
285         return new DefaultModelNormalizer();
286     }
287 
288     protected ModelPathTranslator newModelPathTranslator() {
289         return new DefaultModelPathTranslator(newPathTranslator());
290     }
291 
292     protected ModelUrlNormalizer newModelUrlNormalizer() {
293         return new DefaultModelUrlNormalizer(newUrlNormalizer());
294     }
295 
296     protected InheritanceAssembler newInheritanceAssembler() {
297         return new DefaultInheritanceAssembler();
298     }
299 
300     protected ProfileInjector newProfileInjector() {
301         return new DefaultProfileInjector();
302     }
303 
304     protected SuperPomProvider newSuperPomProvider() {
305         return new DefaultSuperPomProvider(newModelProcessor());
306     }
307 
308     protected DependencyManagementImporter newDependencyManagementImporter() {
309         return new DefaultDependencyManagementImporter();
310     }
311 
312     protected DependencyManagementInjector newDependencyManagementInjector() {
313         return new DefaultDependencyManagementInjector();
314     }
315 
316     protected LifecycleBindingsInjector newLifecycleBindingsInjector() {
317         return new StubLifecycleBindingsInjector();
318     }
319 
320     protected PluginManagementInjector newPluginManagementInjector() {
321         return new DefaultPluginManagementInjector();
322     }
323 
324     protected PluginConfigurationExpander newPluginConfigurationExpander() {
325         return new DefaultPluginConfigurationExpander();
326     }
327 
328     protected ReportConfigurationExpander newReportConfigurationExpander() {
329         return new DefaultReportConfigurationExpander();
330     }
331 
332     @Deprecated
333     protected ReportingConverter newReportingConverter() {
334         return new DefaultReportingConverter();
335     }
336 
337     private ModelSourceTransformer newModelSourceTransformer() {
338         return new BuildModelSourceTransformer();
339     }
340 
341     private ModelVersionParser newModelVersionParser() {
342         // This is a limited parser that does not support ranges and compares versions as strings
343         // in real-life this parser should not be used, but replaced with a proper one
344         return new ModelVersionParser() {
345             @Override
346             public Version parseVersion(String version) {
347                 requireNonNull(version, "version");
348                 return new Version() {
349                     @Override
350                     public String asString() {
351                         return version;
352                     }
353 
354                     @Override
355                     public int compareTo(Version o) {
356                         return version.compareTo(o.asString());
357                     }
358                 };
359             }
360 
361             @Override
362             public VersionRange parseVersionRange(String range) {
363                 throw new IllegalArgumentException("ranges not supported by this parser");
364             }
365 
366             @Override
367             public VersionConstraint parseVersionConstraint(String constraint) {
368                 throw new IllegalArgumentException("constraint not supported by this parser");
369             }
370 
371             @Override
372             public boolean isSnapshot(String version) {
373                 requireNonNull(version, "version");
374                 return version.endsWith("SNAPSHOT");
375             }
376         };
377     }
378 
379     /**
380      * Creates a new model builder instance.
381      *
382      * @return The new model builder instance, never {@code null}.
383      */
384     public DefaultModelBuilder newInstance() {
385         return new DefaultModelBuilder(
386                 modelProcessor != null ? modelProcessor : newModelProcessor(),
387                 modelValidator != null ? modelValidator : newModelValidator(),
388                 modelNormalizer != null ? modelNormalizer : newModelNormalizer(),
389                 modelInterpolator != null ? modelInterpolator : newModelInterpolator(),
390                 modelPathTranslator != null ? modelPathTranslator : newModelPathTranslator(),
391                 modelUrlNormalizer != null ? modelUrlNormalizer : newModelUrlNormalizer(),
392                 superPomProvider != null ? superPomProvider : newSuperPomProvider(),
393                 inheritanceAssembler != null ? inheritanceAssembler : newInheritanceAssembler(),
394                 profileSelector != null ? profileSelector : newProfileSelector(),
395                 profileInjector != null ? profileInjector : newProfileInjector(),
396                 pluginManagementInjector != null ? pluginManagementInjector : newPluginManagementInjector(),
397                 dependencyManagementInjector != null ? dependencyManagementInjector : newDependencyManagementInjector(),
398                 dependencyManagementImporter != null ? dependencyManagementImporter : newDependencyManagementImporter(),
399                 lifecycleBindingsInjector != null ? lifecycleBindingsInjector : newLifecycleBindingsInjector(),
400                 pluginConfigurationExpander != null ? pluginConfigurationExpander : newPluginConfigurationExpander(),
401                 reportConfigurationExpander != null ? reportConfigurationExpander : newReportConfigurationExpander(),
402                 profileActivationFilePathInterpolator != null
403                         ? profileActivationFilePathInterpolator
404                         : newProfileActivationFilePathInterpolator(),
405                 versionProcessor != null ? versionProcessor : newModelVersionPropertiesProcessor(),
406                 transformer != null ? transformer : newModelSourceTransformer(),
407                 versionParser != null ? versionParser : newModelVersionParser());
408     }
409 
410     private static class StubLifecycleBindingsInjector implements LifecycleBindingsInjector {
411 
412         @Override
413         public void injectLifecycleBindings(
414                 Model model, ModelBuildingRequest request, ModelProblemCollector problems) {}
415     }
416 }