View Javadoc

1   package org.codehaus.mojo.properties;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file 
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   * 
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing, 
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 
17   * KIND, either express or implied.  See the License for the 
18   * specific language governing permissions and limitations 
19   * under the License.
20   */
21  
22  import org.apache.maven.plugin.AbstractMojo;
23  import org.apache.maven.plugin.MojoExecutionException;
24  import org.apache.maven.project.MavenProject;
25  
26  import java.io.File;
27  import java.io.FileNotFoundException;
28  import java.io.FileOutputStream;
29  import java.io.IOException;
30  import java.util.Properties;
31  
32  /**
33   * @author <a href="mailto:zarars@gmail.com">Zarar Siddiqi</a>
34   * @version $Id: AbstractWritePropertiesMojo.java 8861 2009-01-21 15:35:38Z pgier $
35   */
36  public abstract class AbstractWritePropertiesMojo extends AbstractMojo
37  {
38  
39      /**
40       * @parameter default-value="${project}"
41       * @required
42       * @readonly
43       */
44      protected MavenProject project;
45  
46      /**
47       * The properties file that will be used when writing properties.
48       *
49       * @parameter
50       * @required
51       */
52      protected File outputFile;
53  
54      protected void writeProperties( Properties properties, File file ) 
55          throws MojoExecutionException
56      {
57          FileOutputStream fos = null;
58          try
59          {
60              fos = new FileOutputStream( file );
61              properties.store( fos, "Properties" );
62          }
63          catch ( FileNotFoundException e )
64          {
65              getLog().error( "Could not create FileOutputStream: " + fos );
66              throw new MojoExecutionException( e.getMessage(), e );
67          }
68          catch ( IOException e )
69          {
70              getLog().error( "Error writing properties: " + fos );
71              throw new MojoExecutionException( e.getMessage(), e );
72          }
73          
74          try
75          {
76              fos.close();
77          }
78          catch ( IOException e )
79          {
80              getLog().error( "Error closing FileOutputStream: " + fos );
81              throw new MojoExecutionException( e.getMessage(), e );
82          }
83      }
84  
85      protected void validateOutputFile()
86          throws MojoExecutionException
87      {
88          if ( outputFile.isDirectory() )
89          {
90              throw new MojoExecutionException( "outputFile must be a file and not a directory" );
91          }
92          // ensure path exists
93          if ( outputFile.getParentFile() != null )
94          {
95              outputFile.getParentFile().mkdirs();
96          }
97      }
98  }