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 private boolean quiet;
74
75
76
77
78
79
80
81
82 private String password;
83
84 @Override
85 public void execute() throws MojoExecutionException {
86 BasicTextEncryptor encryptor = new BasicTextEncryptor();
87 encryptor.setPassword(password);
88 Properties props = project.getProperties();
89 for (String key : properties) {
90 String value = getProperty(key);
91 if (StringUtils.isBlank(value) && !quiet) {
92 getLog().info("Skipping " + key);
93 continue;
94 }
95 String newValue = encryptor.encrypt(value);
96 String newKey = key + "." + suffix;
97 props.setProperty(newKey, newValue);
98 if (quiet) {
99 continue;
100 }
101 if (show) {
102 getLog().info("Setting " + newKey + "=" + newValue + " - " + value);
103 } else {
104 getLog().info("Setting " + newKey + "=" + newValue);
105 }
106 }
107 }
108
109 protected String getProperty(String key) {
110 String sys = System.getProperty(key);
111 String proj = project.getProperties().getProperty(key);
112 if (!StringUtils.isBlank(sys)) {
113 return sys;
114 } else {
115 return proj;
116 }
117
118 }
119
120 public String[] getProperties() {
121 return properties;
122 }
123
124 public void setProperties(String[] properties) {
125 this.properties = properties;
126 }
127
128 public String getSuffix() {
129 return suffix;
130 }
131
132 public void setSuffix(String suffix) {
133 this.suffix = suffix;
134 }
135
136 public boolean isShow() {
137 return show;
138 }
139
140 public void setShow(boolean show) {
141 this.show = show;
142 }
143
144 public boolean isQuiet() {
145 return quiet;
146 }
147
148 public void setQuiet(boolean quiet) {
149 this.quiet = quiet;
150 }
151
152 public String getPassword() {
153 return password;
154 }
155
156 public void setPassword(String password) {
157 this.password = password;
158 }
159
160 public MavenProject getProject() {
161 return project;
162 }
163 }