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.Date;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.Properties;
27  import java.util.Set;
28  
29  import org.apache.commons.io.FileUtils;
30  import org.apache.commons.io.IOUtils;
31  import org.apache.maven.plugin.MojoExecutionException;
32  import org.apache.maven.plugin.MojoFailureException;
33  import org.codehaus.plexus.util.StringUtils;
34  import org.springframework.util.PropertyPlaceholderHelper;
35  
36  /**
37   * Write project properties to a file.
38   * 
39   * @author Jeff Caddel
40   * 
41   * @goal write-project-properties
42   */
43  public class WriteProjectProperties extends AbstractWritePropertiesMojo {
44  
45  	/**
46  	 * If true, the plugin will include system properties when writing the properties file. System properties override both environment
47  	 * variables and project properties.
48  	 * 
49  	 * @parameter default-value="false" expression="${properties.includeSystemProperties}"
50  	 */
51  	private boolean includeSystemProperties;
52  
53  	/**
54  	 * If true, the plugin will include environment variables when writing the properties file. Environment variables are prefixed with
55  	 * "env". Environment variables override project properties.
56  	 * 
57  	 * @parameter default-value="false" expression="${properties.includeEnvironmentVariables}"
58  	 */
59  	private boolean includeEnvironmentVariables;
60  
61  	/**
62  	 * Comma separated set of properties to exclude when writing the properties file
63  	 * 
64  	 * @parameter expression="${properties.exclude}"
65  	 */
66  	private String exclude;
67  
68  	/**
69  	 * Comma separated set of properties to write to the properties file. If provided, only the properties matching those supplied here will
70  	 * be written to the properties file.
71  	 * 
72  	 * @parameter expression="${properties.include}"
73  	 */
74  	private String include;
75  
76  	/**
77  	 * If true placeholders are resolved before writing properties to the file
78  	 * 
79  	 * @parameter expression="${properties.resolvePlaceholders}"
80  	 */
81  	private boolean resolvePlaceholders;
82  
83  	@Override
84  	public void execute() throws MojoExecutionException, MojoFailureException {
85  		Properties properties = new Properties();
86  		// Add project properties
87  		properties.putAll(project.getProperties());
88  		if (includeEnvironmentVariables) {
89  			// Add environment variables, overriding any existing properties with the same key
90  			properties.putAll(getEnvironmentVariables());
91  		}
92  		if (includeSystemProperties) {
93  			// Add system properties, overriding any existing properties with the same key
94  			properties.putAll(System.getProperties());
95  		}
96  
97  		// Remove properties as appropriate
98  		trim(properties, exclude, include);
99  
100 		if (resolvePlaceholders) {
101 			properties = getResolvedProperties(properties);
102 		}
103 
104 		String comment = "# " + new Date() + "\n";
105 		getLog().info("Creating " + outputFile);
106 		writeProperties(outputFile, comment, properties);
107 	}
108 
109 	protected Properties getResolvedProperties(Properties props) {
110 		PropertyPlaceholderHelper pph = new PropertyPlaceholderHelper("${", "}");
111 		List<String> keys = new ArrayList<String>(props.stringPropertyNames());
112 		Collections.sort(keys);
113 		Properties newProps = new Properties();
114 		for (String key : keys) {
115 			String originalValue = props.getProperty(key);
116 			String resolvedValue = pph.replacePlaceholders(originalValue, props);
117 			newProps.setProperty(key, resolvedValue);
118 		}
119 		return newProps;
120 
121 	}
122 
123 	protected static Properties getEnvironmentVariables() {
124 		String prefix = "env";
125 		Map<String, String> map = System.getenv();
126 		Properties props = new Properties();
127 		for (String key : map.keySet()) {
128 			String newKey = prefix + "." + key;
129 			String value = map.get(key);
130 			props.setProperty(newKey, value);
131 		}
132 		return props;
133 	}
134 
135 	protected void trim(Properties properties, String excludeCSV, String includeCSV) {
136 		List<String> omitKeys = ReadPropertiesMojo.getListFromCSV(excludeCSV);
137 		for (String key : omitKeys) {
138 			properties.remove(key);
139 		}
140 		if (StringUtils.isBlank(includeCSV)) {
141 			return;
142 		}
143 		List<String> includeKeys = ReadPropertiesMojo.getListFromCSV(includeCSV);
144 		Set<String> keys = properties.stringPropertyNames();
145 		for (String key : keys) {
146 			if (!includeKeys.contains(key)) {
147 				properties.remove(key);
148 			}
149 		}
150 	}
151 
152 	protected void writeProperties(File file, String comment, Properties properties) throws MojoExecutionException {
153 		SortedProperties sp = new SortedProperties();
154 		sp.putAll(properties);
155 		OutputStream out = null;
156 		try {
157 			out = FileUtils.openOutputStream(file);
158 			sp.store(out, comment);
159 		} catch (IOException e) {
160 			throw new MojoExecutionException("Error creating properties file", e);
161 		} finally {
162 			IOUtils.closeQuietly(out);
163 		}
164 	}
165 
166 	public boolean isIncludeSystemProperties() {
167 		return includeSystemProperties;
168 	}
169 
170 	public void setIncludeSystemProperties(boolean includeSystemProperties) {
171 		this.includeSystemProperties = includeSystemProperties;
172 	}
173 
174 	public boolean isIncludeEnvironmentVariables() {
175 		return includeEnvironmentVariables;
176 	}
177 
178 	public void setIncludeEnvironmentVariables(boolean includeEnvironmentVariables) {
179 		this.includeEnvironmentVariables = includeEnvironmentVariables;
180 	}
181 
182 	public String getExclude() {
183 		return exclude;
184 	}
185 
186 	public void setExclude(String exclude) {
187 		this.exclude = exclude;
188 	}
189 
190 	public String getInclude() {
191 		return include;
192 	}
193 
194 	public void setInclude(String include) {
195 		this.include = include;
196 	}
197 
198 	public boolean isResolvePlaceholders() {
199 		return resolvePlaceholders;
200 	}
201 
202 	public void setResolvePlaceholders(boolean resolvePlaceholders) {
203 		this.resolvePlaceholders = resolvePlaceholders;
204 	}
205 }