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.util.List;
24  import java.util.Map;
25  import java.util.Properties;
26  import java.util.Set;
27  
28  import org.apache.maven.plugin.AbstractMojo;
29  import org.apache.maven.plugin.MojoExecutionException;
30  
31  /**
32   * Dumps this mojo's configuration into a properties file. Note that this mojo uses a custom component configurator.
33   *
34   * @goal custom-config
35   * @phase validate
36   * @configurator coreit
37   *
38   * @author Benjamin Bentmann
39   *
40   */
41  public class CustomConfigMojo
42      extends AbstractMojo
43  {
44  
45      /**
46       * The current project's base directory, used for path alignment.
47       *
48       * @parameter default-value="${basedir}"
49       * @readonly
50       */
51      private File basedir;
52  
53      /**
54       * The path to the properties file into which to save the mojo configuration.
55       *
56       * @parameter property="config.propertiesFile"
57       */
58      private File propertiesFile;
59  
60      /**
61       * A parameter being set only by the custom configurator as a proof of its execution.
62       *
63       * @parameter
64       */
65      String customParam;
66  
67      /**
68       * A parameter with a constant default value. <em>Note:</em> This has intentionally a different default value than
69       * the equally named parameter from {@link ConfigMojo}.
70       *
71       * @parameter default-value="test"
72       */
73      private String defaultParam;
74  
75      /**
76       * A simple parameter of type {@link java.lang.String}.
77       *
78       * @parameter property="config.stringParam"
79       */
80      private String stringParam;
81  
82      /**
83       * A simple parameter of type {@link java.io.File}.
84       *
85       * @parameter property="config.fileParam"
86       */
87      private File fileParam;
88  
89      /**
90       * An array parameter of component type {@link java.lang.String}.
91       *
92       * @parameter
93       */
94      private String[] stringParams;
95  
96      /**
97       * An array parameter of component type {@link java.io.File}.
98       *
99       * @parameter
100      */
101     private File[] fileParams;
102 
103     /**
104      * A collection parameter of type {@link java.util.List}.
105      *
106      * @parameter
107      */
108     private List listParam;
109 
110     /**
111      * A collection parameter of type {@link java.util.Set}.
112      *
113      * @parameter
114      */
115     private Set setParam;
116 
117     /**
118      * A collection parameter of type {@link java.util.Map}.
119      *
120      * @parameter
121      */
122     private Map mapParam;
123 
124     /**
125      * A collection parameter of type {@link java.util.Properties}.
126      *
127      * @parameter
128      */
129     private Properties propertiesParam;
130 
131     /**
132      * Runs this mojo.
133      *
134      * @throws MojoExecutionException If the output file could not be created.
135      */
136     public void execute()
137         throws MojoExecutionException
138     {
139         getLog().info( "[MAVEN-CORE-IT-LOG] Using output file path: " + propertiesFile );
140 
141         if ( propertiesFile == null )
142         {
143             throw new MojoExecutionException( "Path name for output file has not been specified" );
144         }
145 
146         if ( !propertiesFile.isAbsolute() )
147         {
148             propertiesFile = new File( basedir, propertiesFile.getPath() ).getAbsoluteFile();
149         }
150 
151         Properties configProps = new Properties();
152 
153         dumpConfiguration( configProps );
154 
155         getLog().info( "[MAVEN-CORE-IT-LOG] Creating output file: " + propertiesFile );
156 
157         PropertiesUtil.write( propertiesFile, configProps );
158 
159         getLog().info( "[MAVEN-CORE-IT-LOG] Created output file: " + propertiesFile );
160     }
161 
162     /**
163      * Dumps the mojo configuration into the specified properties.
164      *
165      * @param props The properties to dump the configuration into, must not be <code>null</code>.
166      */
167     private void dumpConfiguration( Properties props )
168     {
169         /*
170          * NOTE: This intentionally does not dump the absolute path of a file to check the actual value that was
171          * injected by Maven.
172          */
173         PropertiesUtil.serialize( props, "propertiesFile", propertiesFile );
174         PropertiesUtil.serialize( props, "customParam", customParam );
175         PropertiesUtil.serialize( props, "defaultParam", defaultParam );
176         PropertiesUtil.serialize( props, "stringParam", stringParam );
177         PropertiesUtil.serialize( props, "fileParam", fileParam );
178         PropertiesUtil.serialize( props, "stringParams", stringParams );
179         PropertiesUtil.serialize( props, "fileParams", fileParams );
180         PropertiesUtil.serialize( props, "listParam", listParam );
181         PropertiesUtil.serialize( props, "setParam", setParam );
182         PropertiesUtil.serialize( props, "mapParam", mapParam );
183         PropertiesUtil.serialize( props, "propertiesParam", propertiesParam );
184     }
185 
186 }