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.ArrayList;
19 import java.util.Collections;
20 import java.util.List;
21 import java.util.Properties;
22
23 import org.apache.commons.lang.StringUtils;
24 import org.apache.maven.plugin.AbstractMojo;
25 import org.apache.maven.plugin.MojoExecutionException;
26 import org.apache.maven.project.MavenProject;
27 import org.jasypt.util.text.BasicTextEncryptor;
28
29
30
31
32
33
34
35 public class DecryptAllPropertiesMojo extends AbstractMojo {
36
37
38
39
40
41
42 private MavenProject project;
43
44
45
46
47
48
49
50 private boolean quiet;
51
52
53
54
55
56
57
58 private String endsWith;
59
60
61
62
63
64
65
66 private boolean show;
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 List<String> keys = new ArrayList<String>(props.stringPropertyNames());
82 Collections.sort(keys);
83 for (String key : keys) {
84 boolean decrypt = key.endsWith(endsWith);
85 if (!decrypt) {
86 continue;
87 }
88 String value = getProperty(key);
89 if (StringUtils.isBlank(value) && !quiet) {
90 getLog().info("Skipping blank property " + key);
91 continue;
92 }
93 String newValue = encryptor.decrypt(value);
94 int length = endsWith.length();
95 String newKey = key.substring(0, key.length() - length);
96 props.setProperty(newKey, newValue);
97 if (quiet) {
98 continue;
99 }
100 if (show) {
101 getLog().info("Setting " + newKey + "=" + newValue + " - " + value);
102 } else {
103 getLog().info("Setting " + newKey);
104 }
105 }
106 }
107
108 protected String getProperty(String key) {
109 String sys = System.getProperty(key);
110 String proj = project.getProperties().getProperty(key);
111 if (!StringUtils.isBlank(sys)) {
112 return sys;
113 } else {
114 return proj;
115 }
116 }
117
118 public boolean isQuiet() {
119 return quiet;
120 }
121
122 public void setQuiet(boolean quiet) {
123 this.quiet = quiet;
124 }
125
126 public String getEndsWith() {
127 return endsWith;
128 }
129
130 public void setEndsWith(String endsWith) {
131 this.endsWith = endsWith;
132 }
133
134 public boolean isShow() {
135 return show;
136 }
137
138 public void setShow(boolean show) {
139 this.show = show;
140 }
141
142 public String getPassword() {
143 return password;
144 }
145
146 public void setPassword(String password) {
147 this.password = password;
148 }
149
150 public MavenProject getProject() {
151 return project;
152 }
153 }