001 /**
002 * Copyright 2009-2012 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.codehaus.mojo.properties;
017
018 import java.util.List;
019 import java.util.Properties;
020
021 import org.apache.maven.plugin.MojoExecutionException;
022 import org.apache.maven.plugin.MojoFailureException;
023 import org.kuali.common.util.CollectionUtils;
024 import org.kuali.common.util.PropertyUtils;
025
026 /**
027 * Write project properties to a file.
028 *
029 * @author Jeff Caddel
030 *
031 * @goal write-project-properties
032 */
033 public class WriteProjectProperties extends AbstractWritePropertiesMojo {
034
035 /**
036 * If true, the plugin will include system properties when writing the properties file. System properties override both environment
037 * variables and project properties.
038 *
039 * @parameter default-value="false" expression="${properties.includeSystemProperties}"
040 */
041 private boolean includeSystemProperties;
042
043 /**
044 * If true, the plugin will include environment variables when writing the properties file. Environment variables are prefixed with
045 * "env". Environment variables override project properties.
046 *
047 * @parameter default-value="false" expression="${properties.includeEnvironmentVariables}"
048 */
049 private boolean includeEnvironmentVariables;
050
051 /**
052 * CSV list of properties to exclude. A single wildcard character <code>*</code> is supported.
053 *
054 * @parameter expression="${properties.exclude}"
055 */
056 private String exclude;
057
058 /**
059 * CSV list of properties to include. A single wildcard character <code>*</code> is supported.
060 *
061 * @parameter expression="${properties.include}"
062 */
063 private String include;
064
065 /**
066 * List of properties to include. A single wildcard character <code>*</code> is supported.
067 *
068 * @parameter
069 */
070 private List<String> includes;
071
072 /**
073 * List of properties to exclude. A single wildcard character <code>*</code> is supported.
074 *
075 * @parameter
076 */
077 private List<String> excludes;
078
079 /**
080 * If true placeholders are resolved before writing properties to the file
081 *
082 * @parameter expression="${properties.resolvePlaceholders}" default-value="false"
083 */
084 private boolean resolvePlaceholders;
085
086 /**
087 * If true, these Maven properties are added to the properties file:
088 *
089 * <pre>
090 * project.groupId
091 * project.artifactId
092 * project.version
093 * project.basedir
094 * project.build.directory
095 * </pre>
096 *
097 * @parameter expression="${properties.includeStandardMavenProperties}" default-value="false"
098 */
099 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 }