001 /**
002 * Copyright 2009-2012 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.codehaus.mojo.properties;
017
018 import java.io.File;
019 import java.io.IOException;
020 import java.util.ArrayList;
021 import java.util.Collections;
022 import java.util.Enumeration;
023 import java.util.List;
024 import java.util.Properties;
025
026 import org.apache.commons.io.FileUtils;
027 import org.apache.maven.plugin.MojoExecutionException;
028 import org.apache.maven.plugin.MojoFailureException;
029
030 /**
031 * Writes project properties to a file.
032 *
033 * @author <a href="mailto:zarars@gmail.com">Zarar Siddiqi</a>
034 * @version $Id: WriteProjectProperties.java 9747 2009-05-20 13:27:44Z mark $
035 * @goal write-project-properties
036 */
037 public class WriteProjectProperties extends AbstractWritePropertiesMojo {
038
039 /**
040 * If true, the plugin will create a second properties file with a ".sorted" extension that contains the properties
041 * sorted by name
042 *
043 * @parameter default-value="false" expression="${properties.writeSorted}"
044 */
045 private boolean writeSorted;
046
047 @Override
048 public void execute() throws MojoExecutionException, MojoFailureException {
049 validateOutputFile();
050 Properties properties = new Properties();
051 properties.putAll(project.getProperties());
052
053 Properties systemProperties = System.getProperties();
054
055 // allow system properties to over write key/value found in maven properties
056 Enumeration<?> enumeration = systemProperties.keys();
057 while (enumeration.hasMoreElements()) {
058 String key = (String) enumeration.nextElement();
059 String value = systemProperties.getProperty(key);
060 if (properties.get(key) != null) {
061 properties.put(key, value);
062 }
063
064 }
065
066 getLog().info("Creating " + outputFile);
067 writeProperties(properties, outputFile);
068 if (writeSorted) {
069 createSorted(outputFile, properties);
070 }
071 }
072
073 protected void createSorted(File file, Properties properties) throws MojoExecutionException {
074 List<String> names = new ArrayList<String>(properties.stringPropertyNames());
075 Collections.sort(names);
076 StringBuilder sb = new StringBuilder();
077 for (String name : names) {
078 String value = properties.getProperty(name);
079 value = value.replace("\n", "\\n");
080 value = value.replace("\t", "\\t");
081 value = value.replace(":", "\\:");
082 value = value.replace("#", "\\#");
083 value = value.replace("=", "\\=");
084 sb.append(name + "=" + value + "\n");
085 }
086 try {
087 String filename = outputFile.getAbsolutePath() + ".sorted";
088 getLog().info("Creating " + filename);
089 FileUtils.writeByteArrayToFile(new File(filename), sb.toString().getBytes());
090 } catch (IOException e) {
091 throw new MojoExecutionException("Error creating sorted file", e);
092 }
093 }
094
095 public boolean isWriteSorted() {
096 return writeSorted;
097 }
098
099 public void setWriteSorted(boolean writeSorted) {
100 this.writeSorted = writeSorted;
101 }
102 }