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.Properties;
24  
25  import org.apache.maven.plugin.AbstractMojo;
26  import org.apache.maven.plugin.MojoExecutionException;
27  
28  /**
29   * Dumps this mojo's configuration into a properties file.
30   *
31   * @goal required-config
32   * @phase validate
33   *
34   * @author Benjamin Bentmann
35   */
36  public class RequiredConfigMojo
37      extends AbstractMojo
38  {
39  
40      /**
41       * The current project's base directory, used for path alignment.
42       *
43       * @parameter default-value="${basedir}"
44       * @readonly
45       */
46      private File basedir;
47  
48      /**
49       * The path to the properties file into which to save the mojo configuration.
50       *
51       * @parameter property="config.propertiesFile"
52       */
53      private File propertiesFile;
54  
55      /**
56       * A required parameter to be set via plugin configuration in POM or system property from CLI.
57       *
58       * @parameter property="config.requiredParam"
59       * @required
60       */
61      private String requiredParam;
62  
63      /**
64       * A required parameter that defaults to a non-mandatory value from the POM.
65       *
66       * @parameter default-value="${project.url}"
67       * @required
68       */
69      private String requiredParamWithDefault;
70  
71      /**
72       * Runs this mojo.
73       *
74       * @throws MojoExecutionException If the output file could not be created.
75       */
76      public void execute()
77          throws MojoExecutionException
78      {
79          getLog().info( "[MAVEN-CORE-IT-LOG] Using output file path: " + propertiesFile );
80  
81          if ( propertiesFile == null )
82          {
83              throw new MojoExecutionException( "Path name for output file has not been specified" );
84          }
85  
86          if ( !propertiesFile.isAbsolute() )
87          {
88              propertiesFile = new File( basedir, propertiesFile.getPath() ).getAbsoluteFile();
89          }
90  
91          Properties configProps = new Properties();
92  
93          dumpConfiguration( configProps );
94  
95          getLog().info( "[MAVEN-CORE-IT-LOG] Creating output file: " + propertiesFile );
96  
97          PropertiesUtil.write( propertiesFile, configProps );
98  
99          getLog().info( "[MAVEN-CORE-IT-LOG] Created output file: " + propertiesFile );
100     }
101 
102     /**
103      * Dumps the mojo configuration into the specified properties.
104      *
105      * @param props The properties to dump the configuration into, must not be <code>null</code>.
106      */
107     private void dumpConfiguration( Properties props )
108     {
109         PropertiesUtil.serialize( props, "requiredParam", requiredParam );
110         PropertiesUtil.serialize( props, "requiredParamWithDefault", requiredParamWithDefault );
111     }
112 
113 }