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