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.Properties; 022 023 import org.apache.commons.io.FileUtils; 024 import org.apache.commons.io.IOUtils; 025 import org.apache.maven.plugin.AbstractMojo; 026 import org.apache.maven.plugin.MojoExecutionException; 027 import org.apache.maven.project.MavenProject; 028 029 /** 030 * @author <a href="mailto:zarars@gmail.com">Zarar Siddiqi</a> 031 * @version $Id: AbstractWritePropertiesMojo.java 8861 2009-01-21 15:35:38Z pgier $ 032 */ 033 public abstract class AbstractWritePropertiesMojo extends AbstractMojo { 034 035 /** 036 * @parameter default-value="${project}" 037 * @required 038 * @readonly 039 */ 040 protected MavenProject project; 041 042 /** 043 * The file that properties will be written to 044 * 045 * @parameter expression="${properties.outputFile}" default-value="${project.build.directory}/properties/project.properties"; 046 * @required 047 */ 048 protected File outputFile; 049 050 protected void writeProperties(File file, Properties properties) throws MojoExecutionException { 051 SortedProperties sp = new SortedProperties(); 052 sp.putAll(properties); 053 OutputStream out = null; 054 try { 055 out = FileUtils.openOutputStream(file); 056 sp.store(out, null); 057 } catch (IOException e) { 058 throw new MojoExecutionException("Error creating properties file", e); 059 } finally { 060 IOUtils.closeQuietly(out); 061 } 062 } 063 }