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