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