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.model.Model;
24  import org.apache.maven.model.composition.DefaultDependencyManagementImporter;
25  import org.apache.maven.model.composition.DependencyManagementImporter;
26  import org.apache.maven.model.inheritance.DefaultInheritanceAssembler;
27  import org.apache.maven.model.inheritance.InheritanceAssembler;
28  import org.apache.maven.model.interpolation.DefaultModelVersionProcessor;
29  import org.apache.maven.model.interpolation.ModelInterpolator;
30  import org.apache.maven.model.interpolation.ModelVersionProcessor;
31  import org.apache.maven.model.interpolation.StringVisitorModelInterpolator;
32  import org.apache.maven.model.io.DefaultModelReader;
33  import org.apache.maven.model.io.ModelReader;
34  import org.apache.maven.model.locator.DefaultModelLocator;
35  import org.apache.maven.model.locator.ModelLocator;
36  import org.apache.maven.model.management.DefaultDependencyManagementInjector;
37  import org.apache.maven.model.management.DefaultPluginManagementInjector;
38  import org.apache.maven.model.management.DependencyManagementInjector;
39  import org.apache.maven.model.management.PluginManagementInjector;
40  import org.apache.maven.model.normalization.DefaultModelNormalizer;
41  import org.apache.maven.model.normalization.ModelNormalizer;
42  import org.apache.maven.model.path.DefaultModelPathTranslator;
43  import org.apache.maven.model.path.DefaultModelUrlNormalizer;
44  import org.apache.maven.model.path.DefaultPathTranslator;
45  import org.apache.maven.model.path.DefaultUrlNormalizer;
46  import org.apache.maven.model.path.ModelPathTranslator;
47  import org.apache.maven.model.path.ModelUrlNormalizer;
48  import org.apache.maven.model.path.PathTranslator;
49  import org.apache.maven.model.path.ProfileActivationFilePathInterpolator;
50  import org.apache.maven.model.path.UrlNormalizer;
51  import org.apache.maven.model.plugin.DefaultPluginConfigurationExpander;
52  import org.apache.maven.model.plugin.DefaultReportConfigurationExpander;
53  import org.apache.maven.model.plugin.DefaultReportingConverter;
54  import org.apache.maven.model.plugin.LifecycleBindingsInjector;
55  import org.apache.maven.model.plugin.PluginConfigurationExpander;
56  import org.apache.maven.model.plugin.ReportConfigurationExpander;
57  import org.apache.maven.model.plugin.ReportingConverter;
58  import org.apache.maven.model.profile.DefaultProfileInjector;
59  import org.apache.maven.model.profile.DefaultProfileSelector;
60  import org.apache.maven.model.profile.ProfileInjector;
61  import org.apache.maven.model.profile.ProfileSelector;
62  import org.apache.maven.model.profile.activation.FileProfileActivator;
63  import org.apache.maven.model.profile.activation.JdkVersionProfileActivator;
64  import org.apache.maven.model.profile.activation.OperatingSystemProfileActivator;
65  import org.apache.maven.model.profile.activation.ProfileActivator;
66  import org.apache.maven.model.profile.activation.PropertyProfileActivator;
67  import org.apache.maven.model.root.DefaultRootLocator;
68  import org.apache.maven.model.root.RootLocator;
69  import org.apache.maven.model.superpom.DefaultSuperPomProvider;
70  import org.apache.maven.model.superpom.SuperPomProvider;
71  import org.apache.maven.model.validation.DefaultModelValidator;
72  import org.apache.maven.model.validation.ModelValidator;
73  
74  /**
75   * A factory to create model builder instances when no dependency injection is available. <em>Note:</em> This class is
76   * only meant as a utility for developers that want to employ the model builder outside of the Maven build system, Maven
77   * plugins should always acquire model builder instances via dependency injection. Developers might want to subclass
78   * this factory to provide custom implementations for some of the components used by the model builder, or use the
79   * builder API to inject custom instances.
80   *
81   * @author Benjamin Bentmann
82   * @author Guillaume Nodet
83   */
84  public class DefaultModelBuilderFactory {
85  
86      private ModelProcessor modelProcessor;
87      private ModelValidator modelValidator;
88      private ModelNormalizer modelNormalizer;
89      private ModelInterpolator modelInterpolator;
90      private ModelPathTranslator modelPathTranslator;
91      private ModelUrlNormalizer modelUrlNormalizer;
92      private SuperPomProvider superPomProvider;
93      private InheritanceAssembler inheritanceAssembler;
94      private ProfileSelector profileSelector;
95      private ProfileInjector profileInjector;
96      private PluginManagementInjector pluginManagementInjector;
97      private DependencyManagementInjector dependencyManagementInjector;
98      private DependencyManagementImporter dependencyManagementImporter;
99      private LifecycleBindingsInjector lifecycleBindingsInjector;
100     private PluginConfigurationExpander pluginConfigurationExpander;
101     private ReportConfigurationExpander reportConfigurationExpander;
102     private ProfileActivationFilePathInterpolator profileActivationFilePathInterpolator;
103     private ModelVersionProcessor versionProcessor;
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     protected ModelProcessor newModelProcessor() {
214         return new DefaultModelProcessor(newModelLocator(), newModelReader());
215     }
216 
217     protected ModelLocator newModelLocator() {
218         return new DefaultModelLocator();
219     }
220 
221     protected ModelReader newModelReader() {
222         return new DefaultModelReader(newModelSourceTransformer());
223     }
224 
225     protected ProfileSelector newProfileSelector() {
226         return new DefaultProfileSelector(Arrays.asList(newProfileActivators()));
227     }
228 
229     protected ProfileActivator[] newProfileActivators() {
230         return new ProfileActivator[] {
231             new JdkVersionProfileActivator(),
232             new OperatingSystemProfileActivator(),
233             new PropertyProfileActivator(),
234             new FileProfileActivator(newProfileActivationFilePathInterpolator())
235         };
236     }
237 
238     protected ProfileActivationFilePathInterpolator newProfileActivationFilePathInterpolator() {
239         return new ProfileActivationFilePathInterpolator(newPathTranslator(), newRootLocator());
240     }
241 
242     protected UrlNormalizer newUrlNormalizer() {
243         return new DefaultUrlNormalizer();
244     }
245 
246     protected PathTranslator newPathTranslator() {
247         return new DefaultPathTranslator();
248     }
249 
250     protected RootLocator newRootLocator() {
251         return new DefaultRootLocator();
252     }
253 
254     protected ModelInterpolator newModelInterpolator() {
255         UrlNormalizer normalizer = newUrlNormalizer();
256         PathTranslator pathTranslator = newPathTranslator();
257         RootLocator rootLocator = newRootLocator();
258         return new StringVisitorModelInterpolator(pathTranslator, normalizer, rootLocator);
259     }
260 
261     protected ModelVersionProcessor newModelVersionPropertiesProcessor() {
262         return new DefaultModelVersionProcessor();
263     }
264 
265     protected ModelValidator newModelValidator() {
266         ModelVersionProcessor processor = newModelVersionPropertiesProcessor();
267         return new DefaultModelValidator(processor);
268     }
269 
270     protected ModelNormalizer newModelNormalizer() {
271         return new DefaultModelNormalizer();
272     }
273 
274     protected ModelPathTranslator newModelPathTranslator() {
275         return new DefaultModelPathTranslator(newPathTranslator());
276     }
277 
278     protected ModelUrlNormalizer newModelUrlNormalizer() {
279         return new DefaultModelUrlNormalizer(newUrlNormalizer());
280     }
281 
282     protected InheritanceAssembler newInheritanceAssembler() {
283         return new DefaultInheritanceAssembler();
284     }
285 
286     protected ProfileInjector newProfileInjector() {
287         return new DefaultProfileInjector();
288     }
289 
290     protected SuperPomProvider newSuperPomProvider() {
291         return new DefaultSuperPomProvider(newModelProcessor());
292     }
293 
294     protected DependencyManagementImporter newDependencyManagementImporter() {
295         return new DefaultDependencyManagementImporter();
296     }
297 
298     protected DependencyManagementInjector newDependencyManagementInjector() {
299         return new DefaultDependencyManagementInjector();
300     }
301 
302     protected LifecycleBindingsInjector newLifecycleBindingsInjector() {
303         return new StubLifecycleBindingsInjector();
304     }
305 
306     protected PluginManagementInjector newPluginManagementInjector() {
307         return new DefaultPluginManagementInjector();
308     }
309 
310     protected PluginConfigurationExpander newPluginConfigurationExpander() {
311         return new DefaultPluginConfigurationExpander();
312     }
313 
314     protected ReportConfigurationExpander newReportConfigurationExpander() {
315         return new DefaultReportConfigurationExpander();
316     }
317 
318     @Deprecated
319     protected ReportingConverter newReportingConverter() {
320         return new DefaultReportingConverter();
321     }
322 
323     private ModelSourceTransformer newModelSourceTransformer() {
324         return new DefaultModelSourceTransformer();
325     }
326 
327     /**
328      * Creates a new model builder instance.
329      *
330      * @return The new model builder instance, never {@code null}.
331      */
332     public DefaultModelBuilder newInstance() {
333         return new DefaultModelBuilder(
334                 modelProcessor != null ? modelProcessor : newModelProcessor(),
335                 modelValidator != null ? modelValidator : newModelValidator(),
336                 modelNormalizer != null ? modelNormalizer : newModelNormalizer(),
337                 modelInterpolator != null ? modelInterpolator : newModelInterpolator(),
338                 modelPathTranslator != null ? modelPathTranslator : newModelPathTranslator(),
339                 modelUrlNormalizer != null ? modelUrlNormalizer : newModelUrlNormalizer(),
340                 superPomProvider != null ? superPomProvider : newSuperPomProvider(),
341                 inheritanceAssembler != null ? inheritanceAssembler : newInheritanceAssembler(),
342                 profileSelector != null ? profileSelector : newProfileSelector(),
343                 profileInjector != null ? profileInjector : newProfileInjector(),
344                 pluginManagementInjector != null ? pluginManagementInjector : newPluginManagementInjector(),
345                 dependencyManagementInjector != null ? dependencyManagementInjector : newDependencyManagementInjector(),
346                 dependencyManagementImporter != null ? dependencyManagementImporter : newDependencyManagementImporter(),
347                 lifecycleBindingsInjector != null ? lifecycleBindingsInjector : newLifecycleBindingsInjector(),
348                 pluginConfigurationExpander != null ? pluginConfigurationExpander : newPluginConfigurationExpander(),
349                 reportConfigurationExpander != null ? reportConfigurationExpander : newReportConfigurationExpander(),
350                 profileActivationFilePathInterpolator != null
351                         ? profileActivationFilePathInterpolator
352                         : newProfileActivationFilePathInterpolator(),
353                 versionProcessor != null ? versionProcessor : newModelVersionPropertiesProcessor());
354     }
355 
356     private static class StubLifecycleBindingsInjector implements LifecycleBindingsInjector {
357 
358         @Override
359         public void injectLifecycleBindings(
360                 Model model, ModelBuildingRequest request, ModelProblemCollector problems) {}
361     }
362 }