1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
31
32
33
34
35
36 public class WriteProjectProperties extends AbstractWritePropertiesMojo {
37
38
39
40
41
42
43
44 private boolean includeSystemProperties;
45
46
47
48
49
50
51
52 private boolean includeEnvironmentVariables;
53
54
55
56
57
58
59 private String exclude;
60
61
62
63
64
65
66
67 private String include;
68
69
70
71
72
73
74 private List<String> includes;
75
76
77
78
79
80
81 private List<String> excludes;
82
83
84
85
86
87
88 private boolean resolvePlaceholders;
89
90
91
92
93 private boolean includeStandardMavenProperties;
94
95 @Override
96 public void execute() throws MojoExecutionException, MojoFailureException {
97 Properties properties = new Properties();
98
99
100 properties.putAll(project.getProperties());
101
102 if (includeStandardMavenProperties) {
103 properties.putAll(getStandardMavenProperties(project));
104 }
105
106
107 if (includeEnvironmentVariables) {
108 properties.putAll(PropertyUtils.getEnvAsProperties());
109 }
110
111
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
122 if (resolvePlaceholders) {
123 properties = getResolvedProperties(properties);
124 }
125
126
127 PropertyUtils.trim(properties, includeList, excludeList);
128
129 getLog().info("Creating " + outputFile);
130
131
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 }