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  
36  public class DecryptAllPropertiesMojo extends AbstractMojo {
37  
38      
39  
40  
41  
42  
43      private MavenProject project;
44  
45      
46  
47  
48  
49  
50  
51      private boolean quiet;
52  
53      
54  
55  
56  
57  
58  
59      private String endsWith;
60  
61      
62  
63  
64  
65  
66  
67      private boolean show;
68  
69      
70  
71  
72  
73  
74  
75      private String password;
76  
77      @Override
78      public void execute() throws MojoExecutionException {
79          BasicTextEncryptor encryptor = new BasicTextEncryptor();
80          encryptor.setPassword(password);
81          Properties props = project.getProperties();
82          List<String> keys = new ArrayList<String>(props.stringPropertyNames());
83          Collections.sort(keys);
84          for (String key : keys) {
85              boolean decrypt = key.endsWith(endsWith);
86              if (!decrypt) {
87                  continue;
88              }
89              String value = getProperty(key);
90              if (StringUtils.isBlank(value) && !quiet) {
91                  getLog().info("Skipping blank property " + key);
92                  continue;
93              }
94              String newValue = encryptor.decrypt(value);
95              int length = endsWith.length();
96              String newKey = key.substring(0, key.length() - length);
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);
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     public boolean isQuiet() {
120         return quiet;
121     }
122 
123     public void setQuiet(boolean quiet) {
124         this.quiet = quiet;
125     }
126 
127     public String getEndsWith() {
128         return endsWith;
129     }
130 
131     public void setEndsWith(String endsWith) {
132         this.endsWith = endsWith;
133     }
134 
135     public boolean isShow() {
136         return show;
137     }
138 
139     public void setShow(boolean show) {
140         this.show = show;
141     }
142 
143     public String getPassword() {
144         return password;
145     }
146 
147     public void setPassword(String password) {
148         this.password = password;
149     }
150 
151     public MavenProject getProject() {
152         return project;
153     }
154 }