001    /**
002     * Copyright 2009-2011 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    /*
019     * Licensed to the Apache Software Foundation (ASF) under one
020     * or more contributor license agreements.  See the NOTICE file 
021     * distributed with this work for additional information
022     * regarding copyright ownership.  The ASF licenses this file
023     * to you under the Apache License, Version 2.0 (the
024     * "License"); you may not use this file except in compliance
025     * with the License.  You may obtain a copy of the License at
026     * 
027     *   http://www.apache.org/licenses/LICENSE-2.0
028     * 
029     * Unless required by applicable law or agreed to in writing, 
030     * software distributed under the License is distributed on an
031     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 
032     * KIND, either express or implied.  See the License for the 
033     * specific language governing permissions and limitations 
034     * under the License.
035     */
036    
037    import org.apache.maven.plugin.AbstractMojo;
038    import org.apache.maven.plugin.MojoExecutionException;
039    import org.apache.maven.project.MavenProject;
040    
041    import java.io.File;
042    import java.io.FileNotFoundException;
043    import java.io.FileOutputStream;
044    import java.io.IOException;
045    import java.util.Properties;
046    
047    /**
048     * @author <a href="mailto:zarars@gmail.com">Zarar Siddiqi</a>
049     * @version $Id: AbstractWritePropertiesMojo.java 8861 2009-01-21 15:35:38Z pgier $
050     */
051    public abstract class AbstractWritePropertiesMojo extends AbstractMojo
052    {
053    
054        /**
055         * @parameter default-value="${project}"
056         * @required
057         * @readonly
058         */
059        protected MavenProject project;
060    
061        /**
062         * The properties file that will be used when writing properties.
063         *
064         * @parameter
065         * @required
066         */
067        protected File outputFile;
068    
069        protected void writeProperties( Properties properties, File file ) 
070            throws MojoExecutionException
071        {
072            FileOutputStream fos = null;
073            try
074            {
075                fos = new FileOutputStream( file );
076                properties.store( fos, "Properties" );
077            }
078            catch ( FileNotFoundException e )
079            {
080                getLog().error( "Could not create FileOutputStream: " + fos );
081                throw new MojoExecutionException( e.getMessage(), e );
082            }
083            catch ( IOException e )
084            {
085                getLog().error( "Error writing properties: " + fos );
086                throw new MojoExecutionException( e.getMessage(), e );
087            }
088            
089            try
090            {
091                fos.close();
092            }
093            catch ( IOException e )
094            {
095                getLog().error( "Error closing FileOutputStream: " + fos );
096                throw new MojoExecutionException( e.getMessage(), e );
097            }
098        }
099    
100        protected void validateOutputFile()
101            throws MojoExecutionException
102        {
103            if ( outputFile.isDirectory() )
104            {
105                throw new MojoExecutionException( "outputFile must be a file and not a directory" );
106            }
107            // ensure path exists
108            if ( outputFile.getParentFile() != null )
109            {
110                outputFile.getParentFile().mkdirs();
111            }
112        }
113    }