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.FileNotFoundException;
020    import java.io.FileOutputStream;
021    import java.io.IOException;
022    import java.util.Properties;
023    
024    import org.apache.maven.plugin.AbstractMojo;
025    import org.apache.maven.plugin.MojoExecutionException;
026    import org.apache.maven.project.MavenProject;
027    
028    /**
029     * @author <a href="mailto:zarars@gmail.com">Zarar Siddiqi</a>
030     * @version $Id: AbstractWritePropertiesMojo.java 8861 2009-01-21 15:35:38Z pgier $
031     */
032    public abstract class AbstractWritePropertiesMojo extends AbstractMojo {
033    
034        /**
035         * @parameter default-value="${project}"
036         * @required
037         * @readonly
038         */
039        protected MavenProject project;
040    
041        /**
042         * The properties file that will be used when writing properties.
043         *
044         * @parameter
045         * @required
046         */
047        protected File outputFile;
048    
049        protected void writeProperties(Properties properties, File file) throws MojoExecutionException {
050            FileOutputStream fos = null;
051            try {
052                fos = new FileOutputStream(file);
053                properties.store(fos, "Properties");
054            } catch (FileNotFoundException e) {
055                getLog().error("Could not create FileOutputStream: " + fos);
056                throw new MojoExecutionException(e.getMessage(), e);
057            } catch (IOException e) {
058                getLog().error("Error writing properties: " + fos);
059                throw new MojoExecutionException(e.getMessage(), e);
060            }
061    
062            try {
063                fos.close();
064            } catch (IOException e) {
065                getLog().error("Error closing FileOutputStream: " + fos);
066                throw new MojoExecutionException(e.getMessage(), e);
067            }
068        }
069    
070        protected void validateOutputFile() throws MojoExecutionException {
071            if (outputFile.isDirectory()) {
072                throw new MojoExecutionException("outputFile must be a file and not a directory");
073            }
074            // ensure path exists
075            if (outputFile.getParentFile() != null) {
076                outputFile.getParentFile().mkdirs();
077            }
078        }
079    }