View Javadoc

1   /**
2    * Copyright 2009-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Generate encrypted values for the specified system or project properties.
28   *
29   * @goal encrypt
30   */
31  public class EncryptPropertiesMojo extends AbstractMojo {
32  
33      /**
34       * @parameter default-value="${project}"
35       * @required
36       * @readonly
37       */
38      private MavenProject project;
39  
40      /**
41       * The list of properties containing values to encrypt
42       *
43       * @parameter
44       * @required
45       */
46      private String[] properties;
47  
48      /**
49       * The encrypted value for the properties are stored under their original property key with this text appended to
50       * the end.
51       *
52       * eg "database.password" is stored as "database.password.encrypted"
53       *
54       * @parameter expression="${properties.suffix}" default-value="encrypted"
55       * @required
56       */
57      private String suffix;
58  
59      /**
60       * If true, the plain text values being encrypted are displayed to the console.
61       *
62       * @parameter expression="${properties.show}" default-value="false"
63       * @required
64       */
65      private boolean show;
66  
67      /**
68       * If true, the plugin will emit no logging information
69       *
70       * @parameter expression="${properties.quiet}" default-value="false"
71       * @required
72       */
73      private boolean quiet;
74  
75      /**
76       *
77       * The password for encrypting property values. This same password can be used to to decrypt the encrypted values.
78       *
79       * @parameter expression="${properties.password}"
80       * @required
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 }