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.io.OutputStream;
21  import java.util.ArrayList;
22  import java.util.Collections;
23  import java.util.List;
24  import java.util.Properties;
25  
26  import org.apache.commons.io.FileUtils;
27  import org.apache.commons.io.IOUtils;
28  import org.apache.maven.plugin.AbstractMojo;
29  import org.apache.maven.plugin.MojoExecutionException;
30  import org.apache.maven.project.MavenProject;
31  import org.codehaus.plexus.util.StringUtils;
32  
33  /**
34   * @author <a href="mailto:zarars@gmail.com">Zarar Siddiqi</a>
35   * @version $Id: AbstractWritePropertiesMojo.java 8861 2009-01-21 15:35:38Z pgier $
36   */
37  public abstract class AbstractWritePropertiesMojo extends AbstractMojo {
38  
39  	/**
40  	 * @parameter default-value="${project}"
41  	 * @required
42  	 * @readonly
43  	 */
44  	MavenProject project;
45  
46  	/**
47  	 * The file that properties will be written to
48  	 *
49  	 * @parameter expression="${properties.outputFile}" default-value="${project.build.directory}/properties/project.properties";
50  	 * @required
51  	 */
52  	File outputFile;
53  
54  	/**
55  	 * Either <code>NORMAL</code> or <code>ENVIRONMENT_VARIABLE</code>. When set to <code>ENVIRONMENT_VARIABLE</code> the keys in the
56  	 * properties file will all be upper case with periods replaced by underscores.
57  	 *
58  	 * @parameter expression="${properties.outputStyle}" default-value="NORMAL"
59  	 * @required
60  	 */
61  	OutputStyle outputStyle;
62  
63  	/**
64  	 * If supplied property keys are prefixed with this value before being stored.
65  	 *
66  	 * @parameter expression="${properties.prefix}"
67  	 */
68  	String prefix;
69  
70  	protected void writeProperties(File file, Properties properties, OutputStyle outputStyle, String prefix) throws MojoExecutionException {
71  		Properties prefixed = getPrefixedProperties(properties, prefix);
72  		Properties formatted = getFormattedProperties(prefixed, outputStyle);
73  		Properties sorted = getSortedProperties(formatted);
74  		OutputStream out = null;
75  		try {
76  			out = FileUtils.openOutputStream(file);
77  			sorted.store(out, "Created by the properties-maven-plugin");
78  		} catch (IOException e) {
79  			throw new MojoExecutionException("Error creating properties file", e);
80  		} finally {
81  			IOUtils.closeQuietly(out);
82  		}
83  	}
84  
85  	protected Properties getStandardMavenProperties(MavenProject project) {
86  		Properties properties = new Properties();
87  		properties.setProperty("project.groupId", project.getGroupId());
88  		properties.setProperty("project.artifactId", project.getArtifactId());
89  		properties.setProperty("project.version", project.getVersion());
90  		properties.setProperty("project.basedir", project.getBasedir().getAbsolutePath());
91  		properties.setProperty("project.build.directory", project.getBuild().getDirectory());
92  		return properties;
93  	}
94  
95  	protected SortedProperties getSortedProperties(Properties properties) {
96  		SortedProperties sp = new SortedProperties();
97  		sp.putAll(properties);
98  		return sp;
99  	}
100 
101 	protected Properties getFormattedProperties(Properties properties, OutputStyle style) {
102 		switch (style) {
103 		case NORMAL:
104 			return properties;
105 		case ENVIRONMENT_VARIABLE:
106 			return getEnvironmentVariableProperties(properties);
107 		default:
108 			throw new IllegalArgumentException(outputStyle + " is unknown");
109 		}
110 	}
111 
112 	protected Properties getPrefixedProperties(Properties properties, String prefix) {
113 		if (StringUtils.isBlank(prefix)) {
114 			return properties;
115 		}
116 		List<String> keys = new ArrayList<String>(properties.stringPropertyNames());
117 		Collections.sort(keys);
118 		Properties newProperties = new Properties();
119 		for (String key : keys) {
120 			String value = properties.getProperty(key);
121 			String newKey = prefix + "." + key;
122 			newProperties.setProperty(newKey, value);
123 		}
124 		return newProperties;
125 	}
126 
127 	protected Properties getEnvironmentVariableProperties(Properties properties) {
128 		List<String> keys = new ArrayList<String>(properties.stringPropertyNames());
129 		Collections.sort(keys);
130 		Properties newProperties = new Properties();
131 		for (String key : keys) {
132 			String value = properties.getProperty(key);
133 			String newKey = key.toUpperCase().replace(".", "_");
134 			newProperties.setProperty(newKey, value);
135 		}
136 		return newProperties;
137 	}
138 }