1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.maven.plugin.buildmetadata.io;
17
18 import java.io.BufferedInputStream;
19 import java.io.BufferedOutputStream;
20 import java.io.File;
21 import java.io.FileInputStream;
22 import java.io.FileNotFoundException;
23 import java.io.FileOutputStream;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.OutputStream;
27 import java.util.Properties;
28
29 import org.apache.commons.io.IOUtils;
30 import org.apache.maven.plugin.MojoExecutionException;
31 import org.apache.maven.plugin.logging.Log;
32 import org.apache.maven.project.MavenProject;
33
34 import de.smartics.maven.plugin.buildmetadata.common.MojoUtils;
35 import de.smartics.maven.plugin.buildmetadata.common.SortedProperties;
36
37
38
39
40
41
42
43 public final class BuildPropertiesFileHelper
44 {
45
46
47
48
49
50
51
52
53
54 private final Log log;
55
56
57
58
59 private final File propertiesOutputFile;
60
61
62
63
64
65
66
67
68
69
70
71 public BuildPropertiesFileHelper(final Log log,
72 final File propertiesOutputFile)
73 {
74 this.log = log;
75 this.propertiesOutputFile = propertiesOutputFile;
76 }
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96 public File writePropertiesFile(final Properties buildMetaDataProperties)
97 throws MojoExecutionException
98 {
99 final File buildMetaDataFile =
100 createBuildMetaDataFile(propertiesOutputFile);
101 if (log.isInfoEnabled())
102 {
103 log.info("Writing properties '" + buildMetaDataFile.getAbsolutePath()
104 + "'...");
105 }
106
107 OutputStream out = null;
108 try
109 {
110 out = new BufferedOutputStream(new FileOutputStream(buildMetaDataFile));
111 final String comments = "Created by maven-buildmetadata-plugin.";
112 final Properties sortedBuildMetaDataProperties =
113 SortedProperties.createSorted(buildMetaDataProperties);
114 sortedBuildMetaDataProperties.store(out, comments);
115 }
116 catch (final FileNotFoundException e)
117 {
118 final String message =
119 "Cannot find file '" + buildMetaDataFile
120 + "' to write properties to.";
121 throw MojoUtils.createException(log, e, message);
122 }
123 catch (final IOException e)
124 {
125 final String message =
126 "Cannot write properties to file '" + buildMetaDataFile + "'.";
127 throw MojoUtils.createException(log, e, message);
128 }
129 finally
130 {
131 IOUtils.closeQuietly(out);
132 }
133
134 return buildMetaDataFile;
135 }
136
137
138
139
140
141
142
143
144
145 private File createBuildMetaDataFile(final File propertiesOutputFile)
146 throws MojoExecutionException
147 {
148 final File outputDirectory = propertiesOutputFile.getParentFile();
149 if (!outputDirectory.exists())
150 {
151 final boolean created = outputDirectory.mkdirs();
152 if (!created)
153 {
154 throw new MojoExecutionException("Cannot create output directory '"
155 + outputDirectory + "'.");
156 }
157 }
158 return propertiesOutputFile;
159 }
160
161
162
163
164
165
166
167
168
169
170 public void readBuildPropertiesFile(final Properties buildMetaDataProperties)
171 throws MojoExecutionException
172 {
173 InputStream inStream = null;
174 try
175 {
176 inStream =
177 new BufferedInputStream(new FileInputStream(propertiesOutputFile));
178 buildMetaDataProperties.load(inStream);
179 }
180 catch (final IOException e)
181 {
182 throw new MojoExecutionException(
183 "Cannot read provided properties file: "
184 + propertiesOutputFile.getAbsolutePath(), e);
185 }
186 finally
187 {
188 IOUtils.closeQuietly(inStream);
189 }
190 }
191
192
193
194
195
196
197
198
199 public Properties getProjectProperties(final MavenProject project)
200 {
201 Properties projectProperties = project.getProperties();
202 if (projectProperties == null)
203 {
204 projectProperties = new Properties();
205 project.getModel().setProperties(projectProperties);
206 }
207
208 return projectProperties;
209 }
210
211
212
213 }