View Javadoc

1   package org.codehaus.mojo.properties;
2   
3   import org.apache.maven.plugin.AbstractMojo;
4   import org.apache.maven.plugin.MojoExecutionException;
5   import org.apache.maven.project.MavenProject;
6   
7   import java.io.File;
8   import java.io.FileNotFoundException;
9   import java.io.FileOutputStream;
10  import java.io.IOException;
11  import java.util.Properties;
12  
13  /**
14   * @author <a href="mailto:zarars@gmail.com">Zarar Siddiqi</a>
15   * @version $Id$
16   */
17  public abstract class AbstractWritePropertiesMojo extends AbstractMojo {
18  
19      /**
20       * @parameter default-value="${project}"
21       * @required
22       * @readonly
23       */
24       protected MavenProject project;
25  
26      /**
27       * @parameter
28       * @required
29       */
30      protected File outputFile;
31  
32      protected void writeProperties(Properties properties, File file) {
33          FileOutputStream fos = null;
34          try {
35              fos = new FileOutputStream(file);
36          } catch (FileNotFoundException e) {
37              getLog().error("Could not create FileOutputStream: " + fos);
38              e.printStackTrace();
39          }
40          try {
41              properties.store(fos, "Properties");
42          } catch (IOException e) {
43              getLog().error("Error writing properties: " + fos);
44              e.printStackTrace();
45          }
46          try {
47              fos.close();
48          } catch (IOException e) {
49              getLog().error("Error closing FileOutputStream: " + fos);
50              e.printStackTrace();
51          }
52      }
53  
54      protected void validateOutputFile() throws MojoExecutionException {
55          if (outputFile.isDirectory()) {
56              throw new MojoExecutionException("outputFile must be a file and not a directory");
57          }
58          // ensure path exists
59          if (outputFile.getParentFile() != null) {
60              outputFile.getParentFile().mkdirs();
61          }
62      }
63  }