View Javadoc

1   /**
2    * Copyright 2009-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.codehaus.mojo.properties;
17  
18  import java.io.File;
19  import java.io.IOException;
20  import java.text.SimpleDateFormat;
21  import java.util.ArrayList;
22  import java.util.Collections;
23  import java.util.Date;
24  import java.util.List;
25  import java.util.Properties;
26  
27  import org.apache.commons.io.FileUtils;
28  import org.apache.maven.plugin.MojoExecutionException;
29  import org.apache.maven.plugin.MojoFailureException;
30  
31  /**
32   * Writes project properties to a file.
33   *
34   * @author <a href="mailto:zarars@gmail.com">Zarar Siddiqi</a>
35   * @version $Id: WriteProjectProperties.java 9747 2009-05-20 13:27:44Z mark $
36   * @goal write-project-properties
37   */
38  public class WriteProjectProperties extends AbstractWritePropertiesMojo {
39  
40      /**
41       * If true, the plugin will create the properties file formatted the same way Ant formats properties files using the
42       * &lt;echoproperties&gt; task. The properties will be sorted by name with the ':', '#' and '=' symbols escaped with
43       * a backslash
44       *
45       * @parameter default-value="false" expression="${properties.antEchoPropertiesMode}"
46       */
47      private boolean antEchoPropertiesMode;
48  
49      /**
50       * If true, the plugin will include system properties when writing the properties file
51       *
52       * @parameter default-value="false" expression="${properties.includeSystemProperties}"
53       */
54      private boolean includeSystemProperties;
55  
56      /**
57       * Comma separated set of properties to omit from writing to the properties file
58       *
59       * @parameter expression="${properties.omit}"
60       */
61      private String omit;
62  
63      @Override
64      public void execute() throws MojoExecutionException, MojoFailureException {
65          validateOutputFile();
66          Properties properties = new Properties();
67          properties.putAll(project.getProperties());
68  
69          Properties systemProperties = System.getProperties();
70  
71          // Make sure system properties override Maven project properties
72          for (String key : systemProperties.stringPropertyNames()) {
73              String mavenValue = properties.getProperty(key);
74              String systemValue = systemProperties.getProperty(key);
75              // If we are including system properties, always put the system property
76              if (includeSystemProperties) {
77                  properties.put(key, systemValue);
78              } else if (mavenValue != null) {
79                  // Otherwise only update our properties object if the System property overrides a Maven project property
80                  properties.put(key, systemValue);
81              }
82          }
83  
84          remove(properties, omit);
85  
86          getLog().info("Creating " + outputFile);
87          if (antEchoPropertiesMode) {
88              echoPropertiesMode(outputFile, properties);
89          } else {
90              writeProperties(properties, outputFile);
91          }
92      }
93  
94      protected void remove(Properties properties, String csv) {
95          List<String> keys = ReadPropertiesMojo.getListFromCSV(csv);
96          for (String key : keys) {
97              properties.remove(key);
98          }
99  
100     }
101 
102     protected void echoPropertiesMode(File file, Properties properties) throws MojoExecutionException {
103         // DSTAMP=20120304
104         // TODAY=March 4 2012
105         // TSTAMP=1651
106         SimpleDateFormat dstamp = new SimpleDateFormat("yyyyMMdd");
107         SimpleDateFormat today = new SimpleDateFormat("MMMM d yyyy");
108         SimpleDateFormat tstamp = new SimpleDateFormat("HHmm");
109         List<String> names = new ArrayList<String>(properties.stringPropertyNames());
110         Collections.sort(names);
111         Date now = new Date();
112         StringBuilder sb = new StringBuilder();
113         sb.append("#Ant properties\n");
114         sb.append("#" + now + "\n");
115         sb.append("DSTAMP=" + dstamp.format(now) + "\n");
116         sb.append("TODAY=" + today.format(now) + "\n");
117         sb.append("TSTAMP=" + tstamp.format(now) + "\n");
118         for (String name : names) {
119             String value = properties.getProperty(name);
120             value = value.replace("\n", "\\n");
121             value = value.replace("\r", "\\r");
122             value = value.replace("\t", "\\t");
123             value = value.replace(":", "\\:");
124             value = value.replace("#", "\\#");
125             value = value.replace("=", "\\=");
126             sb.append(name + "=" + value + "\n");
127         }
128         try {
129             FileUtils.writeByteArrayToFile(file, sb.toString().getBytes());
130         } catch (IOException e) {
131             throw new MojoExecutionException("Error creating properties file", e);
132         }
133     }
134 
135     public boolean isAntEchoPropertiesMode() {
136         return antEchoPropertiesMode;
137     }
138 
139     public void setAntEchoPropertiesMode(boolean antEchoPropertiesMode) {
140         this.antEchoPropertiesMode = antEchoPropertiesMode;
141     }
142 
143     public boolean isIncludeSystemProperties() {
144         return includeSystemProperties;
145     }
146 
147     public void setIncludeSystemProperties(boolean includeSystemProperties) {
148         this.includeSystemProperties = includeSystemProperties;
149     }
150 }