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.io.File;
19 import java.io.FileInputStream;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.util.ArrayList;
23 import java.util.List;
24 import java.util.Properties;
25 import java.util.Set;
26
27 import org.apache.commons.io.IOUtils;
28 import org.apache.commons.lang.StringUtils;
29 import org.apache.maven.plugin.AbstractMojo;
30 import org.apache.maven.plugin.MojoExecutionException;
31 import org.apache.maven.project.MavenProject;
32 import org.codehaus.plexus.util.cli.CommandLineUtils;
33 import org.springframework.core.io.DefaultResourceLoader;
34 import org.springframework.core.io.Resource;
35 import org.springframework.core.io.ResourceLoader;
36
37
38
39
40
41
42
43
44
45
46 public class ReadPropertiesMojo extends AbstractMojo {
47
48
49
50
51
52
53 private MavenProject project;
54
55
56
57
58
59
60
61
62 private String[] locations;
63
64
65
66
67
68
69 private boolean quiet;
70
71
72
73
74
75
76 private boolean silent;
77
78
79
80
81
82
83 private boolean verbose;
84
85
86
87
88
89
90 private String ignore;
91
92 @Override
93 public void execute() throws MojoExecutionException {
94 List<String> ignoreList = getListFromCSV(ignore);
95 Properties projectProperties = project.getProperties();
96 if (!silent && verbose && !StringUtils.isBlank(ignore)) {
97 getLog().info("Ignoring " + ignore);
98 }
99 for (int i = 0; i < locations.length; i++) {
100 String location = locations[i];
101 if (!validate(location)) {
102 continue;
103 }
104 if (!silent) {
105 getLog().info("Loading " + location);
106 }
107 Properties p = getProperties(location);
108 updateProperties(projectProperties, p, ignoreList);
109 }
110
111 Properties env = getEnvironment();
112 for (String name : projectProperties.stringPropertyNames()) {
113 String value = getPropertyValue(name, projectProperties, env);
114 projectProperties.setProperty(name, value);
115 }
116 }
117
118 protected Properties getEnvironment() throws MojoExecutionException {
119 try {
120 return CommandLineUtils.getSystemEnvVars();
121 } catch (IOException e) {
122 throw new MojoExecutionException("Error get environment variables", e);
123 }
124 }
125
126 protected void updateProperties(Properties p1, Properties p2, List<String> ignore) {
127 Set<String> names = p2.stringPropertyNames();
128 for (String name : names) {
129 if (!ignore.contains(name)) {
130 String value = p2.getProperty(name);
131 p1.setProperty(name, value);
132 }
133 }
134 }
135
136 protected static final List<String> getListFromCSV(String csv) {
137 if (StringUtils.isBlank(csv)) {
138 return new ArrayList<String>();
139 }
140 List<String> list = new ArrayList<String>();
141 String[] tokens = StringUtils.split(csv, ",");
142 for (String token : tokens) {
143 list.add(token.trim());
144 }
145 return list;
146 }
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165 protected String getPropertyValue(String k, Properties p, Properties environment) {
166 String v = p.getProperty(k);
167 String ret = "";
168 int idx, idx2;
169
170 while ((idx = v.indexOf("${")) >= 0) {
171
172 ret += v.substring(0, idx);
173
174
175 v = v.substring(idx + 2);
176
177 idx2 = v.indexOf("}");
178
179
180 if (idx2 < 0) {
181 break;
182 }
183
184
185
186 String nk = v.substring(0, idx2);
187 v = v.substring(idx2 + 1);
188 String nv = p.getProperty(nk);
189
190
191 if (nv == null) {
192 nv = System.getProperty(nk);
193 }
194
195
196 if (nv == null && nk.startsWith("env.") && environment != null) {
197 nv = environment.getProperty(nk.substring(4));
198 }
199
200
201
202
203
204
205 if (nv == null || nv.equals(nk)) {
206 ret += "${" + nk + "}";
207 } else {
208 v = nv + v;
209 }
210 }
211 return ret + v;
212 }
213
214 protected String toEmpty(String s) {
215 if (StringUtils.isBlank(s)) {
216 return "";
217 } else {
218 return s;
219 }
220 }
221
222 protected boolean exists(String location) {
223 if (StringUtils.isBlank(location)) {
224 return false;
225 }
226 File file = new File(location);
227 if (file.exists()) {
228 return true;
229 }
230 ResourceLoader loader = new DefaultResourceLoader();
231 Resource resource = loader.getResource(location);
232 return resource.exists();
233 }
234
235 protected boolean validate(String location) throws MojoExecutionException {
236 boolean exists = exists(location);
237 if (exists) {
238 return true;
239 }
240 if (quiet) {
241 if (verbose && !silent) {
242 getLog().info("Ignoring non-existent properties file '" + toEmpty(location) + "'");
243 }
244 return false;
245 } else {
246 throw new MojoExecutionException("Non-existent properties file '" + location + "'");
247 }
248 }
249
250 protected InputStream getInputStream(String location) throws IOException {
251 File file = new File(location);
252 if (file.exists()) {
253 return new FileInputStream(location);
254 }
255 ResourceLoader loader = new DefaultResourceLoader();
256 Resource resource = loader.getResource(location);
257 return resource.getInputStream();
258 }
259
260 protected Properties getProperties(String location) throws MojoExecutionException {
261 InputStream in = null;
262 try {
263 Properties properties = new Properties();
264 in = getInputStream(location);
265 if (location.toLowerCase().endsWith(".xml")) {
266 properties.loadFromXML(in);
267 } else {
268 properties.load(in);
269 }
270 return properties;
271 } catch (IOException e) {
272 throw new MojoExecutionException("Error reading properties file " + location, e);
273 } finally {
274 IOUtils.closeQuietly(in);
275 }
276 }
277
278 public boolean isQuiet() {
279 return quiet;
280 }
281
282 public void setQuiet(boolean quiet) {
283 this.quiet = quiet;
284 }
285
286 public String getIgnore() {
287 return ignore;
288 }
289
290 public void setIgnore(String ignoreProperties) {
291 this.ignore = ignoreProperties;
292 }
293
294 public MavenProject getProject() {
295 return project;
296 }
297
298 public String[] getLocations() {
299 return locations;
300 }
301
302 public void setLocations(String[] locations) {
303 this.locations = locations;
304 }
305
306 public boolean isVerbose() {
307 return verbose;
308 }
309
310 public void setVerbose(boolean verbose) {
311 this.verbose = verbose;
312 }
313
314 public boolean isSilent() {
315 return silent;
316 }
317
318 public void setSilent(boolean silent) {
319 this.silent = silent;
320 }
321
322 }