View Javadoc

1   /**
2    * Copyright 2009-2013 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.List;
19  import java.util.Properties;
20  
21  import org.apache.maven.plugin.MojoExecutionException;
22  import org.apache.maven.plugin.MojoFailureException;
23  import org.kuali.common.util.CollectionUtils;
24  import org.kuali.common.util.PropertyUtils;
25  
26  /**
27   * Write project properties to a file.
28   *
29   * @author Jeff Caddel
30   *
31   * @goal write-project-properties
32   */
33  public class WriteProjectProperties extends AbstractWritePropertiesMojo {
34  
35  	/**
36  	 * If true, the plugin will include system properties when writing the properties file. System properties override both environment
37  	 * variables and project properties.
38  	 *
39  	 * @parameter default-value="false" expression="${properties.includeSystemProperties}"
40  	 */
41  	private boolean includeSystemProperties;
42  
43  	/**
44  	 * If true, the plugin will include environment variables when writing the properties file. Environment variables are prefixed with
45  	 * "env". Environment variables override project properties.
46  	 *
47  	 * @parameter default-value="false" expression="${properties.includeEnvironmentVariables}"
48  	 */
49  	private boolean includeEnvironmentVariables;
50  
51  	/**
52  	 * CSV list of properties to exclude. A single wildcard character <code>*</code> is supported.
53  	 *
54  	 * @parameter expression="${properties.exclude}"
55  	 */
56  	private String exclude;
57  
58  	/**
59  	 * CSV list of properties to include. A single wildcard character <code>*</code> is supported.
60  	 *
61  	 * @parameter expression="${properties.include}"
62  	 */
63  	private String include;
64  
65  	/**
66  	 * List of properties to include. A single wildcard character <code>*</code> is supported.
67  	 *
68  	 * @parameter
69  	 */
70  	private List<String> includes;
71  
72  	/**
73  	 * List of properties to exclude. A single wildcard character <code>*</code> is supported.
74  	 *
75  	 * @parameter
76  	 */
77  	private List<String> excludes;
78  
79  	/**
80  	 * If true placeholders are resolved before writing properties to the file
81  	 *
82  	 * @parameter expression="${properties.resolvePlaceholders}" default-value="false"
83  	 */
84  	private boolean resolvePlaceholders;
85  
86  	/**
87  	 * If true, these Maven properties are added to the properties file:
88  	 *
89  	 * <pre>
90  	 *   project.groupId
91  	 *   project.artifactId
92  	 *   project.version
93  	 *   project.basedir
94  	 *   project.build.directory
95  	 * </pre>
96  	 *
97  	 * @parameter expression="${properties.includeStandardMavenProperties}" default-value="false"
98  	 */
99  	private boolean includeStandardMavenProperties;
100 
101 	@Override
102 	public void execute() throws MojoExecutionException, MojoFailureException {
103 		Properties properties = new Properties();
104 
105 		// Add project properties
106 		properties.putAll(project.getProperties());
107 
108 		if (includeStandardMavenProperties) {
109 			properties.putAll(MavenUtils.getInternalMavenProperties(project));
110 		}
111 
112 		// Add environment variables, overriding any existing properties with the same key
113 		if (includeEnvironmentVariables) {
114 			properties.putAll(PropertyUtils.getEnvAsProperties());
115 		}
116 
117 		// Add system properties, overriding any existing properties with the same key
118 		if (includeSystemProperties) {
119 			properties.putAll(System.getProperties());
120 		}
121 
122 		// Merge the lists with the csv values
123 		List<String> includeList = CollectionUtils.sortedMerge(includes, include);
124 		List<String> excludeList = CollectionUtils.sortedMerge(excludes, exclude);
125 
126 		// Override any properties from the includes list with their equivalent system/env value
127 		override(properties, includeList);
128 
129 		// Resolve placeholders
130 		if (resolvePlaceholders) {
131 			Properties resolved = PropertyUtils.getResolvedProperties(properties);
132 			getLog().info("Resolved " + resolved.size() + " properties");
133 			properties.putAll(resolved);
134 		}
135 
136 		// Remove properties as appropriate
137 		PropertyUtils.trim(properties, includeList, excludeList);
138 
139 		// Save the properties to a file
140 		writeProperties(outputFile, properties, outputStyle, prefix, encoding, comment);
141 	}
142 
143 	protected void override(Properties properties, List<String> includes) {
144 		List<String> keys = PropertyUtils.getSortedKeys(properties, includes, null);
145 		Properties global = PropertyUtils.getGlobalProperties(properties);
146 		properties.clear();
147 		for (String key : keys) {
148 			String value = global.getProperty(key);
149 			if (value != null) {
150 				properties.setProperty(key, value);
151 			}
152 		}
153 	}
154 
155 	public boolean isIncludeSystemProperties() {
156 		return includeSystemProperties;
157 	}
158 
159 	public void setIncludeSystemProperties(boolean includeSystemProperties) {
160 		this.includeSystemProperties = includeSystemProperties;
161 	}
162 
163 	public boolean isIncludeEnvironmentVariables() {
164 		return includeEnvironmentVariables;
165 	}
166 
167 	public void setIncludeEnvironmentVariables(boolean includeEnvironmentVariables) {
168 		this.includeEnvironmentVariables = includeEnvironmentVariables;
169 	}
170 
171 	public String getExclude() {
172 		return exclude;
173 	}
174 
175 	public void setExclude(String exclude) {
176 		this.exclude = exclude;
177 	}
178 
179 	public String getInclude() {
180 		return include;
181 	}
182 
183 	public void setInclude(String include) {
184 		this.include = include;
185 	}
186 
187 	public boolean isResolvePlaceholders() {
188 		return resolvePlaceholders;
189 	}
190 
191 	public void setResolvePlaceholders(boolean resolvePlaceholders) {
192 		this.resolvePlaceholders = resolvePlaceholders;
193 	}
194 
195 	public boolean isIncludeStandardMavenProperties() {
196 		return includeStandardMavenProperties;
197 	}
198 
199 	public void setIncludeStandardMavenProperties(boolean includeStandardMavenProperties) {
200 		this.includeStandardMavenProperties = includeStandardMavenProperties;
201 	}
202 
203 	public List<String> getIncludes() {
204 		return includes;
205 	}
206 
207 	public void setIncludes(List<String> includes) {
208 		this.includes = includes;
209 	}
210 
211 	public List<String> getExcludes() {
212 		return excludes;
213 	}
214 
215 	public void setExcludes(List<String> excludes) {
216 		this.excludes = excludes;
217 	}
218 
219 }