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.io.IOException;
020    import java.io.OutputStream;
021    import java.util.ArrayList;
022    import java.util.Collections;
023    import java.util.List;
024    import java.util.Properties;
025    
026    import org.apache.commons.io.FileUtils;
027    import org.apache.commons.io.IOUtils;
028    import org.apache.maven.plugin.AbstractMojo;
029    import org.apache.maven.plugin.MojoExecutionException;
030    import org.apache.maven.project.MavenProject;
031    import org.codehaus.plexus.util.StringUtils;
032    
033    /**
034     * @author <a href="mailto:zarars@gmail.com">Zarar Siddiqi</a>
035     * @version $Id: AbstractWritePropertiesMojo.java 8861 2009-01-21 15:35:38Z pgier $
036     */
037    public abstract class AbstractWritePropertiesMojo extends AbstractMojo {
038    
039            /**
040             * @parameter default-value="${project}"
041             * @required
042             * @readonly
043             */
044            MavenProject project;
045    
046            /**
047             * The file that properties will be written to
048             *
049             * @parameter expression="${properties.outputFile}" default-value="${project.build.directory}/properties/project.properties";
050             * @required
051             */
052            File outputFile;
053    
054            /**
055             * Either <code>NORMAL</code> or <code>ENVIRONMENT_VARIABLE</code>. When set to <code>ENVIRONMENT_VARIABLE</code> the keys in the
056             * properties file will all be upper case with periods replaced by underscores.
057             *
058             * @parameter expression="${properties.outputStyle}" default-value="NORMAL"
059             * @required
060             */
061            OutputStyle outputStyle;
062    
063            /**
064             * If supplied property keys are prefixed with this value before being stored.
065             *
066             * @parameter expression="${properties.prefix}"
067             */
068            String prefix;
069    
070            protected void writeProperties(File file, Properties properties, OutputStyle outputStyle, String prefix) throws MojoExecutionException {
071                    Properties prefixed = getPrefixedProperties(properties, prefix);
072                    Properties formatted = getFormattedProperties(prefixed, outputStyle);
073                    Properties sorted = getSortedProperties(formatted);
074                    OutputStream out = null;
075                    try {
076                            out = FileUtils.openOutputStream(file);
077                            sorted.store(out, "Created by the properties-maven-plugin");
078                    } catch (IOException e) {
079                            throw new MojoExecutionException("Error creating properties file", e);
080                    } finally {
081                            IOUtils.closeQuietly(out);
082                    }
083            }
084    
085            protected Properties getStandardMavenProperties(MavenProject project) {
086                    Properties properties = new Properties();
087                    properties.setProperty("project.groupId", project.getGroupId());
088                    properties.setProperty("project.artifactId", project.getArtifactId());
089                    properties.setProperty("project.version", project.getVersion());
090                    properties.setProperty("project.basedir", project.getBasedir().getAbsolutePath());
091                    properties.setProperty("project.build.directory", project.getBuild().getDirectory());
092                    return properties;
093            }
094    
095            protected SortedProperties getSortedProperties(Properties properties) {
096                    SortedProperties sp = new SortedProperties();
097                    sp.putAll(properties);
098                    return sp;
099            }
100    
101            protected Properties getFormattedProperties(Properties properties, OutputStyle style) {
102                    switch (style) {
103                    case NORMAL:
104                            return properties;
105                    case ENVIRONMENT_VARIABLE:
106                            return getEnvironmentVariableProperties(properties);
107                    default:
108                            throw new IllegalArgumentException(outputStyle + " is unknown");
109                    }
110            }
111    
112            protected Properties getPrefixedProperties(Properties properties, String prefix) {
113                    if (StringUtils.isBlank(prefix)) {
114                            return properties;
115                    }
116                    List<String> keys = new ArrayList<String>(properties.stringPropertyNames());
117                    Collections.sort(keys);
118                    Properties newProperties = new Properties();
119                    for (String key : keys) {
120                            String value = properties.getProperty(key);
121                            String newKey = prefix + "." + key;
122                            newProperties.setProperty(newKey, value);
123                    }
124                    return newProperties;
125            }
126    
127            protected Properties getEnvironmentVariableProperties(Properties properties) {
128                    List<String> keys = new ArrayList<String>(properties.stringPropertyNames());
129                    Collections.sort(keys);
130                    Properties newProperties = new Properties();
131                    for (String key : keys) {
132                            String value = properties.getProperty(key);
133                            String newKey = key.toUpperCase().replace(".", "_");
134                            newProperties.setProperty(newKey, value);
135                    }
136                    return newProperties;
137            }
138    }