View Javadoc

1   /**
2    * Copyright 2009-2013 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.Arrays;
19  import java.util.List;
20  import java.util.Properties;
21  
22  import org.apache.maven.plugin.AbstractMojo;
23  import org.apache.maven.plugin.MojoExecutionException;
24  import org.apache.maven.project.MavenProject;
25  import org.jasypt.util.text.BasicTextEncryptor;
26  import org.kuali.common.util.PropertyUtils;
27  
28  /**
29   * Generate encrypted values for the specified system or project properties.
30   * 
31   * @goal encrypt
32   */
33  public class EncryptPropertiesMojo extends AbstractMojo {
34  
35  	/**
36  	 * @parameter default-value="${project}"
37  	 * @required
38  	 * @readonly
39  	 */
40  	private MavenProject project;
41  
42  	/**
43  	 * The list of properties containing values to encrypt
44  	 * 
45  	 * @parameter
46  	 * @required
47  	 */
48  	private String[] properties;
49  
50  	/**
51  	 * 
52  	 * The password for encrypting property values. This same password can be used to to decrypt the encrypted values.
53  	 * 
54  	 * @parameter expression="${properties.password}"
55  	 * @required
56  	 */
57  	private String password;
58  
59  	@Override
60  	public void execute() throws MojoExecutionException {
61  
62  		// Setup the encryptor
63  		BasicTextEncryptor encryptor = new BasicTextEncryptor();
64  		encryptor.setPassword(password);
65  
66  		// Get project properties overridden by system/env properties
67  		Properties p = PropertyUtils.getGlobalProperties(project.getProperties());
68  
69  		// Set up the includes list
70  		List<String> includes = PropertyUtils.getSortedKeys(project.getProperties());
71  		if (properties != null) {
72  			includes = Arrays.asList(properties);
73  		}
74  
75  		// Trim things down to just the properties we want
76  		PropertyUtils.trim(p, includes, null);
77  
78  		// Encrypt the property values
79  		PropertyUtils.encrypt(p, encryptor);
80  
81  		// Update Maven with the encrypted values
82  		project.getProperties().putAll(p);
83  	}
84  
85  	public String[] getProperties() {
86  		return properties;
87  	}
88  
89  	public void setProperties(String[] properties) {
90  		this.properties = properties;
91  	}
92  
93  	public String getPassword() {
94  		return password;
95  	}
96  
97  	public void setPassword(String password) {
98  		this.password = password;
99  	}
100 
101 	public MavenProject getProject() {
102 		return project;
103 	}
104 }