001    /**
002     * Copyright 2009-2012 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.codehaus.mojo.properties;
017    
018    import java.io.File;
019    import java.util.Properties;
020    
021    import org.apache.maven.plugin.AbstractMojo;
022    import org.apache.maven.project.MavenProject;
023    import org.kuali.common.util.PropertyUtils;
024    
025    /**
026     * @author <a href="mailto:zarars@gmail.com">Zarar Siddiqi</a>
027     * @version $Id: AbstractWritePropertiesMojo.java 8861 2009-01-21 15:35:38Z pgier $
028     */
029    public abstract class AbstractWritePropertiesMojo extends AbstractMojo {
030    
031            /**
032             * @parameter default-value="${project}"
033             * @required
034             * @readonly
035             */
036            MavenProject project;
037    
038            /**
039             * The file that properties will be written to
040             *
041             * @parameter expression="${properties.outputFile}" default-value="${project.build.directory}/properties/project.properties";
042             * @required
043             */
044            File outputFile;
045    
046            /**
047             * Either <code>NORMAL</code> or <code>ENVIRONMENT_VARIABLE</code>. When set to <code>ENVIRONMENT_VARIABLE</code> the keys in the
048             * properties file will all be upper case with periods replaced by underscores.
049             *
050             * @parameter expression="${properties.outputStyle}" default-value="NORMAL"
051             * @required
052             */
053            OutputStyle outputStyle;
054    
055            /**
056             * If supplied property keys are prefixed with this value before being stored.
057             *
058             * @parameter expression="${properties.prefix}"
059             */
060            String prefix;
061    
062            /**
063             * The encoding to use when storing the properties
064             *
065             * @parameter expression="${properties.encoding}" default-value="${project.build.sourceEncoding}"
066             */
067            String encoding;
068    
069            /**
070             * Anything provided here is added as a comment at the top of the properties file.
071             *
072             * @parameter expression="${properties.comment}"
073             */
074            String comment;
075    
076            protected void writeProperties(File file, Properties properties, OutputStyle outputStyle, String prefix, String encoding, String comment) {
077                    Properties prefixed = PropertyUtils.getPrefixedProperties(properties, prefix);
078                    Properties formatted = getFormattedProperties(prefixed, outputStyle);
079                    PropertyUtils.store(formatted, file, encoding, comment);
080            }
081    
082            protected Properties getFormattedProperties(Properties properties, OutputStyle style) {
083                    switch (style) {
084                    case NORMAL:
085                            return properties;
086                    case ENVIRONMENT_VARIABLE:
087                            return PropertyUtils.reformatKeysAsEnvVars(properties);
088                    default:
089                            throw new IllegalArgumentException(outputStyle + " is unknown");
090                    }
091            }
092    
093            public MavenProject getProject() {
094                    return project;
095            }
096    
097            public File getOutputFile() {
098                    return outputFile;
099            }
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    }