View Javadoc
1   package org.apache.maven.model.building;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
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.DefaultReportingConverter;
53  import org.apache.maven.model.plugin.LifecycleBindingsInjector;
54  import org.apache.maven.model.plugin.PluginConfigurationExpander;
55  import org.apache.maven.model.plugin.ReportConfigurationExpander;
56  import org.apache.maven.model.plugin.ReportingConverter;
57  import org.apache.maven.model.profile.DefaultProfileInjector;
58  import org.apache.maven.model.profile.DefaultProfileSelector;
59  import org.apache.maven.model.profile.ProfileInjector;
60  import org.apache.maven.model.profile.ProfileSelector;
61  import org.apache.maven.model.profile.activation.FileProfileActivator;
62  import org.apache.maven.model.profile.activation.JdkVersionProfileActivator;
63  import org.apache.maven.model.profile.activation.OperatingSystemProfileActivator;
64  import org.apache.maven.model.profile.activation.ProfileActivator;
65  import org.apache.maven.model.profile.activation.PropertyProfileActivator;
66  import org.apache.maven.model.superpom.DefaultSuperPomProvider;
67  import org.apache.maven.model.superpom.SuperPomProvider;
68  import org.apache.maven.model.validation.DefaultModelValidator;
69  import org.apache.maven.model.validation.ModelValidator;
70  
71  /**
72   * A factory to create model builder instances when no dependency injection is available. <em>Note:</em> This class is
73   * only meant as a utility for developers that want to employ the model builder outside of the Maven build system, Maven
74   * plugins should always acquire model builder instances via dependency injection. Developers might want to subclass
75   * this factory to provide custom implementations for some of the components used by the model builder.
76   *
77   * @author Benjamin Bentmann
78   */
79  public class DefaultModelBuilderFactory
80  {
81  
82      protected ModelProcessor newModelProcessor()
83      {
84          DefaultModelProcessor processor = new DefaultModelProcessor();
85          processor.setModelLocator( newModelLocator() );
86          processor.setModelReader( newModelReader() );
87          return processor;
88      }
89  
90      protected ModelLocator newModelLocator()
91      {
92          return new DefaultModelLocator();
93      }
94  
95      protected ModelReader newModelReader()
96      {
97          return new DefaultModelReader();
98      }
99  
100     protected ProfileSelector newProfileSelector()
101     {
102         DefaultProfileSelector profileSelector = new DefaultProfileSelector();
103 
104         for ( ProfileActivator activator : newProfileActivators() )
105         {
106             profileSelector.addProfileActivator( activator );
107         }
108 
109         return profileSelector;
110     }
111 
112     protected ProfileActivator[] newProfileActivators()
113     {
114         return new ProfileActivator[] { new JdkVersionProfileActivator(), new OperatingSystemProfileActivator(),
115             new PropertyProfileActivator(), new FileProfileActivator()
116                         .setProfileActivationFilePathInterpolator( newProfileActivationFilePathInterpolator() ) };
117     }
118 
119     protected ProfileActivationFilePathInterpolator newProfileActivationFilePathInterpolator()
120     {
121         return new ProfileActivationFilePathInterpolator().setPathTranslator( newPathTranslator() );
122     }
123 
124     protected UrlNormalizer newUrlNormalizer()
125     {
126         return new DefaultUrlNormalizer();
127     }
128 
129     protected PathTranslator newPathTranslator()
130     {
131         return new DefaultPathTranslator();
132     }
133 
134     protected ModelInterpolator newModelInterpolator()
135     {
136         UrlNormalizer normalizer = newUrlNormalizer();
137         PathTranslator pathTranslator = newPathTranslator();
138         return new StringVisitorModelInterpolator().setPathTranslator( pathTranslator ).setUrlNormalizer( normalizer )
139                         .setVersionPropertiesProcessor( newModelVersionPropertiesProcessor() );
140     }
141 
142     protected ModelVersionProcessor newModelVersionPropertiesProcessor()
143     {
144         return new DefaultModelVersionProcessor();
145     }
146 
147     protected ModelValidator newModelValidator()
148     {
149         return new DefaultModelValidator( newModelVersionPropertiesProcessor() );
150     }
151 
152     protected ModelNormalizer newModelNormalizer()
153     {
154         return new DefaultModelNormalizer();
155     }
156 
157     protected ModelPathTranslator newModelPathTranslator()
158     {
159         return new DefaultModelPathTranslator().setPathTranslator( newPathTranslator() );
160     }
161 
162     protected ModelUrlNormalizer newModelUrlNormalizer()
163     {
164         return new DefaultModelUrlNormalizer().setUrlNormalizer( newUrlNormalizer() );
165     }
166 
167     protected InheritanceAssembler newInheritanceAssembler()
168     {
169         return new DefaultInheritanceAssembler();
170     }
171 
172     protected ProfileInjector newProfileInjector()
173     {
174         return new DefaultProfileInjector();
175     }
176 
177     protected SuperPomProvider newSuperPomProvider()
178     {
179         return new DefaultSuperPomProvider().setModelProcessor( newModelProcessor() );
180     }
181 
182     protected DependencyManagementImporter newDependencyManagementImporter()
183     {
184         return new DefaultDependencyManagementImporter();
185     }
186 
187     protected DependencyManagementInjector newDependencyManagementInjector()
188     {
189         return new DefaultDependencyManagementInjector();
190     }
191 
192     protected LifecycleBindingsInjector newLifecycleBindingsInjector()
193     {
194         return new StubLifecycleBindingsInjector();
195     }
196 
197     protected PluginManagementInjector newPluginManagementInjector()
198     {
199         return new DefaultPluginManagementInjector();
200     }
201 
202     protected PluginConfigurationExpander newPluginConfigurationExpander()
203     {
204         return new DefaultPluginConfigurationExpander();
205     }
206 
207     protected ReportConfigurationExpander newReportConfigurationExpander()
208     {
209         return new DefaultReportConfigurationExpander();
210     }
211 
212     protected ReportingConverter newReportingConverter()
213     {
214         return new DefaultReportingConverter();
215     }
216 
217     /**
218      * Creates a new model builder instance.
219      *
220      * @return The new model builder instance, never {@code null}.
221      */
222     public DefaultModelBuilder newInstance()
223     {
224         DefaultModelBuilder modelBuilder = new DefaultModelBuilder();
225 
226         modelBuilder.setModelProcessor( newModelProcessor() );
227         modelBuilder.setModelValidator( newModelValidator() );
228         modelBuilder.setModelNormalizer( newModelNormalizer() );
229         modelBuilder.setModelPathTranslator( newModelPathTranslator() );
230         modelBuilder.setModelUrlNormalizer( newModelUrlNormalizer() );
231         modelBuilder.setModelInterpolator( newModelInterpolator() );
232         modelBuilder.setInheritanceAssembler( newInheritanceAssembler() );
233         modelBuilder.setProfileInjector( newProfileInjector() );
234         modelBuilder.setProfileSelector( newProfileSelector() );
235         modelBuilder.setSuperPomProvider( newSuperPomProvider() );
236         modelBuilder.setDependencyManagementImporter( newDependencyManagementImporter() );
237         modelBuilder.setDependencyManagementInjector( newDependencyManagementInjector() );
238         modelBuilder.setLifecycleBindingsInjector( newLifecycleBindingsInjector() );
239         modelBuilder.setPluginManagementInjector( newPluginManagementInjector() );
240         modelBuilder.setPluginConfigurationExpander( newPluginConfigurationExpander() );
241         modelBuilder.setReportConfigurationExpander( newReportConfigurationExpander() );
242         modelBuilder.setReportingConverter( newReportingConverter() );
243         modelBuilder.setProfileActivationFilePathInterpolator( newProfileActivationFilePathInterpolator() );
244 
245         return modelBuilder;
246     }
247 
248     private static class StubLifecycleBindingsInjector
249         implements LifecycleBindingsInjector
250     {
251 
252         @Override
253         public void injectLifecycleBindings( Model model, ModelBuildingRequest request, ModelProblemCollector problems )
254         {
255         }
256 
257     }
258 
259 }