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 org.apache.maven.model.Model;
22  import org.apache.maven.model.composition.DefaultDependencyManagementImporter;
23  import org.apache.maven.model.composition.DependencyManagementImporter;
24  import org.apache.maven.model.inheritance.DefaultInheritanceAssembler;
25  import org.apache.maven.model.inheritance.InheritanceAssembler;
26  import org.apache.maven.model.interpolation.DefaultModelVersionProcessor;
27  import org.apache.maven.model.interpolation.ModelInterpolator;
28  import org.apache.maven.model.interpolation.ModelVersionProcessor;
29  import org.apache.maven.model.interpolation.StringVisitorModelInterpolator;
30  import org.apache.maven.model.io.DefaultModelReader;
31  import org.apache.maven.model.io.ModelReader;
32  import org.apache.maven.model.locator.DefaultModelLocator;
33  import org.apache.maven.model.locator.ModelLocator;
34  import org.apache.maven.model.management.DefaultDependencyManagementInjector;
35  import org.apache.maven.model.management.DefaultPluginManagementInjector;
36  import org.apache.maven.model.management.DependencyManagementInjector;
37  import org.apache.maven.model.management.PluginManagementInjector;
38  import org.apache.maven.model.normalization.DefaultModelNormalizer;
39  import org.apache.maven.model.normalization.ModelNormalizer;
40  import org.apache.maven.model.path.DefaultModelPathTranslator;
41  import org.apache.maven.model.path.DefaultModelUrlNormalizer;
42  import org.apache.maven.model.path.DefaultPathTranslator;
43  import org.apache.maven.model.path.DefaultUrlNormalizer;
44  import org.apache.maven.model.path.ModelPathTranslator;
45  import org.apache.maven.model.path.ModelUrlNormalizer;
46  import org.apache.maven.model.path.PathTranslator;
47  import org.apache.maven.model.path.ProfileActivationFilePathInterpolator;
48  import org.apache.maven.model.path.UrlNormalizer;
49  import org.apache.maven.model.plugin.DefaultPluginConfigurationExpander;
50  import org.apache.maven.model.plugin.DefaultReportConfigurationExpander;
51  import org.apache.maven.model.plugin.DefaultReportingConverter;
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.plugin.ReportingConverter;
56  import org.apache.maven.model.profile.DefaultProfileInjector;
57  import org.apache.maven.model.profile.DefaultProfileSelector;
58  import org.apache.maven.model.profile.ProfileInjector;
59  import org.apache.maven.model.profile.ProfileSelector;
60  import org.apache.maven.model.profile.activation.FileProfileActivator;
61  import org.apache.maven.model.profile.activation.JdkVersionProfileActivator;
62  import org.apache.maven.model.profile.activation.OperatingSystemProfileActivator;
63  import org.apache.maven.model.profile.activation.ProfileActivator;
64  import org.apache.maven.model.profile.activation.PropertyProfileActivator;
65  import org.apache.maven.model.superpom.DefaultSuperPomProvider;
66  import org.apache.maven.model.superpom.SuperPomProvider;
67  import org.apache.maven.model.validation.DefaultModelValidator;
68  import org.apache.maven.model.validation.ModelValidator;
69  
70  /**
71   * A factory to create model builder instances when no dependency injection is available. <em>Note:</em> This class is
72   * only meant as a utility for developers that want to employ the model builder outside of the Maven build system, Maven
73   * plugins should always acquire model builder instances via dependency injection. Developers might want to subclass
74   * this factory to provide custom implementations for some of the components used by the model builder.
75   *
76   * @author Benjamin Bentmann
77   */
78  public class DefaultModelBuilderFactory {
79  
80      protected ModelProcessor newModelProcessor() {
81          DefaultModelProcessor processor = new DefaultModelProcessor();
82          processor.setModelLocator(newModelLocator());
83          processor.setModelReader(newModelReader());
84          return processor;
85      }
86  
87      protected ModelLocator newModelLocator() {
88          return new DefaultModelLocator();
89      }
90  
91      protected ModelReader newModelReader() {
92          return new DefaultModelReader();
93      }
94  
95      protected ProfileSelector newProfileSelector() {
96          DefaultProfileSelector profileSelector = new DefaultProfileSelector();
97  
98          for (ProfileActivator activator : newProfileActivators()) {
99              profileSelector.addProfileActivator(activator);
100         }
101 
102         return profileSelector;
103     }
104 
105     protected ProfileActivator[] newProfileActivators() {
106         return new ProfileActivator[] {
107             new JdkVersionProfileActivator(),
108             new OperatingSystemProfileActivator(),
109             new PropertyProfileActivator(),
110             new FileProfileActivator()
111                     .setProfileActivationFilePathInterpolator(newProfileActivationFilePathInterpolator())
112         };
113     }
114 
115     protected ProfileActivationFilePathInterpolator newProfileActivationFilePathInterpolator() {
116         return new ProfileActivationFilePathInterpolator().setPathTranslator(newPathTranslator());
117     }
118 
119     protected UrlNormalizer newUrlNormalizer() {
120         return new DefaultUrlNormalizer();
121     }
122 
123     protected PathTranslator newPathTranslator() {
124         return new DefaultPathTranslator();
125     }
126 
127     protected ModelInterpolator newModelInterpolator() {
128         UrlNormalizer normalizer = newUrlNormalizer();
129         PathTranslator pathTranslator = newPathTranslator();
130         return new StringVisitorModelInterpolator()
131                 .setPathTranslator(pathTranslator)
132                 .setUrlNormalizer(normalizer)
133                 .setVersionPropertiesProcessor(newModelVersionPropertiesProcessor());
134     }
135 
136     protected ModelVersionProcessor newModelVersionPropertiesProcessor() {
137         return new DefaultModelVersionProcessor();
138     }
139 
140     protected ModelValidator newModelValidator() {
141         return new DefaultModelValidator(newModelVersionPropertiesProcessor());
142     }
143 
144     protected ModelNormalizer newModelNormalizer() {
145         return new DefaultModelNormalizer();
146     }
147 
148     protected ModelPathTranslator newModelPathTranslator() {
149         return new DefaultModelPathTranslator().setPathTranslator(newPathTranslator());
150     }
151 
152     protected ModelUrlNormalizer newModelUrlNormalizer() {
153         return new DefaultModelUrlNormalizer().setUrlNormalizer(newUrlNormalizer());
154     }
155 
156     protected InheritanceAssembler newInheritanceAssembler() {
157         return new DefaultInheritanceAssembler();
158     }
159 
160     protected ProfileInjector newProfileInjector() {
161         return new DefaultProfileInjector();
162     }
163 
164     protected SuperPomProvider newSuperPomProvider() {
165         return new DefaultSuperPomProvider().setModelProcessor(newModelProcessor());
166     }
167 
168     protected DependencyManagementImporter newDependencyManagementImporter() {
169         return new DefaultDependencyManagementImporter();
170     }
171 
172     protected DependencyManagementInjector newDependencyManagementInjector() {
173         return new DefaultDependencyManagementInjector();
174     }
175 
176     protected LifecycleBindingsInjector newLifecycleBindingsInjector() {
177         return new StubLifecycleBindingsInjector();
178     }
179 
180     protected PluginManagementInjector newPluginManagementInjector() {
181         return new DefaultPluginManagementInjector();
182     }
183 
184     protected PluginConfigurationExpander newPluginConfigurationExpander() {
185         return new DefaultPluginConfigurationExpander();
186     }
187 
188     protected ReportConfigurationExpander newReportConfigurationExpander() {
189         return new DefaultReportConfigurationExpander();
190     }
191 
192     protected ReportingConverter newReportingConverter() {
193         return new DefaultReportingConverter();
194     }
195 
196     /**
197      * Creates a new model builder instance.
198      *
199      * @return The new model builder instance, never {@code null}.
200      */
201     public DefaultModelBuilder newInstance() {
202         DefaultModelBuilder modelBuilder = new DefaultModelBuilder();
203 
204         modelBuilder.setModelProcessor(newModelProcessor());
205         modelBuilder.setModelValidator(newModelValidator());
206         modelBuilder.setModelNormalizer(newModelNormalizer());
207         modelBuilder.setModelPathTranslator(newModelPathTranslator());
208         modelBuilder.setModelUrlNormalizer(newModelUrlNormalizer());
209         modelBuilder.setModelInterpolator(newModelInterpolator());
210         modelBuilder.setInheritanceAssembler(newInheritanceAssembler());
211         modelBuilder.setProfileInjector(newProfileInjector());
212         modelBuilder.setProfileSelector(newProfileSelector());
213         modelBuilder.setSuperPomProvider(newSuperPomProvider());
214         modelBuilder.setDependencyManagementImporter(newDependencyManagementImporter());
215         modelBuilder.setDependencyManagementInjector(newDependencyManagementInjector());
216         modelBuilder.setLifecycleBindingsInjector(newLifecycleBindingsInjector());
217         modelBuilder.setPluginManagementInjector(newPluginManagementInjector());
218         modelBuilder.setPluginConfigurationExpander(newPluginConfigurationExpander());
219         modelBuilder.setReportConfigurationExpander(newReportConfigurationExpander());
220         modelBuilder.setReportingConverter(newReportingConverter());
221         modelBuilder.setProfileActivationFilePathInterpolator(newProfileActivationFilePathInterpolator());
222 
223         return modelBuilder;
224     }
225 
226     private static class StubLifecycleBindingsInjector implements LifecycleBindingsInjector {
227 
228         @Override
229         public void injectLifecycleBindings(
230                 Model model, ModelBuildingRequest request, ModelProblemCollector problems) {}
231     }
232 }