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  		// Resolve placeholders
92  		if (resolvePlaceholders) {
93  			properties = getResolvedProperties(properties);
94  		}
95  
96  		// Remove properties as appropriate
97  		trim(properties, exclude, include);
98  
99  		getLog().info("Creating " + outputFile);
100 		writeProperties(this.outputFile, properties, this.outputStyle);
101 	}
102 
103 	protected Properties getResolvedProperties(Properties props) {
104 		PropertyPlaceholderHelper pph = new PropertyPlaceholderHelper("${", "}");
105 		List<String> keys = new ArrayList<String>(props.stringPropertyNames());
106 		Collections.sort(keys);
107 		Properties newProps = new Properties();
108 		for (String key : keys) {
109 			String originalValue = props.getProperty(key);
110 			String resolvedValue = pph.replacePlaceholders(originalValue, props);
111 			newProps.setProperty(key, resolvedValue);
112 		}
113 		return newProps;
114 
115 	}
116 
117 	protected static Properties getEnvironmentVariables() {
118 		String prefix = "env";
119 		Map<String, String> map = System.getenv();
120 		Properties props = new Properties();
121 		for (String key : map.keySet()) {
122 			String newKey = prefix + "." + key;
123 			String value = map.get(key);
124 			props.setProperty(newKey, value);
125 		}
126 		return props;
127 	}
128 
129 	protected void trim(Properties properties, String excludeCSV, String includeCSV) {
130 		List<String> omitKeys = ReadPropertiesMojo.getListFromCSV(excludeCSV);
131 		for (String key : omitKeys) {
132 			properties.remove(key);
133 		}
134 		if (StringUtils.isBlank(includeCSV)) {
135 			return;
136 		}
137 		List<String> includeKeys = ReadPropertiesMojo.getListFromCSV(includeCSV);
138 		Set<String> keys = properties.stringPropertyNames();
139 		for (String key : keys) {
140 			if (!includeKeys.contains(key)) {
141 				properties.remove(key);
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 }