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