1 package org.codehaus.mojo.properties;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
34
35
36 public abstract class AbstractWritePropertiesMojo extends AbstractMojo
37 {
38
39
40
41
42
43
44 protected MavenProject project;
45
46
47
48
49
50
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
93 if ( outputFile.getParentFile() != null )
94 {
95 outputFile.getParentFile().mkdirs();
96 }
97 }
98 }