|
1 |
/**
|
|
2 |
*
|
|
3 |
* Licensed under the Educational Community License, Version 2.0 (the "License");
|
|
4 |
* you may not use this file except in compliance with the License.
|
|
5 |
* You may obtain a copy of the License at
|
|
6 |
*
|
|
7 |
* http://www.opensource.org/licenses/ecl2.php
|
|
8 |
*
|
|
9 |
* Unless required by applicable law or agreed to in writing, software
|
|
10 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
11 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12 |
* See the License for the specific language governing permissions and
|
|
13 |
* limitations under the License.
|
|
14 |
*/
|
|
15 |
package eu.dnetlib.maven.plugin.properties;
|
|
16 |
|
|
17 |
import java.io.File;
|
|
18 |
import java.io.FileInputStream;
|
|
19 |
import java.io.IOException;
|
|
20 |
import java.io.InputStream;
|
|
21 |
import java.io.OutputStream;
|
|
22 |
import java.text.SimpleDateFormat;
|
|
23 |
import java.util.ArrayList;
|
|
24 |
import java.util.Arrays;
|
|
25 |
import java.util.Collections;
|
|
26 |
import java.util.Date;
|
|
27 |
import java.util.List;
|
|
28 |
import java.util.Map;
|
|
29 |
import java.util.Properties;
|
|
30 |
import java.util.Set;
|
|
31 |
|
|
32 |
import org.apache.commons.io.FileUtils;
|
|
33 |
import org.apache.commons.io.IOUtils;
|
|
34 |
import org.apache.commons.lang.StringUtils;
|
|
35 |
import org.apache.maven.plugin.AbstractMojo;
|
|
36 |
import org.apache.maven.plugin.MojoExecutionException;
|
|
37 |
import org.apache.maven.plugin.MojoFailureException;
|
|
38 |
import org.apache.maven.project.MavenProject;
|
|
39 |
import org.springframework.core.io.DefaultResourceLoader;
|
|
40 |
import org.springframework.core.io.Resource;
|
|
41 |
import org.springframework.core.io.ResourceLoader;
|
|
42 |
|
|
43 |
/**
|
|
44 |
* Writes project properties for the keys listed in specified properties files.
|
|
45 |
* Based on:
|
|
46 |
* http://site.kuali.org/maven/plugins/properties-maven-plugin/1.3.2/write-project-properties-mojo.html
|
|
47 |
|
|
48 |
* @author mhorst
|
|
49 |
* @goal write-project-properties
|
|
50 |
*/
|
|
51 |
public class WritePredefinedProjectProperties extends AbstractMojo {
|
|
52 |
|
|
53 |
private static final String CR = "\r";
|
|
54 |
private static final String LF = "\n";
|
|
55 |
private static final String TAB = "\t";
|
|
56 |
private static final String[] ANT_ESCAPE_CHARS = { CR, LF, TAB, ":", "#", "=" };
|
|
57 |
|
|
58 |
/**
|
|
59 |
* @parameter expression="${properties.includePropertyKeysFromFiles}"
|
|
60 |
*/
|
|
61 |
private String[] includePropertyKeysFromFiles;
|
|
62 |
|
|
63 |
/**
|
|
64 |
* @parameter default-value="${project}"
|
|
65 |
* @required
|
|
66 |
* @readonly
|
|
67 |
*/
|
|
68 |
private MavenProject project;
|
|
69 |
|
|
70 |
/**
|
|
71 |
* The file that properties will be written to
|
|
72 |
*
|
|
73 |
* @parameter expression="${properties.outputFile}"
|
|
74 |
* default-value="${project.build.directory}/properties/project.properties";
|
|
75 |
* @required
|
|
76 |
*/
|
|
77 |
private File outputFile;
|
|
78 |
|
|
79 |
/**
|
|
80 |
* If true, the plugin will silently ignore any non-existent properties files, and the build will continue
|
|
81 |
*
|
|
82 |
* @parameter expression="${properties.quiet}" default-value="true"
|
|
83 |
*/
|
|
84 |
private boolean quiet;
|
|
85 |
|
|
86 |
/**
|
|
87 |
* Comma separated list of characters to escape when writing property values. cr=carriage return, lf=linefeed,
|
|
88 |
* tab=tab. Any other values are taken literally.
|
|
89 |
*
|
|
90 |
* @parameter default-value="cr,lf,tab" expression="${properties.escapeChars}"
|
|
91 |
*/
|
|
92 |
private String escapeChars;
|
|
93 |
|
|
94 |
/**
|
|
95 |
* If true, the plugin will create the properties file formatted the same way Ant formats properties files using the
|
|
96 |
* <code>echoproperties</code> task. This mode adds 3 custom properties at the top of the file, DSTAMP, TODAY, and
|
|
97 |
* TSTAMP. In this mode <code>escapeChars</code> is ignored and the 6 characters Ant escapes are used instead
|
|
98 |
* <code>CR</code>,<code>LF</code>,<code>TAB</code>,<code>:</code>,<code>#</code>,<code>=</code>
|
|
99 |
*
|
|
100 |
* @parameter default-value="false" expression="${properties.antEchoPropertiesMode}"
|
|
101 |
*/
|
|
102 |
private boolean antEchoPropertiesMode;
|
|
103 |
|
|
104 |
/**
|
|
105 |
* If true, the plugin will include system properties when writing the properties file. System properties override
|
|
106 |
* both environment variables and project properties.
|
|
107 |
*
|
|
108 |
* @parameter default-value="false" expression="${properties.includeSystemProperties}"
|
|
109 |
*/
|
|
110 |
private boolean includeSystemProperties;
|
|
111 |
|
|
112 |
/**
|
|
113 |
* If true, the plugin will include environment variables when writing the properties file. Environment variables
|
|
114 |
* are prefixed with "env". Environment variables override project properties.
|
|
115 |
*
|
|
116 |
* @parameter default-value="false" expression="${properties.includeEnvironmentVariables}"
|
|
117 |
*/
|
|
118 |
private boolean includeEnvironmentVariables;
|
|
119 |
|
|
120 |
/**
|
|
121 |
* Comma separated set of properties to exclude when writing the properties file
|
|
122 |
*
|
|
123 |
* @parameter expression="${properties.exclude}"
|
|
124 |
*/
|
|
125 |
private String exclude;
|
|
126 |
|
|
127 |
/**
|
|
128 |
* Comma separated set of properties to write to the properties file. If provided, only the properties matching
|
|
129 |
* those supplied here will be written to the properties file.
|
|
130 |
*
|
|
131 |
* @parameter expression="${properties.include}"
|
|
132 |
*/
|
|
133 |
private String include;
|
|
134 |
|
|
135 |
/* (non-Javadoc)
|
|
136 |
* @see org.apache.maven.plugin.AbstractMojo#execute()
|
|
137 |
*/
|
|
138 |
@Override
|
|
139 |
public void execute() throws MojoExecutionException, MojoFailureException {
|
|
140 |
Properties properties = new Properties();
|
|
141 |
// Add project properties
|
|
142 |
properties.putAll(project.getProperties());
|
|
143 |
if (includeEnvironmentVariables) {
|
|
144 |
// Add environment variables, overriding any existing properties with the same key
|
|
145 |
properties.putAll(getEnvironmentVariables());
|
|
146 |
}
|
|
147 |
if (includeSystemProperties) {
|
|
148 |
// Add system properties, overriding any existing properties with the same key
|
|
149 |
properties.putAll(System.getProperties());
|
|
150 |
}
|
|
151 |
|
|
152 |
// Remove properties as appropriate
|
|
153 |
trim(properties, exclude, include);
|
|
154 |
|
|
155 |
String comment = "# " + new Date() + "\n";
|
|
156 |
List<String> escapeTokens = getEscapeChars(escapeChars);
|
|
157 |
if (antEchoPropertiesMode) {
|
|
158 |
escapeTokens = Arrays.asList(ANT_ESCAPE_CHARS);
|
|
159 |
comment = getAntHeader();
|
|
160 |
properties.remove("DSTAMP");
|
|
161 |
properties.remove("TODAY");
|
|
162 |
properties.remove("TSTAMP");
|
|
163 |
}
|
|
164 |
|
|
165 |
getLog().info("Creating " + outputFile);
|
|
166 |
writeProperties(outputFile, comment, properties, escapeTokens);
|
|
167 |
}
|
|
168 |
|
|
169 |
/**
|
|
170 |
* Provides environment variables.
|
|
171 |
* @return environment variables
|
|
172 |
*/
|
|
173 |
protected static Properties getEnvironmentVariables() {
|
|
174 |
String prefix = "env";
|
|
175 |
Map<String, String> map = System.getenv();
|
|
176 |
Properties props = new Properties();
|
|
177 |
for (String key : map.keySet()) {
|
|
178 |
String newKey = prefix + "." + key;
|
|
179 |
String value = map.get(key);
|
|
180 |
props.setProperty(newKey, value);
|
|
181 |
}
|
|
182 |
return props;
|
|
183 |
}
|
|
184 |
|
|
185 |
/**
|
|
186 |
* Removes properties which should not be written.
|
|
187 |
* @param properties
|
|
188 |
* @param omitCSV
|
|
189 |
* @param includeCSV
|
|
190 |
* @throws MojoExecutionException
|
|
191 |
*/
|
|
192 |
protected void trim(Properties properties, String omitCSV, String includeCSV) throws MojoExecutionException {
|
|
193 |
List<String> omitKeys = getListFromCSV(omitCSV);
|
|
194 |
for (String key : omitKeys) {
|
|
195 |
properties.remove(key);
|
|
196 |
}
|
|
197 |
|
|
198 |
List<String> includeKeys = getListFromCSV(includeCSV);
|
|
199 |
// mh: including keys from predefined properties
|
|
200 |
if (includePropertyKeysFromFiles!=null && includePropertyKeysFromFiles.length>0) {
|
|
201 |
for (String currentIncludeLoc : includePropertyKeysFromFiles) {
|
|
202 |
if (validate(currentIncludeLoc)) {
|
|
203 |
Properties p = getProperties(currentIncludeLoc);
|
|
204 |
for (String key : p.stringPropertyNames()) {
|
|
205 |
includeKeys.add(key);
|
|
206 |
}
|
|
207 |
}
|
|
208 |
}
|
|
209 |
}
|
|
210 |
if (includeKeys!=null && !includeKeys.isEmpty()) {
|
|
211 |
// removing only when include keys provided
|
|
212 |
Set<String> keys = properties.stringPropertyNames();
|
|
213 |
for (String key : keys) {
|
|
214 |
if (!includeKeys.contains(key)) {
|
|
215 |
properties.remove(key);
|
|
216 |
}
|
|
217 |
}
|
|
218 |
}
|
|
219 |
}
|
|
220 |
|
|
221 |
protected String toEmpty(String s) {
|
|
222 |
if (StringUtils.isBlank(s)) {
|
|
223 |
return "";
|
|
224 |
} else {
|
|
225 |
return s;
|
|
226 |
}
|
|
227 |
}
|
|
228 |
|
|
229 |
/**
|
|
230 |
* Checks whether file exists.
|
|
231 |
* @param location
|
|
232 |
* @return true when exists, false otherwise.
|
|
233 |
*/
|
|
234 |
protected boolean exists(String location) {
|
|
235 |
if (StringUtils.isBlank(location)) {
|
|
236 |
return false;
|
|
237 |
}
|
|
238 |
File file = new File(location);
|
|
239 |
if (file.exists()) {
|
|
240 |
return true;
|
|
241 |
}
|
|
242 |
ResourceLoader loader = new DefaultResourceLoader();
|
|
243 |
Resource resource = loader.getResource(location);
|
|
244 |
return resource.exists();
|
|
245 |
}
|
|
246 |
|
|
247 |
/**
|
|
248 |
* Validates resource location.
|
|
249 |
* @param location
|
|
250 |
* @return true when valid, false otherwise
|
|
251 |
* @throws MojoExecutionException
|
|
252 |
*/
|
|
253 |
protected boolean validate(String location) throws MojoExecutionException {
|
|
254 |
boolean exists = exists(location);
|
|
255 |
if (exists) {
|
|
256 |
return true;
|
|
257 |
}
|
|
258 |
if (quiet) {
|
|
259 |
getLog().info("Ignoring non-existent properties file '" + toEmpty(location) + "'");
|
|
260 |
return false;
|
|
261 |
} else {
|
|
262 |
throw new MojoExecutionException("Non-existent properties file '" + location + "'");
|
|
263 |
}
|
|
264 |
}
|
|
265 |
|
|
266 |
/**
|
|
267 |
* Provides input stream.
|
|
268 |
* @param location
|
|
269 |
* @return input stream
|
|
270 |
* @throws IOException
|
|
271 |
*/
|
|
272 |
protected InputStream getInputStream(String location) throws IOException {
|
|
273 |
File file = new File(location);
|
|
274 |
if (file.exists()) {
|
|
275 |
return new FileInputStream(location);
|
|
276 |
}
|
|
277 |
ResourceLoader loader = new DefaultResourceLoader();
|
|
278 |
Resource resource = loader.getResource(location);
|
|
279 |
return resource.getInputStream();
|
|
280 |
}
|
|
281 |
|
|
282 |
/**
|
|
283 |
* Creates properties for given location.
|
|
284 |
* @param location
|
|
285 |
* @return properties for given location
|
|
286 |
* @throws MojoExecutionException
|
|
287 |
*/
|
|
288 |
protected Properties getProperties(String location) throws MojoExecutionException {
|
|
289 |
InputStream in = null;
|
|
290 |
try {
|
|
291 |
Properties properties = new Properties();
|
|
292 |
in = getInputStream(location);
|
|
293 |
if (location.toLowerCase().endsWith(".xml")) {
|
|
294 |
properties.loadFromXML(in);
|
|
295 |
} else {
|
|
296 |
properties.load(in);
|
|
297 |
}
|
|
298 |
return properties;
|
|
299 |
} catch (IOException e) {
|
|
300 |
throw new MojoExecutionException("Error reading properties file " + location, e);
|
|
301 |
} finally {
|
|
302 |
IOUtils.closeQuietly(in);
|
|
303 |
}
|
|
304 |
}
|
|
305 |
|
|
306 |
/**
|
|
307 |
* Provides ant header.
|
|
308 |
* @return ant header
|
|
309 |
*/
|
|
310 |
protected String getAntHeader() {
|
|
311 |
SimpleDateFormat dstamp = new SimpleDateFormat("yyyyMMdd");
|
|
312 |
SimpleDateFormat today = new SimpleDateFormat("MMMM d yyyy");
|
|
313 |
SimpleDateFormat tstamp = new SimpleDateFormat("HHmm");
|
|
314 |
Date now = new Date();
|
|
315 |
StringBuilder sb = new StringBuilder();
|
|
316 |
sb.append("# Ant properties\n");
|
|
317 |
sb.append("# " + now + "\n");
|
|
318 |
sb.append("DSTAMP=" + dstamp.format(now) + "\n");
|
|
319 |
sb.append("TODAY=" + today.format(now) + "\n");
|
|
320 |
sb.append("TSTAMP=" + tstamp.format(now) + "\n");
|
|
321 |
return sb.toString();
|
|
322 |
}
|
|
323 |
|
|
324 |
/**
|
|
325 |
* Provides escape characters.
|
|
326 |
* @param escapeChars
|
|
327 |
* @return escape characters
|
|
328 |
*/
|
|
329 |
protected List<String> getEscapeChars(String escapeChars) {
|
|
330 |
List<String> tokens = getListFromCSV(escapeChars);
|
|
331 |
List<String> realTokens = new ArrayList<String>();
|
|
332 |
for (String token : tokens) {
|
|
333 |
String realToken = getRealToken(token);
|
|
334 |
realTokens.add(realToken);
|
|
335 |
}
|
|
336 |
return realTokens;
|
|
337 |
}
|
|
338 |
|
|
339 |
/**
|
|
340 |
* Provides real token.
|
|
341 |
* @param token
|
|
342 |
* @return real token
|
|
343 |
*/
|
|
344 |
protected String getRealToken(String token) {
|
|
345 |
if (token.equalsIgnoreCase("CR")) {
|
|
346 |
return CR;
|
|
347 |
} else if (token.equalsIgnoreCase("LF")) {
|
|
348 |
return LF;
|
|
349 |
} else if (token.equalsIgnoreCase("TAB")) {
|
|
350 |
return TAB;
|
|
351 |
} else {
|
|
352 |
return token;
|
|
353 |
}
|
|
354 |
}
|
|
355 |
|
|
356 |
/**
|
|
357 |
* Returns content.
|
|
358 |
* @param comment
|
|
359 |
* @param properties
|
|
360 |
* @param escapeTokens
|
|
361 |
* @return content
|
|
362 |
*/
|
|
363 |
protected String getContent(String comment, Properties properties, List<String> escapeTokens) {
|
|
364 |
List<String> names = new ArrayList<String>(properties.stringPropertyNames());
|
|
365 |
Collections.sort(names);
|
|
366 |
StringBuilder sb = new StringBuilder();
|
|
367 |
if (!StringUtils.isBlank(comment)) {
|
|
368 |
sb.append(comment);
|
|
369 |
}
|
|
370 |
for (String name : names) {
|
|
371 |
String value = properties.getProperty(name);
|
|
372 |
String escapedValue = escape(value, escapeTokens);
|
|
373 |
sb.append(name + "=" + escapedValue + "\n");
|
|
374 |
}
|
|
375 |
return sb.toString();
|
|
376 |
}
|
|
377 |
|
|
378 |
/**
|
|
379 |
* Writes properties to given file.
|
|
380 |
* @param file
|
|
381 |
* @param comment
|
|
382 |
* @param properties
|
|
383 |
* @param escapeTokens
|
|
384 |
* @throws MojoExecutionException
|
|
385 |
*/
|
|
386 |
protected void writeProperties(File file, String comment, Properties properties, List<String> escapeTokens)
|
|
387 |
throws MojoExecutionException {
|
|
388 |
try {
|
|
389 |
String content = getContent(comment, properties, escapeTokens);
|
|
390 |
FileUtils.writeStringToFile(file, content);
|
|
391 |
} catch (IOException e) {
|
|
392 |
throw new MojoExecutionException("Error creating properties file", e);
|
|
393 |
}
|
|
394 |
}
|
|
395 |
|
|
396 |
/**
|
|
397 |
* Writes properties.
|
|
398 |
* @param properties
|
|
399 |
* @param file
|
|
400 |
* @throws MojoExecutionException
|
|
401 |
*/
|
|
402 |
protected void writeProperties(Properties properties, File file) throws MojoExecutionException {
|
|
403 |
OutputStream out = null;
|
|
404 |
try {
|
|
405 |
out = FileUtils.openOutputStream(outputFile);
|
|
406 |
properties.store(out, "Properties");
|
|
407 |
} catch (IOException e) {
|
|
408 |
throw new MojoExecutionException("Error creating properties file", e);
|
|
409 |
} finally {
|
|
410 |
IOUtils.closeQuietly(out);
|
|
411 |
}
|
|
412 |
}
|
|
413 |
|
|
414 |
/**
|
|
415 |
* Escapes characters.
|
|
416 |
* @param s
|
|
417 |
* @param escapeChars
|
|
418 |
* @return
|
|
419 |
*/
|
|
420 |
protected String escape(String s, List<String> escapeChars) {
|
|
421 |
String result = s;
|
|
422 |
for (String escapeChar : escapeChars) {
|
|
423 |
result = result.replace(escapeChar, getReplacementToken(escapeChar));
|
|
424 |
}
|
|
425 |
return result;
|
|
426 |
}
|
|
427 |
|
|
428 |
/**
|
|
429 |
* Provides replacement token.
|
|
430 |
* @param escapeChar
|
|
431 |
* @return replacement token
|
|
432 |
*/
|
|
433 |
protected String getReplacementToken(String escapeChar) {
|
|
434 |
if (escapeChar.equals(CR)) {
|
|
435 |
return "\\r";
|
|
436 |
} else if (escapeChar.equals(LF)) {
|
|
437 |
return "\\n";
|
|
438 |
} else if (escapeChar.equals(TAB)) {
|
|
439 |
return "\\t";
|
|
440 |
} else {
|
|
441 |
return "\\" + escapeChar;
|
|
442 |
}
|
|
443 |
}
|
|
444 |
|
|
445 |
/**
|
|
446 |
* Returns list from csv.
|
|
447 |
* @param csv
|
|
448 |
* @return list of values generated from CSV
|
|
449 |
*/
|
|
450 |
protected static final List<String> getListFromCSV(String csv) {
|
|
451 |
if (StringUtils.isBlank(csv)) {
|
|
452 |
return new ArrayList<String>();
|
|
453 |
}
|
|
454 |
List<String> list = new ArrayList<String>();
|
|
455 |
String[] tokens = StringUtils.split(csv, ",");
|
|
456 |
for (String token : tokens) {
|
|
457 |
list.add(token.trim());
|
|
458 |
}
|
|
459 |
return list;
|
|
460 |
}
|
|
461 |
|
|
462 |
public boolean isAntEchoPropertiesMode() {
|
|
463 |
return antEchoPropertiesMode;
|
|
464 |
}
|
|
465 |
|
|
466 |
public void setAntEchoPropertiesMode(boolean antEchoPropertiesMode) {
|
|
467 |
this.antEchoPropertiesMode = antEchoPropertiesMode;
|
|
468 |
}
|
|
469 |
|
|
470 |
public boolean isIncludeSystemProperties() {
|
|
471 |
return includeSystemProperties;
|
|
472 |
}
|
|
473 |
|
|
474 |
public void setIncludeSystemProperties(boolean includeSystemProperties) {
|
|
475 |
this.includeSystemProperties = includeSystemProperties;
|
|
476 |
}
|
|
477 |
|
|
478 |
public String getEscapeChars() {
|
|
479 |
return escapeChars;
|
|
480 |
}
|
|
481 |
|
|
482 |
public void setEscapeChars(String escapeChars) {
|
|
483 |
this.escapeChars = escapeChars;
|
|
484 |
}
|
|
485 |
|
|
486 |
public boolean isIncludeEnvironmentVariables() {
|
|
487 |
return includeEnvironmentVariables;
|
|
488 |
}
|
|
489 |
|
|
490 |
public void setIncludeEnvironmentVariables(boolean includeEnvironmentVariables) {
|
|
491 |
this.includeEnvironmentVariables = includeEnvironmentVariables;
|
|
492 |
}
|
|
493 |
|
|
494 |
public String getExclude() {
|
|
495 |
return exclude;
|
|
496 |
}
|
|
497 |
|
|
498 |
public void setExclude(String exclude) {
|
|
499 |
this.exclude = exclude;
|
|
500 |
}
|
|
501 |
|
|
502 |
public String getInclude() {
|
|
503 |
return include;
|
|
504 |
}
|
|
505 |
|
|
506 |
public void setInclude(String include) {
|
|
507 |
this.include = include;
|
|
508 |
}
|
|
509 |
|
|
510 |
public boolean isQuiet() {
|
|
511 |
return quiet;
|
|
512 |
}
|
|
513 |
|
|
514 |
public void setQuiet(boolean quiet) {
|
|
515 |
this.quiet = quiet;
|
|
516 |
}
|
|
517 |
|
|
518 |
/**
|
|
519 |
* Sets property files for which keys properties should be included.
|
|
520 |
* @param includePropertyKeysFromFiles
|
|
521 |
*/
|
|
522 |
public void setIncludePropertyKeysFromFiles(
|
|
523 |
String[] includePropertyKeysFromFiles) {
|
|
524 |
if (includePropertyKeysFromFiles!=null) {
|
|
525 |
this.includePropertyKeysFromFiles = Arrays.copyOf(
|
|
526 |
includePropertyKeysFromFiles,
|
|
527 |
includePropertyKeysFromFiles.length);
|
|
528 |
}
|
|
529 |
}
|
|
530 |
}
|
0 |
531 |
|
[maven-release-plugin] copy for tag icm-iis-properties-maven-plugin-0.0.2