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.util.ArrayList;
21  import java.util.Collections;
22  import java.util.Enumeration;
23  import java.util.List;
24  import java.util.Properties;
25  
26  import org.apache.commons.io.FileUtils;
27  import org.apache.maven.plugin.MojoExecutionException;
28  import org.apache.maven.plugin.MojoFailureException;
29  
30  /**
31   * Writes project properties to a file.
32   *
33   * @author <a href="mailto:zarars@gmail.com">Zarar Siddiqi</a>
34   * @version $Id: WriteProjectProperties.java 9747 2009-05-20 13:27:44Z mark $
35   * @goal write-project-properties
36   */
37  public class WriteProjectProperties extends AbstractWritePropertiesMojo {
38  
39      /**
40       * If true, the plugin will create a second properties file with a ".sorted" extension that contains the properties
41       * sorted by name
42       *
43       * @parameter default-value="false" expression="${properties.writeSorted}"
44       */
45      private boolean writeSorted;
46  
47      @Override
48      public void execute() throws MojoExecutionException, MojoFailureException {
49          validateOutputFile();
50          Properties properties = new Properties();
51          properties.putAll(project.getProperties());
52  
53          Properties systemProperties = System.getProperties();
54  
55          // allow system properties to over write key/value found in maven properties
56          Enumeration<?> enumeration = systemProperties.keys();
57          while (enumeration.hasMoreElements()) {
58              String key = (String) enumeration.nextElement();
59              String value = systemProperties.getProperty(key);
60              if (properties.get(key) != null) {
61                  properties.put(key, value);
62              }
63  
64          }
65  
66          getLog().info("Creating " + outputFile);
67          writeProperties(properties, outputFile);
68          if (writeSorted) {
69              createSorted(outputFile, properties);
70          }
71      }
72  
73      protected void createSorted(File file, Properties properties) throws MojoExecutionException {
74          List<String> names = new ArrayList<String>(properties.stringPropertyNames());
75          Collections.sort(names);
76          StringBuilder sb = new StringBuilder();
77          for (String name : names) {
78              String value = properties.getProperty(name);
79              value = value.replace("\n", "\\n");
80              value = value.replace("\t", "\\t");
81              value = value.replace(":", "\\:");
82              value = value.replace("#", "\\#");
83              value = value.replace("=", "\\=");
84              sb.append(name + "=" + value + "\n");
85          }
86          try {
87              String filename = outputFile.getAbsolutePath() + ".sorted";
88              getLog().info("Creating " + filename);
89              FileUtils.writeByteArrayToFile(new File(filename), sb.toString().getBytes());
90          } catch (IOException e) {
91              throw new MojoExecutionException("Error creating sorted file", e);
92          }
93      }
94  
95      public boolean isWriteSorted() {
96          return writeSorted;
97      }
98  
99      public void setWriteSorted(boolean writeSorted) {
100         this.writeSorted = writeSorted;
101     }
102 }