1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.codehaus.mojo.properties;
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37 import org.apache.maven.plugin.AbstractMojo;
38 import org.apache.maven.plugin.MojoExecutionException;
39 import org.apache.maven.project.MavenProject;
40
41 import java.io.File;
42 import java.io.FileNotFoundException;
43 import java.io.FileOutputStream;
44 import java.io.IOException;
45 import java.util.Properties;
46
47
48
49
50
51 public abstract class AbstractWritePropertiesMojo extends AbstractMojo
52 {
53
54
55
56
57
58
59 protected MavenProject project;
60
61
62
63
64
65
66
67 protected File outputFile;
68
69 protected void writeProperties( Properties properties, File file )
70 throws MojoExecutionException
71 {
72 FileOutputStream fos = null;
73 try
74 {
75 fos = new FileOutputStream( file );
76 properties.store( fos, "Properties" );
77 }
78 catch ( FileNotFoundException e )
79 {
80 getLog().error( "Could not create FileOutputStream: " + fos );
81 throw new MojoExecutionException( e.getMessage(), e );
82 }
83 catch ( IOException e )
84 {
85 getLog().error( "Error writing properties: " + fos );
86 throw new MojoExecutionException( e.getMessage(), e );
87 }
88
89 try
90 {
91 fos.close();
92 }
93 catch ( IOException e )
94 {
95 getLog().error( "Error closing FileOutputStream: " + fos );
96 throw new MojoExecutionException( e.getMessage(), e );
97 }
98 }
99
100 protected void validateOutputFile()
101 throws MojoExecutionException
102 {
103 if ( outputFile.isDirectory() )
104 {
105 throw new MojoExecutionException( "outputFile must be a file and not a directory" );
106 }
107
108 if ( outputFile.getParentFile() != null )
109 {
110 outputFile.getParentFile().mkdirs();
111 }
112 }
113 }