1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.codehaus.mojo.properties;
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 import java.io.File;
32 import java.io.FileInputStream;
33 import java.io.IOException;
34 import java.io.InputStream;
35 import java.util.Enumeration;
36 import java.util.Properties;
37
38 import org.apache.commons.io.IOUtils;
39 import org.apache.maven.plugin.AbstractMojo;
40 import org.apache.maven.plugin.MojoExecutionException;
41 import org.apache.maven.project.MavenProject;
42 import org.codehaus.plexus.util.cli.CommandLineUtils;
43
44
45
46
47
48
49
50
51
52
53 public class ReadPropertiesMojo extends AbstractMojo {
54
55
56
57
58
59
60 private MavenProject project;
61
62
63
64
65
66
67
68 private File[] files;
69
70
71
72
73
74
75 private boolean quiet;
76
77 public void execute() throws MojoExecutionException {
78 Properties projectProperties = project.getProperties();
79 for (int i = 0; i < files.length; i++) {
80 File file = files[i];
81 if (validate(file)) {
82 load(file, projectProperties);
83 }
84 }
85
86 boolean useEnvVariables = false;
87 for (Enumeration n = projectProperties.propertyNames(); n.hasMoreElements();) {
88 String k = (String) n.nextElement();
89 String p = (String) projectProperties.get(k);
90 if (p.indexOf("${env.") != -1) {
91 useEnvVariables = true;
92 break;
93 }
94 }
95 Properties environment = null;
96 if (useEnvVariables) {
97 try {
98 environment = CommandLineUtils.getSystemEnvVars();
99 } catch (IOException e) {
100 throw new MojoExecutionException("Error getting system envorinment variables: ", e);
101 }
102 }
103 for (Enumeration n = projectProperties.propertyNames(); n.hasMoreElements();) {
104 String k = (String) n.nextElement();
105 projectProperties.setProperty(k, getPropertyValue(k, projectProperties, environment));
106 }
107 }
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126 protected String getPropertyValue(String k, Properties p, Properties environment) {
127 String v = p.getProperty(k);
128 String ret = "";
129 int idx, idx2;
130
131 while ((idx = v.indexOf("${")) >= 0) {
132
133 ret += v.substring(0, idx);
134
135
136 v = v.substring(idx + 2);
137
138 idx2 = v.indexOf("}");
139
140
141 if (idx2 < 0) {
142 break;
143 }
144
145
146
147 String nk = v.substring(0, idx2);
148 v = v.substring(idx2 + 1);
149 String nv = p.getProperty(nk);
150
151
152 if (nv == null) {
153 nv = System.getProperty(nk);
154 }
155
156
157 if (nv == null && nk.startsWith("env.") && environment != null) {
158 nv = environment.getProperty(nk.substring(4));
159 }
160
161
162
163
164
165
166 if (nv == null || nv.equals(nk)) {
167 ret += "${" + nk + "}";
168 } else {
169 v = nv + v;
170 }
171 }
172 return ret + v;
173 }
174
175 protected boolean validate(File file) throws MojoExecutionException {
176 boolean exists = file != null && file.exists();
177 if (exists) {
178 return true;
179 }
180 if (quiet) {
181 getLog().info("Ignoring non-existent properties file '" + file + "'");
182 return false;
183 } else {
184 throw new MojoExecutionException("Non-existent properties file '" + file + "'");
185 }
186 }
187
188 protected void load(File file, Properties properties) throws MojoExecutionException {
189 InputStream in = null;
190 try {
191 getLog().info("Loading " + file);
192 in = new FileInputStream(file);
193 String filename = file.getName().toLowerCase();
194 if (filename.endsWith(".xml")) {
195 properties.loadFromXML(in);
196 } else {
197 properties.load(in);
198 }
199 } catch (IOException e) {
200 throw new MojoExecutionException("Error reading properties file " + file.getAbsolutePath(), e);
201 } finally {
202 IOUtils.closeQuietly(in);
203 }
204 }
205
206 }