View Javadoc

1   /**
2    * Copyright 2009-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.codehaus.mojo.properties;
17  
18  import java.io.File;
19  import java.io.IOException;
20  import java.io.OutputStream;
21  import java.util.Properties;
22  
23  import org.apache.commons.io.FileUtils;
24  import org.apache.commons.io.IOUtils;
25  import org.apache.maven.plugin.AbstractMojo;
26  import org.apache.maven.plugin.MojoExecutionException;
27  import org.apache.maven.project.MavenProject;
28  
29  /**
30   * @author <a href="mailto:zarars@gmail.com">Zarar Siddiqi</a>
31   * @version $Id: AbstractWritePropertiesMojo.java 8861 2009-01-21 15:35:38Z pgier $
32   */
33  public abstract class AbstractWritePropertiesMojo extends AbstractMojo {
34  
35  	/**
36  	 * @parameter default-value="${project}"
37  	 * @required
38  	 * @readonly
39  	 */
40  	protected MavenProject project;
41  
42  	/**
43  	 * The file that properties will be written to
44  	 * 
45  	 * @parameter expression="${properties.outputFile}" default-value="${project.build.directory}/properties/project.properties";
46  	 * @required
47  	 */
48  	protected File outputFile;
49  
50  	protected void writeProperties(File file, Properties properties) throws MojoExecutionException {
51  		SortedProperties sp = new SortedProperties();
52  		sp.putAll(properties);
53  		OutputStream out = null;
54  		try {
55  			out = FileUtils.openOutputStream(file);
56  			sp.store(out, null);
57  		} catch (IOException e) {
58  			throw new MojoExecutionException("Error creating properties file", e);
59  		} finally {
60  			IOUtils.closeQuietly(out);
61  		}
62  	}
63  }