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.io.File;
19  import java.util.Properties;
20  
21  import org.apache.maven.plugin.AbstractMojo;
22  import org.apache.maven.project.MavenProject;
23  import org.kuali.common.util.PropertyUtils;
24  
25  /**
26   * @author <a href="mailto:zarars@gmail.com">Zarar Siddiqi</a>
27   * @version $Id: AbstractWritePropertiesMojo.java 8861 2009-01-21 15:35:38Z pgier $
28   */
29  public abstract class AbstractWritePropertiesMojo extends AbstractMojo {
30  
31  	/**
32  	 * @parameter default-value="${project}"
33  	 * @required
34  	 * @readonly
35  	 */
36  	MavenProject project;
37  
38  	/**
39  	 * The file that properties will be written to
40  	 *
41  	 * @parameter expression="${properties.outputFile}" default-value="${project.build.directory}/properties/project.properties";
42  	 * @required
43  	 */
44  	File outputFile;
45  
46  	/**
47  	 * Either <code>NORMAL</code> or <code>ENVIRONMENT_VARIABLE</code>. When set to <code>ENVIRONMENT_VARIABLE</code> the keys in the
48  	 * properties file will all be upper case with periods replaced by underscores.
49  	 *
50  	 * @parameter expression="${properties.outputStyle}" default-value="NORMAL"
51  	 * @required
52  	 */
53  	OutputStyle outputStyle;
54  
55  	/**
56  	 * If supplied property keys are prefixed with this value before being stored.
57  	 *
58  	 * @parameter expression="${properties.prefix}"
59  	 */
60  	String prefix;
61  
62  	/**
63  	 * The encoding to use when storing the properties
64  	 *
65  	 * @parameter expression="${properties.encoding}" default-value="${project.build.sourceEncoding}"
66  	 */
67  	String encoding;
68  
69  	/**
70  	 * Anything provided here is added as a comment at the top of the properties file.
71  	 *
72  	 * @parameter expression="${properties.comment}"
73  	 */
74  	String comment;
75  
76  	protected void writeProperties(File file, Properties properties, OutputStyle outputStyle, String prefix, String encoding, String comment) {
77  		Properties prefixed = PropertyUtils.getPrefixedProperties(properties, prefix);
78  		Properties formatted = getFormattedProperties(prefixed, outputStyle);
79  		PropertyUtils.store(formatted, file, encoding, comment);
80  	}
81  
82  	protected Properties getFormattedProperties(Properties properties, OutputStyle style) {
83  		switch (style) {
84  		case NORMAL:
85  			return properties;
86  		case ENVIRONMENT_VARIABLE:
87  			return PropertyUtils.reformatKeysAsEnvVars(properties);
88  		default:
89  			throw new IllegalArgumentException(outputStyle + " is unknown");
90  		}
91  	}
92  
93  	public MavenProject getProject() {
94  		return project;
95  	}
96  
97  	public File getOutputFile() {
98  		return outputFile;
99  	}
100 
101 	public void setOutputFile(File outputFile) {
102 		this.outputFile = outputFile;
103 	}
104 
105 	public OutputStyle getOutputStyle() {
106 		return outputStyle;
107 	}
108 
109 	public void setOutputStyle(OutputStyle outputStyle) {
110 		this.outputStyle = outputStyle;
111 	}
112 
113 	public String getPrefix() {
114 		return prefix;
115 	}
116 
117 	public void setPrefix(String prefix) {
118 		this.prefix = prefix;
119 	}
120 
121 	public String getEncoding() {
122 		return encoding;
123 	}
124 
125 	public void setEncoding(String encoding) {
126 		this.encoding = encoding;
127 	}
128 
129 	public String getComment() {
130 		return comment;
131 	}
132 
133 	public void setComment(String comment) {
134 		this.comment = comment;
135 	}
136 }