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