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.List;
20  import java.util.Properties;
21  
22  import org.apache.maven.plugin.MojoExecutionException;
23  import org.apache.maven.plugin.MojoFailureException;
24  import org.kuali.common.util.CollectionUtils;
25  import org.kuali.common.util.PropertyUtils;
26  import org.kuali.common.util.property.Constants;
27  import org.springframework.util.PropertyPlaceholderHelper;
28  
29  /**
30   * Write project properties to a file.
31   *
32   * @author Jeff Caddel
33   *
34   * @goal write-project-properties
35   */
36  public class WriteProjectProperties extends AbstractWritePropertiesMojo {
37  
38  	/**
39  	 * If true, the plugin will include system properties when writing the properties file. System properties override both environment
40  	 * variables and project properties.
41  	 *
42  	 * @parameter default-value="false" expression="${properties.includeSystemProperties}"
43  	 */
44  	private boolean includeSystemProperties;
45  
46  	/**
47  	 * If true, the plugin will include environment variables when writing the properties file. Environment variables are prefixed with
48  	 * "env". Environment variables override project properties.
49  	 *
50  	 * @parameter default-value="false" expression="${properties.includeEnvironmentVariables}"
51  	 */
52  	private boolean includeEnvironmentVariables;
53  
54  	/**
55  	 * Comma separated set of properties to exclude when writing the properties file
56  	 *
57  	 * @parameter expression="${properties.exclude}"
58  	 */
59  	private String exclude;
60  
61  	/**
62  	 * Comma separated set of properties to write to the properties file. If provided, only the properties matching those supplied here will
63  	 * be written to the properties file.
64  	 *
65  	 * @parameter expression="${properties.include}"
66  	 */
67  	private String include;
68  
69  	/**
70  	 * List of properties to include
71  	 *
72  	 * @parameter
73  	 */
74  	private List<String> includes;
75  
76  	/**
77  	 * List of properties to exclude
78  	 *
79  	 * @parameter
80  	 */
81  	private List<String> excludes;
82  
83  	/**
84  	 * If true placeholders are resolved before writing properties to the file
85  	 *
86  	 * @parameter expression="${properties.resolvePlaceholders}" default-value="false"
87  	 */
88  	private boolean resolvePlaceholders;
89  
90  	/**
91  	 * @parameter expression="${properties.includeStandardMavenProperties}" default-value="false"
92  	 */
93  	private boolean includeStandardMavenProperties;
94  
95  	@Override
96  	public void execute() throws MojoExecutionException, MojoFailureException {
97  		Properties properties = new Properties();
98  
99  		// Add project properties
100 		properties.putAll(project.getProperties());
101 
102 		if (includeStandardMavenProperties) {
103 			properties.putAll(getStandardMavenProperties(project));
104 		}
105 
106 		// Add environment variables, overriding any existing properties with the same key
107 		if (includeEnvironmentVariables) {
108 			properties.putAll(PropertyUtils.getEnvAsProperties());
109 		}
110 
111 		// Add system properties, overriding any existing properties with the same key
112 		if (includeSystemProperties) {
113 			properties.putAll(System.getProperties());
114 		}
115 
116 		List<String> includeList = getList(includes, include);
117 		List<String> excludeList = getList(excludes, exclude);
118 
119 		override(properties, includeList);
120 
121 		// Resolve placeholders
122 		if (resolvePlaceholders) {
123 			properties = getResolvedProperties(properties);
124 		}
125 
126 		// Remove properties as appropriate
127 		PropertyUtils.trim(properties, includeList, excludeList);
128 
129 		getLog().info("Creating " + outputFile);
130 
131 		// Save the properties to a file
132 		writeProperties(this.outputFile, properties, this.outputStyle, this.prefix);
133 	}
134 
135 	protected void override(Properties properties, List<String> includes) {
136 		List<String> keys = getKeys(properties, includes);
137 		Properties global = PropertyUtils.getGlobalProperties(properties);
138 		properties.clear();
139 		for (String key : keys) {
140 			String value = global.getProperty(key);
141 			if (value != null) {
142 				properties.setProperty(key, value);
143 			}
144 		}
145 	}
146 
147 	protected List<String> getKeys(Properties properties, List<String> keys) {
148 		List<String> newKeys = PropertyUtils.getSortedKeys(properties);
149 		for (String key : keys) {
150 			if (!newKeys.contains(key)) {
151 				newKeys.add(key);
152 			}
153 		}
154 		return newKeys;
155 	}
156 
157 	protected Properties getResolvedProperties(Properties props) {
158 		PropertyPlaceholderHelper pph = Constants.DEFAULT_PROPERTY_PLACEHOLDER_HELPER;
159 		List<String> keys = PropertyUtils.getSortedKeys(props);
160 		Properties newProps = new Properties();
161 		for (String key : keys) {
162 			String originalValue = props.getProperty(key);
163 			String resolvedValue = pph.replacePlaceholders(originalValue, props);
164 			newProps.setProperty(key, resolvedValue);
165 		}
166 		return newProps;
167 
168 	}
169 
170 	protected List<String> getList(List<String> list, String csv) {
171 		List<String> newList = new ArrayList<String>();
172 		if (!CollectionUtils.isEmpty(list)) {
173 			newList.addAll(list);
174 		}
175 		List<String> csvList = CollectionUtils.getTrimmedListFromCSV(csv);
176 		for (String element : csvList) {
177 			if (!newList.contains(element)) {
178 				newList.add(element);
179 			}
180 		}
181 		return newList;
182 	}
183 
184 	public boolean isIncludeSystemProperties() {
185 		return includeSystemProperties;
186 	}
187 
188 	public void setIncludeSystemProperties(boolean includeSystemProperties) {
189 		this.includeSystemProperties = includeSystemProperties;
190 	}
191 
192 	public boolean isIncludeEnvironmentVariables() {
193 		return includeEnvironmentVariables;
194 	}
195 
196 	public void setIncludeEnvironmentVariables(boolean includeEnvironmentVariables) {
197 		this.includeEnvironmentVariables = includeEnvironmentVariables;
198 	}
199 
200 	public String getExclude() {
201 		return exclude;
202 	}
203 
204 	public void setExclude(String exclude) {
205 		this.exclude = exclude;
206 	}
207 
208 	public String getInclude() {
209 		return include;
210 	}
211 
212 	public void setInclude(String include) {
213 		this.include = include;
214 	}
215 
216 	public boolean isResolvePlaceholders() {
217 		return resolvePlaceholders;
218 	}
219 
220 	public void setResolvePlaceholders(boolean resolvePlaceholders) {
221 		this.resolvePlaceholders = resolvePlaceholders;
222 	}
223 
224 	public boolean isIncludeStandardMavenProperties() {
225 		return includeStandardMavenProperties;
226 	}
227 
228 	public void setIncludeStandardMavenProperties(boolean includeStandardMavenProperties) {
229 		this.includeStandardMavenProperties = includeStandardMavenProperties;
230 	}
231 
232 	public List<String> getIncludes() {
233 		return includes;
234 	}
235 
236 	public void setIncludes(List<String> includes) {
237 		this.includes = includes;
238 	}
239 
240 }