/
AbstractGeneratorMojo.java
370 lines (336 loc) · 10.9 KB
/
AbstractGeneratorMojo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*
* Copyright 2013 The Sculptor Project Team, including the original
* author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.sculptor.maven.plugin;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.FileUtils;
/**
* Base class for all code generator-related {@link Mojo}s of this plugin. It
* provides common properties (like the outlet directories for the code
* generator) and shared code (like handling of the generated files).
*/
public abstract class AbstractGeneratorMojo extends AbstractMojo {
/**
* The enclosing project.
*/
@Parameter(defaultValue="${project}", readonly = true)
protected MavenProject project;
/**
* Directory for source-code artifacts. If an artifact with the same name
* already exists, the generation of the artifact will be skipped.
*/
@Parameter(defaultValue = "src/main/java", required = true)
protected File outletSrcOnceDir;
/**
* Directory for non-source-code artifacts. If an artifact with the same
* name already exists, the generation of the artifact will be skipped.
*/
@Parameter(defaultValue = "src/main/resources", required = true)
protected File outletResOnceDir;
/**
* Directory for source-code artifacts. Existings artifacts will be
* overwritten.
*/
@Parameter(defaultValue = "src/generated/java", required = true)
protected File outletSrcDir;
/**
* Directory for non-source-code artifacts. Existings artifacts will be
* overwritten.
*/
@Parameter(defaultValue = "src/generated/resources", required = true)
protected File outletResDir;
/**
* Directory for Webapp artifacts. If an artifact with the same name already
* exists, the generation of the artifact will be skipped.
*/
@Parameter(defaultValue = "src/main/webapp", required = true)
protected File outletWebrootDir;
/**
* Directory for test source-code artifacts. If an artifact with the same
* name already exists, the generation of the artifact will be skipped.
*/
@Parameter(defaultValue = "src/test/java", required = true)
protected File outletSrcTestOnceDir;
/**
* Directory for test non-source-code artifacts. Existings artifacts will
* not be overwritten.
*/
@Parameter(defaultValue = "src/test/resources", required = true)
protected File outletResTestOnceDir;
/**
* Directory for source-code test-artifacts. Existings artifacts will be
* overwritten.
*/
@Parameter(defaultValue = "src/test/generated/java", required = true)
protected File outletSrcTestDir;
/**
* Directory for non-source-code test-artifacts. Existings artifacts will be
* overwritten.
*/
@Parameter(defaultValue = "src/test/generated/resources", required = true)
protected File outletResTestDir;
/**
* Directory for documentation artifacts. Existings artifacts will be
* overwritten.
*/
@Parameter(defaultValue = "src/site", required = true)
protected File outletDocDir;
/**
* File holding the status of the last code generator execution.
*/
@Parameter(defaultValue = ".sculptor-status", required = true)
private File statusFile;
/**
* Verbose logging.
* <p>
* Can be set from command line using '-Dverbose=true'.
*/
@Parameter(property = "verbose", defaultValue = "false")
private boolean verbose;
/**
* Returns enclosing {@link MavenProject}.
*/
protected MavenProject getProject() {
return project;
}
/**
* Check if the logging should be verbose.
* <p>
* If Maven debug logging is requested "mvn -X" the verbose logging is active
* as well.
*
* @return true to verbose logging
*/
protected boolean isVerbose() {
return verbose || getLog().isDebugEnabled();
}
/**
* Returns the StatusFile (defined via {@link #statusFile}) or
* <code>null</code> if none exists.
*/
protected File getStatusFile() {
return (statusFile.exists() ? statusFile : null);
}
/**
* Updates the StatusFile (defined via {@link #statusFile}). This file
* indicates the last successful execution of the code generator and is used
* to check the state of the source files.
*
* @param createdFiles
* list of files created by the code generator
*/
protected boolean updateStatusFile(List<File> createdFiles) {
boolean success;
final Properties statusProperties = new Properties();
try {
for (File createdFile : createdFiles) {
try {
statusProperties.setProperty(
getProjectRelativePath(createdFile),
calculateChecksum(createdFile));
} catch (IOException e) {
getLog().warn(
"Checksum calculation failed: " + e.getMessage());
}
}
final FileWriter statusWriter = new FileWriter(statusFile);
try {
statusProperties.store(statusWriter,
"Sculptor created the following " + createdFiles.size()
+ " files");
success = true;
} finally {
statusWriter.close();
}
} catch (IOException e) {
getLog().warn("Updating status file failed: " + e.getMessage());
success = false;
}
return success;
}
/**
* Returns the list of generated files from the StatusFile (defined via
* {@link #statusFile}) or <code>null</code> if no StatusFile exists.
*/
protected Set<String> getGeneratedFiles() {
Properties statusFileProps = getStatusProperties();
if (statusFileProps != null) {
return statusFileProps.stringPropertyNames();
}
return null;
}
/**
* Returns the {@link Properties} instance populated from the StatusFile
* (defined via {@link #statusFile}) or <code>null</code> if no StatusFile
* exists.
*/
private Properties getStatusProperties() {
final File statusFile = getStatusFile();
if (statusFile != null) {
try {
Properties statusFileProps = new Properties();
final FileReader statusReader = new FileReader(statusFile);
try {
statusFileProps.load(statusReader);
return statusFileProps;
} finally {
statusReader.close();
}
} catch (IOException e) {
getLog().warn("Reading status file failed: " + e.getMessage());
}
}
return null;
}
/**
* Deletes the files in the directories marked as 'generated' and the
* unmodified one-shot generated files.
* <p>
* The list of all previously generated files is retrieved from the StatusFile
* (defined via {@link #statusFile}). Modified one-shot generated files are
* detected by a changed file checksum.
*/
protected boolean deleteGeneratedFiles() {
boolean success;
// First delete all files in the directories marked as 'generated'
cleanDirectory(outletSrcDir);
cleanDirectory(outletResDir);
cleanDirectory(outletSrcTestDir);
cleanDirectory(outletResTestDir);
// Finally delete the non-modified one-shot generated files in the other folders
Properties statusFileProps = getStatusProperties();
// If there is no status file to compare against or no files are
// generated then skip deletion
if (statusFileProps == null || statusFileProps.isEmpty()) {
// No status file - we can't delete any previously generated files
success = true;
} else {
try {
// Iterate through the list of generated files
for (String fileName : statusFileProps.stringPropertyNames()) {
File file = new File(getProject().getBasedir(), fileName);
if (file.exists()) {
// For one-shot generated files compare checksum before
// deleting
boolean delete;
if (fileName
.startsWith(getProjectRelativePath(outletSrcOnceDir))
|| fileName
.startsWith(getProjectRelativePath(outletResOnceDir))
|| fileName
.startsWith(getProjectRelativePath(outletWebrootDir))
|| fileName
.startsWith(getProjectRelativePath(outletSrcTestOnceDir))
|| fileName
.startsWith(getProjectRelativePath(outletResTestOnceDir))) {
delete = calculateChecksum(file).equals(
statusFileProps.getProperty(fileName));
if (!delete
&& (isVerbose() || getLog()
.isDebugEnabled())) {
getLog().info(
"Keeping previously generated modified"
+ " file: " + file);
}
} else {
delete = false;
}
if (delete) {
if (isVerbose() || getLog().isDebugEnabled()) {
getLog().info(
"Deleting previously generated file: "
+ file);
}
// We have to make sure the file is deleted on
// Windows as well
FileUtils.forceDelete(file);
// Delete image file generated from dot file
if (fileName.endsWith(".dot")) {
File imageFile = new File(getProject()
.getBasedir(), fileName + ".png");
if (imageFile.exists()) {
if (isVerbose()
|| getLog().isDebugEnabled()) {
getLog().info(
"Deleting previously generated file: "
+ imageFile);
}
// We have to make sure the file is deleted
// on Windows as well
FileUtils.forceDelete(imageFile);
}
}
}
}
}
success = true;
} catch (IOException e) {
getLog().warn("Reading status file failed: " + e.getMessage());
success = false;
}
}
return success;
}
/**
* Deletes all files within the given directory.
*/
private void cleanDirectory(File dir) {
if (isVerbose() || getLog().isDebugEnabled()) {
getLog().info("Deleting previously generated files in directory: " + dir.getPath());
}
if (dir.exists()) {
try {
FileUtils.cleanDirectory(dir);
} catch (IOException e) {
getLog().warn("Cleaning directory failed: " + e.getMessage());
}
}
}
/**
* Returns the path of given file relative to the enclosing Maven project.
*/
protected String getProjectRelativePath(File file) {
String path = file.getAbsolutePath();
String prefix = project.getBasedir().getAbsolutePath();
if (path.startsWith(prefix)) {
path = path.substring(prefix.length() + 1);
}
if (File.separatorChar != '/') {
path = path.replace(File.separatorChar, '/');
}
return path;
}
/**
* Returns a hex representation of the MD5 checksum from given file.
*/
private String calculateChecksum(File file) throws IOException {
InputStream is = new FileInputStream(file);
return DigestUtils.md5Hex(is);
}
}