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