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  
24  import org.apache.maven.plugin.MojoExecutionException;
25  import org.apache.maven.plugin.MojoFailureException;
26  import org.springframework.util.PropertyPlaceholderHelper;
27  
28  /**
29   * Write project properties to a file.
30   *
31   * @author Jeff Caddel
32   *
33   * @goal write-project-properties
34   */
35  public class WriteProjectProperties extends AbstractWritePropertiesMojo {
36  
37  	/**
38  	 * If true, the plugin will include system properties when writing the properties file. System properties override both environment
39  	 * variables and project properties.
40  	 *
41  	 * @parameter default-value="false" expression="${properties.includeSystemProperties}"
42  	 */
43  	private boolean includeSystemProperties;
44  
45  	/**
46  	 * If true, the plugin will include environment variables when writing the properties file. Environment variables are prefixed with
47  	 * "env". Environment variables override project properties.
48  	 *
49  	 * @parameter default-value="false" expression="${properties.includeEnvironmentVariables}"
50  	 */
51  	private boolean includeEnvironmentVariables;
52  
53  	/**
54  	 * Comma separated set of properties to exclude when writing the properties file
55  	 *
56  	 * @parameter expression="${properties.exclude}"
57  	 */
58  	private String exclude;
59  
60  	/**
61  	 * Comma separated set of properties to write to the properties file. If provided, only the properties matching those supplied here will
62  	 * be written to the properties file.
63  	 *
64  	 * @parameter expression="${properties.include}"
65  	 */
66  	private String include;
67  
68  	/**
69  	 * If true placeholders are resolved before writing properties to the file
70  	 *
71  	 * @parameter expression="${properties.resolvePlaceholders}"
72  	 */
73  	private boolean resolvePlaceholders;
74  
75  	@Override
76  	public void execute() throws MojoExecutionException, MojoFailureException {
77  		Properties properties = new Properties();
78  		// Add project properties
79  		properties.putAll(project.getProperties());
80  		if (includeEnvironmentVariables) {
81  			// Add environment variables, overriding any existing properties with the same key
82  			properties.putAll(getEnvironmentVariables());
83  		}
84  		if (includeSystemProperties) {
85  			// Add system properties, overriding any existing properties with the same key
86  			properties.putAll(System.getProperties());
87  		}
88  
89  		// Resolve placeholders
90  		if (resolvePlaceholders) {
91  			properties = getResolvedProperties(properties);
92  		}
93  
94  		// Remove properties as appropriate
95  		trim(properties, exclude, include);
96  
97  		getLog().info("Creating " + outputFile);
98  
99  		// Save the properties to a file
100 		writeProperties(this.outputFile, properties, this.outputStyle, this.prefix);
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> excludes = ReadPropertiesMojo.getListFromCSV(excludeCSV);
131 		List<String> includes = ReadPropertiesMojo.getListFromCSV(includeCSV);
132 		List<String> keys = new ArrayList<String>(properties.stringPropertyNames());
133 		Collections.sort(keys);
134 		for (String key : keys) {
135 			boolean include = include(key, includes, excludes);
136 			if (!include) {
137 				properties.remove(key);
138 			}
139 		}
140 	}
141 
142 	public boolean include(String value, List<String> includes, List<String> excludes) {
143 		if (excludes.contains(value)) {
144 			return false;
145 		} else {
146 			return includes.size() == 0 || includes.contains(value);
147 		}
148 	}
149 
150 	public boolean isIncludeSystemProperties() {
151 		return includeSystemProperties;
152 	}
153 
154 	public void setIncludeSystemProperties(boolean includeSystemProperties) {
155 		this.includeSystemProperties = includeSystemProperties;
156 	}
157 
158 	public boolean isIncludeEnvironmentVariables() {
159 		return includeEnvironmentVariables;
160 	}
161 
162 	public void setIncludeEnvironmentVariables(boolean includeEnvironmentVariables) {
163 		this.includeEnvironmentVariables = includeEnvironmentVariables;
164 	}
165 
166 	public String getExclude() {
167 		return exclude;
168 	}
169 
170 	public void setExclude(String exclude) {
171 		this.exclude = exclude;
172 	}
173 
174 	public String getInclude() {
175 		return include;
176 	}
177 
178 	public void setInclude(String include) {
179 		this.include = include;
180 	}
181 
182 	public boolean isResolvePlaceholders() {
183 		return resolvePlaceholders;
184 	}
185 
186 	public void setResolvePlaceholders(boolean resolvePlaceholders) {
187 		this.resolvePlaceholders = resolvePlaceholders;
188 	}
189 
190 }