1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
30
31
32 public abstract class AbstractWritePropertiesMojo extends AbstractMojo {
33
34
35
36
37
38
39 protected MavenProject project;
40
41
42
43
44
45
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
75 if (outputFile.getParentFile() != null) {
76 outputFile.getParentFile().mkdirs();
77 }
78 }
79 }