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.FileNotFoundException;
20  import java.io.FileOutputStream;
21  import java.io.IOException;
22  import java.util.Properties;
23  
24  import org.apache.maven.plugin.AbstractMojo;
25  import org.apache.maven.plugin.MojoExecutionException;
26  import org.apache.maven.project.MavenProject;
27  
28  /**
29   * @author <a href="mailto:zarars@gmail.com">Zarar Siddiqi</a>
30   * @version $Id: AbstractWritePropertiesMojo.java 8861 2009-01-21 15:35:38Z pgier $
31   */
32  public abstract class AbstractWritePropertiesMojo extends AbstractMojo {
33  
34      /**
35       * @parameter default-value="${project}"
36       * @required
37       * @readonly
38       */
39      protected MavenProject project;
40  
41      /**
42       * The properties file that will be used when writing properties.
43       *
44       * @parameter
45       * @required
46       */
47      protected File outputFile;
48  
49      protected void writeProperties(Properties properties, File file) throws MojoExecutionException {
50          FileOutputStream fos = null;
51          try {
52              fos = new FileOutputStream(file);
53              properties.store(fos, "Properties");
54          } catch (FileNotFoundException e) {
55              getLog().error("Could not create FileOutputStream: " + fos);
56              throw new MojoExecutionException(e.getMessage(), e);
57          } catch (IOException e) {
58              getLog().error("Error writing properties: " + fos);
59              throw new MojoExecutionException(e.getMessage(), e);
60          }
61  
62          try {
63              fos.close();
64          } catch (IOException e) {
65              getLog().error("Error closing FileOutputStream: " + fos);
66              throw new MojoExecutionException(e.getMessage(), e);
67          }
68      }
69  
70      protected void validateOutputFile() throws MojoExecutionException {
71          if (outputFile.isDirectory()) {
72              throw new MojoExecutionException("outputFile must be a file and not a directory");
73          }
74          // ensure path exists
75          if (outputFile.getParentFile() != null) {
76              outputFile.getParentFile().mkdirs();
77          }
78      }
79  }