View Javadoc

1   package org.apache.maven.plugin.coreit;
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 java.io.File;
23  import java.net.URI;
24  import java.net.URL;
25  import java.util.Date;
26  import java.util.List;
27  import java.util.Map;
28  import java.util.Properties;
29  import java.util.Set;
30  
31  import org.apache.maven.plugin.AbstractMojo;
32  import org.apache.maven.plugin.MojoExecutionException;
33  import org.codehaus.plexus.configuration.PlexusConfiguration;
34  
35  /**
36   * Dumps this mojo's configuration into a properties file.
37   * 
38   * @goal config
39   * @phase validate
40   * 
41   * @author Benjamin Bentmann
42   * @version $Id: ConfigMojo.java 1070148 2011-02-12 20:31:55Z bentmann $
43   */
44  public class ConfigMojo
45      extends AbstractMojo
46  {
47  
48      /**
49       * The current project's base directory, used for path alignment.
50       * 
51       * @parameter default-value="${basedir}"
52       * @readonly
53       */
54      private File basedir;
55  
56      /**
57       * The path to the properties file into which to save the mojo configuration.
58       * 
59       * @parameter expression="${config.propertiesFile}"
60       */
61      private File propertiesFile;
62  
63      /**
64       * A parameter with an alias.
65       * 
66       * @parameter alias="aliasParamLegacy"
67       */
68      private String aliasParam;
69  
70      /**
71       * A parameter with a constant default value.
72       * 
73       * @parameter default-value="maven-core-it"
74       */
75      private String defaultParam;
76  
77      /**
78       * A parameter with a default value using multiple expressions.
79       * 
80       * @parameter default-value="${project.groupId}:${project.artifactId}:${project.version}"
81       */
82      private String defaultParamWithExpression;
83  
84      /**
85       * A parameter that combines all of the annotations.
86       * 
87       * @parameter alias="fullyAnnotatedParam" expression="${config.aliasDefaultExpressionParam}" default-value="test"
88       */
89      private String aliasDefaultExpressionParam;
90  
91      /**
92       * A simple parameter of type {@link java.lang.Boolean}.
93       * 
94       * @parameter expression="${config.booleanParam}"
95       */
96      private Boolean booleanParam;
97  
98      /**
99       * A simple parameter of type {@link java.lang.Boolean#TYPE}.
100      * 
101      * @parameter expression="${config.primitiveBooleanParam}"
102      */
103     private boolean primitiveBooleanParam;
104 
105     /**
106      * A simple parameter of type {@link java.lang.Byte}.
107      * 
108      * @parameter expression="${config.byteParam}"
109      */
110     private Byte byteParam;
111 
112     /**
113      * A simple parameter of type {@link java.lang.Short}.
114      * 
115      * @parameter expression="${config.shortParam}"
116      */
117     private Short shortParam;
118 
119     /**
120      * A simple parameter of type {@link java.lang.Integer}.
121      * 
122      * @parameter expression="${config.intergerParam}"
123      */
124     private Integer integerParam;
125 
126     /**
127      * A simple parameter of type {@link java.lang.Integer#TYPE}.
128      * 
129      * @parameter expression="${config.primitiveIntegerParam}"
130      */
131     private int primitiveIntegerParam;
132 
133     /**
134      * A simple parameter of type {@link java.lang.Long}.
135      * 
136      * @parameter expression="${config.longParam}"
137      */
138     private Long longParam;
139 
140     /**
141      * A simple parameter of type {@link java.lang.Float}.
142      * 
143      * @parameter expression="${config.floatParam}"
144      */
145     private Float floatParam;
146 
147     /**
148      * A simple parameter of type {@link java.lang.Double}.
149      * 
150      * @parameter expression="${config.doubleParam}"
151      */
152     private Double doubleParam;
153 
154     /**
155      * A simple parameter of type {@link java.lang.Character}.
156      * 
157      * @parameter expression="${config.characterParam}"
158      */
159     private Character characterParam;
160 
161     /**
162      * A simple parameter of type {@link java.lang.String}.
163      * 
164      * @parameter expression="${config.stringParam}"
165      */
166     private String stringParam;
167 
168     /**
169      * A simple parameter of type {@link java.io.File}.
170      * 
171      * @parameter expression="${config.fileParam}"
172      */
173     private File fileParam;
174 
175     /**
176      * A simple parameter of type {@link java.util.Date}.
177      * 
178      * @parameter expression="${config.dateParam}"
179      */
180     private Date dateParam;
181 
182     /**
183      * A simple parameter of type {@link java.net.URL}.
184      * 
185      * @parameter expression="${config.urlParam}"
186      */
187     private URL urlParam;
188 
189     /**
190      * A simple parameter of type {@link java.net.URI} (requires Maven 3.x).
191      * 
192      * @parameter
193      */
194     private URI uriParam;
195 
196     /**
197      * An array parameter of component type {@link java.lang.String}.
198      * 
199      * @parameter
200      */
201     private String[] stringParams;
202 
203     /**
204      * An array parameter of component type {@link java.io.File}.
205      * 
206      * @parameter
207      */
208     private File[] fileParams;
209 
210     /**
211      * A collection parameter of type {@link java.util.List}.
212      * 
213      * @parameter
214      */
215     private List listParam;
216 
217     /**
218      * A collection parameter of type {@link java.util.Set}.
219      * 
220      * @parameter
221      */
222     private Set setParam;
223 
224     /**
225      * A collection parameter of type {@link java.util.Map}.
226      * 
227      * @parameter
228      */
229     private Map mapParam;
230 
231     /**
232      * A collection parameter of type {@link java.util.Properties}.
233      * 
234      * @parameter
235      */
236     private Properties propertiesParam;
237 
238     /**
239      * A complex parameter with an alias.
240      * 
241      * @parameter alias="aliasStringParamsLegacy"
242      */
243     private String[] aliasStringParams;
244 
245     /**
246      * A complex parameter of type {@link org.apache.maven.plugin.coreit.Bean}.
247      * 
248      * @parameter
249      */
250     private Bean beanParam;
251 
252     /**
253      * A raw DOM snippet.
254      * 
255      * @parameter
256      */
257     private PlexusConfiguration domParam;
258 
259     /**
260      * Runs this mojo.
261      * 
262      * @throws MojoExecutionException If the output file could not be created.
263      */
264     public void execute()
265         throws MojoExecutionException
266     {
267         getLog().info( "[MAVEN-CORE-IT-LOG] Using output file path: " + propertiesFile );
268 
269         if ( propertiesFile == null )
270         {
271             throw new MojoExecutionException( "Path name for output file has not been specified" );
272         }
273 
274         if ( !propertiesFile.isAbsolute() )
275         {
276             propertiesFile = new File( basedir, propertiesFile.getPath() ).getAbsoluteFile();
277         }
278 
279         Properties configProps = new Properties();
280 
281         dumpConfiguration( configProps );
282 
283         getLog().info( "[MAVEN-CORE-IT-LOG] Creating output file: " + propertiesFile );
284 
285         PropertiesUtil.write( propertiesFile, configProps );
286 
287         getLog().info( "[MAVEN-CORE-IT-LOG] Created output file: " + propertiesFile );
288     }
289 
290     /**
291      * Dumps the mojo configuration into the specified properties.
292      * 
293      * @param props The properties to dump the configuration into, must not be <code>null</code>.
294      */
295     private void dumpConfiguration( Properties props )
296     {
297         /*
298          * NOTE: This intentionally does not dump the absolute path of a file to check the actual value that was
299          * injected by Maven.
300          */
301         PropertiesUtil.serialize( props, "propertiesFile", propertiesFile );
302         PropertiesUtil.serialize( props, "aliasParam", aliasParam );
303         PropertiesUtil.serialize( props, "defaultParam", defaultParam );
304         PropertiesUtil.serialize( props, "defaultParamWithExpression", defaultParamWithExpression );
305         PropertiesUtil.serialize( props, "aliasDefaultExpressionParam", aliasDefaultExpressionParam );
306         PropertiesUtil.serialize( props, "booleanParam", booleanParam );
307         if ( primitiveBooleanParam )
308         {
309             PropertiesUtil.serialize( props, "primitiveBooleanParam", Boolean.valueOf( primitiveBooleanParam ) );
310         }
311         PropertiesUtil.serialize( props, "byteParam", byteParam );
312         PropertiesUtil.serialize( props, "shortParam", shortParam );
313         PropertiesUtil.serialize( props, "integerParam", integerParam );
314         if ( primitiveIntegerParam != 0 )
315         {
316             PropertiesUtil.serialize( props, "primitiveIntegerParam", new Integer( primitiveIntegerParam ) );
317         }
318         PropertiesUtil.serialize( props, "longParam", longParam );
319         PropertiesUtil.serialize( props, "floatParam", floatParam );
320         PropertiesUtil.serialize( props, "doubleParam", doubleParam );
321         PropertiesUtil.serialize( props, "characterParam", characterParam );
322         PropertiesUtil.serialize( props, "stringParam", stringParam );
323         PropertiesUtil.serialize( props, "fileParam", fileParam );
324         PropertiesUtil.serialize( props, "dateParam", dateParam );
325         PropertiesUtil.serialize( props, "urlParam", urlParam );
326         PropertiesUtil.serialize( props, "uriParam", uriParam );
327         PropertiesUtil.serialize( props, "stringParams", stringParams );
328         PropertiesUtil.serialize( props, "fileParams", fileParams );
329         PropertiesUtil.serialize( props, "listParam", listParam );
330         PropertiesUtil.serialize( props, "setParam", setParam );
331         PropertiesUtil.serialize( props, "mapParam", mapParam );
332         PropertiesUtil.serialize( props, "propertiesParam", propertiesParam );
333         PropertiesUtil.serialize( props, "aliasStringParams", aliasStringParams );
334         PropertiesUtil.serialize( props, "domParam", domParam );
335         if ( beanParam != null )
336         {
337             PropertiesUtil.serialize( props, "beanParam.fieldParam", beanParam.fieldParam );
338             PropertiesUtil.serialize( props, "beanParam.setterParam", beanParam.setterParam );
339             PropertiesUtil.serialize( props, "beanParam.setterCalled", Boolean.valueOf( beanParam.setterCalled ) );
340         }
341     }
342 
343 }