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.Properties;
19
20 import org.apache.commons.lang.StringUtils;
21 import org.apache.maven.plugin.AbstractMojo;
22 import org.apache.maven.plugin.MojoExecutionException;
23 import org.apache.maven.project.MavenProject;
24 import org.jasypt.util.text.BasicTextEncryptor;
25
26
27
28
29
30
31 public class EncryptPropertiesMojo extends AbstractMojo {
32
33
34
35
36
37
38 private MavenProject project;
39
40
41
42
43
44
45
46 private String[] properties;
47
48
49
50
51
52
53
54
55
56
57 private String suffix;
58
59
60
61
62
63
64
65 private boolean show;
66
67
68
69
70
71
72
73
74 private String password;
75
76 @Override
77 public void execute() throws MojoExecutionException {
78 BasicTextEncryptor encryptor = new BasicTextEncryptor();
79 encryptor.setPassword(password);
80 Properties props = project.getProperties();
81 for (String key : properties) {
82 String value = getProperty(key);
83 if (StringUtils.isBlank(value)) {
84 getLog().info("Skipping " + key);
85 continue;
86 }
87 String newValue = encryptor.encrypt(value);
88 String newKey = key + "." + suffix;
89 props.setProperty(newKey, newValue);
90 if (show) {
91 getLog().info("Setting " + newKey + "=" + newValue + " - " + value);
92 } else {
93 getLog().info("Setting " + newKey + "=" + newValue);
94 }
95 }
96 }
97
98 protected String getProperty(String key) {
99 String sys = System.getProperty(key);
100 String proj = project.getProperties().getProperty(key);
101 if (!StringUtils.isBlank(sys)) {
102 return sys;
103 } else {
104 return proj;
105 }
106
107 }
108
109 public String[] getProperties() {
110 return properties;
111 }
112
113 public void setProperties(String[] properties) {
114 this.properties = properties;
115 }
116 }