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