1
2
3
4
5
6
7
8
9
10
11
12
13
14 package net.sf.maven.plugins.eclipseformat;
15
16 import org.apache.maven.plugin.AbstractMojo;
17 import org.apache.maven.plugin.MojoExecutionException;
18 import org.apache.maven.project.MavenProject;
19
20 import org.codehaus.plexus.util.DirectoryScanner;
21 import org.codehaus.plexus.util.IOUtil;
22 import org.codehaus.plexus.util.StringUtils;
23
24 import org.eclipse.jdt.core.ToolFactory;
25 import org.eclipse.jdt.core.formatter.CodeFormatter;
26
27 import org.eclipse.jface.text.Document;
28 import org.eclipse.jface.text.IDocument;
29
30 import org.eclipse.text.edits.TextEdit;
31
32 import java.io.BufferedWriter;
33 import java.io.File;
34 import java.io.FileWriter;
35
36 import java.util.Properties;
37
38
39
40
41
42
43
44
45
46
47 public class EclipseFormatMojo extends AbstractMojo {
48 protected static final String CODE_FORMAT_CONFIG = "/code-formatter.xml";
49
50
51
52
53
54
55
56
57 private String fileFormat;
58
59
60
61
62
63
64 private String fileEncoding;
65
66
67
68
69
70
71 private boolean failOnError;
72
73
74
75
76
77
78 private File sourceDirectory;
79
80
81
82
83
84
85 private File testSourceDirectory;
86
87
88
89
90
91
92
93 private String srcIncludesPattern;
94
95
96
97
98
99
100
101
102 private String srcExcludesPattern;
103
104
105
106
107
108
109
110 private String testIncludesPattern;
111
112
113
114
115
116
117
118
119 private String testExcludesPattern;
120
121
122
123
124
125
126 private String formatConfig;
127 private CodeFormatter codeFormatter;
128 private String lineSeperator;
129
130
131
132
133 public void execute() {
134 try {
135 init();
136
137 if (sourceDirectory.exists()) {
138 String[] filesToFormat = getIncludedFiles(sourceDirectory, srcIncludesPattern, srcExcludesPattern);
139
140 formatDirectory(sourceDirectory, filesToFormat);
141 }
142
143 if (testSourceDirectory.exists()) {
144 String[] filesToFormat = getIncludedFiles(testSourceDirectory, testIncludesPattern, testExcludesPattern);
145
146 formatDirectory(testSourceDirectory, filesToFormat);
147 }
148 } catch (Exception e) {
149 getLog().error("Error formatting files", e);
150 }
151 }
152
153
154
155
156 private String[] getIncludedFiles(final File directory, final String includes, final String excludes) {
157 DirectoryScanner scanner = new DirectoryScanner();
158
159 scanner.setBasedir(directory);
160 scanner.setIncludes(StringUtils.split(includes, ","));
161 scanner.setExcludes(StringUtils.split(excludes, ","));
162 scanner.scan();
163
164 return scanner.getIncludedFiles();
165 }
166
167
168
169
170 private void formatDirectory(final File directory, final String[] filesToFormat) throws MojoExecutionException {
171 for (int i = 0; i < filesToFormat.length; i++) {
172 File currentFile = new File(directory, filesToFormat[i]);
173 formatFile(currentFile);
174 }
175 }
176
177
178
179
180
181
182
183 private void formatFile(final File file) throws MojoExecutionException {
184 IDocument doc = new Document();
185
186 try {
187 String contents = new String(org.eclipse.jdt.internal.compiler.util.Util.getFileCharContent(file,
188 fileEncoding));
189 doc.set(contents);
190
191
192 TextEdit edit = codeFormatter.format(CodeFormatter.K_COMPILATION_UNIT, contents, 0, contents.length(), 0,
193 lineSeperator);
194
195
196 edit.apply(doc);
197
198 BufferedWriter out = new BufferedWriter(new FileWriter(file));
199
200 try {
201 out.write(doc.get());
202 out.flush();
203
204 if (getLog().isDebugEnabled()) {
205 getLog().debug("Formatted file: " + file);
206 }
207 } finally {
208 IOUtil.close(out);
209 }
210 } catch (Exception e) {
211 getLog().error("Error writing file: " + file, e);
212
213 if (failOnError) {
214 throw new MojoExecutionException("Error in format goal", e);
215 }
216 }
217 }
218
219
220
221
222
223
224 private void init() throws MojoExecutionException {
225 ConfigReader handler = new ConfigReader(getLog(), CODE_FORMAT_CONFIG, formatConfig, failOnError);
226 Properties formatterOptions = handler.getProperties();
227 codeFormatter = ToolFactory.createCodeFormatter(formatterOptions);
228
229 lineSeperator = System.getProperty("line.separator");
230
231 if (StringUtils.isNotBlank(fileFormat)) {
232 if ("dos".equalsIgnoreCase(fileFormat)) {
233 lineSeperator = "\r\n";
234 } else {
235 if ("mac".equalsIgnoreCase(fileFormat)) {
236 lineSeperator = "\r";
237 } else {
238 if ("unix".equalsIgnoreCase(fileFormat)) {
239 lineSeperator = "\n";
240 }
241 }
242 }
243 }
244 }
245
246
247
248
249
250
251
252
253
254
255
256
257 public void setFileFormat(final String fileFormat) {
258 this.fileFormat = fileFormat;
259 }
260
261
262
263
264
265
266
267
268
269
270 public void setFailOnError(final boolean failOnError) {
271 this.failOnError = failOnError;
272 }
273
274
275
276
277
278
279
280
281
282
283 public void setSourceDirectory(final File sourceDirectory) {
284 this.sourceDirectory = sourceDirectory;
285 }
286
287
288
289
290
291
292
293
294
295
296 public void setTestSourceDirectory(final File testSourceDirectory) {
297 this.testSourceDirectory = testSourceDirectory;
298 }
299
300
301
302
303
304
305
306
307
308
309
310 public void setSrcIncludesPattern(final String srcIncludesPattern) {
311 this.srcIncludesPattern = srcIncludesPattern;
312 }
313
314
315
316
317
318
319
320
321
322
323
324
325 public void setSrcExcludesPattern(final String srcExcludesPattern) {
326 this.srcExcludesPattern = srcExcludesPattern;
327 }
328
329
330
331
332
333
334
335
336
337
338
339 public void setTestIncludesPattern(final String testIncludesPattern) {
340 this.testIncludesPattern = testIncludesPattern;
341 }
342
343
344
345
346
347
348
349
350
351
352
353
354 public void setTestExcludesPattern(final String testExcludesPattern) {
355 this.testExcludesPattern = testExcludesPattern;
356 }
357
358
359
360
361
362
363
364 public void setFormatConfig(final String formatConfig) {
365 this.formatConfig = formatConfig;
366 }
367
368
369
370
371
372
373
374 public void setFileEncoding(final String fileEncoding) {
375 this.fileEncoding = fileEncoding;
376 }
377
378
379
380
381
382
383
384 public void setProject(final MavenProject project) {
385
386 }
387 }