Clover Coverage Report - Maven Eclipse Formatter Plugin 1.1-SNAPSHOT
Coverage timestamp: Wed Dec 31 1969 19:00:00 EST
../../../../img/srcFileCovDistChart0.png 0% of files have more coverage
100   298   36   4
16   197   0.36   25
25     1.44  
1    
 
  EclipseFormatterMojo       Line # 27 100 0% 36 141 0% 0.0
 
No Tests
 
1    package org.codehaus.mojo.exec;
2   
3    import java.io.File;
4    import java.io.FileOutputStream;
5    import java.io.IOException;
6    import java.io.InputStream;
7    import java.io.OutputStream;
8    import java.util.ArrayList;
9    import java.util.List;
10   
11    import org.apache.commons.io.IOUtils;
12    import org.apache.maven.plugin.MojoExecutionException;
13    import org.apache.tools.ant.DirectoryScanner;
14    import org.codehaus.plexus.util.StringUtils;
15    import org.springframework.core.io.DefaultResourceLoader;
16    import org.springframework.core.io.Resource;
17    import org.springframework.core.io.ResourceLoader;
18    import static org.codehaus.plexus.util.StringUtils.isEmpty;
19   
20    /**
21    * A plugin for executing the Eclipse java source code formatter
22    *
23    * @author Jeff Caddel
24    * @goal format
25    * @aggregator
26    */
 
27    public class EclipseFormatterMojo extends ExecMojo {
28    private static final String FS = System.getProperty("file.separator");
29   
30    /**
31    * Binaries representing a Java VM. Default values are "javaw.exe", "java.exe", and "java". This list is searched in
32    * order, stopping as soon as one is found.
33    *
34    * @parameter
35    */
36    private String[] javaBinaries = new String[] { "javaw.exe", "java.exe", "java" };
37   
38    /**
39    * Full path to the Eclipse executable binary
40    *
41    * @parameter expression="${eclipse.binary}"
42    * @required
43    */
44    private String eclipseBinary;
45   
46    /**
47    * Full path to a Java VM. This gets filled in using the system property "java.home" unless a value is supplied
48    * here.
49    *
50    * @parameter expression="${eclipse.vm}"
51    */
52    private String vm;
53   
54    /**
55    * This is the name of the Eclipse application that performs the formatting
56    *
57    * @parameter expression="${eclipse.application}" default-value="org.eclipse.jdt.core.JavaCodeFormatter"
58    * @required
59    */
60    private String application;
61   
62    /**
63    * Pointer to an Eclipse "org.eclipse.jdt.core.prefs" file. Supports "classpath:" style notation
64    *
65    * @parameter expression="${eclipse.formatterPreferences}" default-value="classpath:eclipse.prefs"
66    * @required
67    */
68    private String formatterPreferences;
69   
70    /**
71    * Any arguments specified here are passed to the Eclipse binary as additional command line arguments. Default
72    * values are "-nosplash -verbose"
73    *
74    * @parameter
75    */
76    private String[] eclipseArgs = new String[] { "-nosplash", "-verbose" };
77   
78    /**
79    * Regular expressions for directories that contain Java source code to format. Default value is
80    * **/src/main/java. The Eclipse formatter will recursively inspect any directories matching these
81    * patterns for *.java files
82    *
83    * @parameter
84    */
85    private String[] includes = new String[] { "**/src/main/java" };
86   
87    /**
88    * Regular expressions for directories to exclude from the process that scans for *.java files
89    *
90    * @parameter
91    */
92    private String[] excludes = new String[] { "**/.settings", "**/.svn" };
93   
 
94  0 toggle protected List<File> getSourceDirectories() {
95  0 DirectoryScanner scanner = new DirectoryScanner();
96  0 scanner.setBasedir(project.getBasedir());
97  0 scanner.setIncludes(includes);
98  0 scanner.setExcludes(excludes);
99  0 scanner.scan();
100  0 String[] includedDirs = scanner.getIncludedDirectories();
101  0 List<File> dirs = new ArrayList<File>();
102  0 for (String includedDir : includedDirs) {
103  0 File file = new File(project.getBasedir().getAbsolutePath() + FS + includedDir);
104  0 dirs.add(file);
105    }
106  0 return dirs;
107    }
108   
 
109  0 toggle protected void showDirs(List<File> srcDirs) {
110  0 getLog().info("Located " + srcDirs.size() + " source directories:");
111  0 for (File dir : srcDirs) {
112  0 getLog().info(dir.getAbsolutePath());
113    }
114    }
115   
 
116  0 toggle protected void showConfig(File eclipseBinary, File javaBinary) {
117  0 getLog().info("Eclipse Location: " + eclipseBinary.getAbsolutePath());
118  0 getLog().info("Java VM: " + javaBinary.getAbsolutePath());
119  0 getLog().info("Scanning directory: " + project.getBasedir());
120  0 StringBuilder sb = new StringBuilder();
121  0 for (String include : includes) {
122  0 sb.append(include + " ");
123    }
124  0 getLog().info("Includes: " + sb.toString());
125  0 sb = new StringBuilder();
126  0 for (String exclude : excludes) {
127  0 sb.append(exclude + " ");
128    }
129  0 getLog().info("Excludes: " + sb.toString());
130    }
131   
 
132  0 toggle @Override
133    public void execute() throws MojoExecutionException {
134  0 if (!fileExists(eclipseBinary)) {
135  0 throw new MojoExecutionException(eclipseBinary + " does not exist");
136    }
137  0 File eclipseBinaryFile = new File(eclipseBinary);
138  0 File javaBinary = getJavaBinary();
139  0 showConfig(eclipseBinaryFile, javaBinary);
140   
141  0 List<File> dirs = getSourceDirectories();
142  0 if (dirs.size() == 0) {
143  0 getLog().info("No directories containing source code were located");
144  0 return;
145    }
146  0 showDirs(dirs);
147   
148  0 super.setExecutable(eclipseBinaryFile.getAbsolutePath());
149  0 super.setArguments(getEclipseArguments(javaBinary, dirs));
150   
151  0 super.execute();
152    }
153   
 
154  0 toggle protected boolean fileExists(String filename) {
155  0 return new File(filename).exists();
156    }
157   
 
158  0 toggle protected File getJavaBinary() throws MojoExecutionException {
159    // They provided us with a VM, and we found a file in the location they specified
160  0 if (!isEmpty(vm) && fileExists(vm)) {
161  0 return new File(vm);
162    }
163   
164    // They asked us to use a specific VM, but it doesn't exist
165  0 if (!isEmpty(vm) && !fileExists(vm)) {
166  0 throw new MojoExecutionException(vm + " does not exist");
167    }
168   
169    // They did not specify a VM, attempt to locate one
170  0 String javaHome = System.getProperty("java.home");
171  0 String binaryHome = javaHome + FS + "bin";
172  0 for (String binary : javaBinaries) {
173  0 File file = new File(binaryHome + FS + binary);
174  0 if (file.exists()) {
175  0 return file;
176    }
177    }
178  0 throw new MojoExecutionException(
179    "No Java VM location was supplied, and we could not locate one using the System property 'java.home'");
180    }
181   
 
182  0 toggle protected List<String> getEclipseArguments(File javaBinary, List<File> dirs) throws MojoExecutionException {
183  0 List<String> args = new ArrayList<String>();
184  0 args.add("-application");
185  0 args.add(application);
186  0 args.add("-vm");
187  0 args.add(javaBinary.getAbsolutePath());
188  0 args.add("-config");
189  0 args.add(getConfigAbsolutePath());
190  0 for (String arg : eclipseArgs) {
191  0 addIfNotEmpty(args, arg);
192    }
193  0 for (File dir : dirs) {
194  0 args.add(dir.getAbsolutePath());
195    }
196  0 return args;
197    }
198   
 
199  0 toggle protected String getConfigAbsolutePath() throws MojoExecutionException {
200  0 File file = new File(formatterPreferences);
201  0 if (file.exists()) {
202  0 return file.getAbsolutePath();
203    }
204  0 ResourceLoader loader = new DefaultResourceLoader();
205  0 Resource resource = loader.getResource(formatterPreferences);
206  0 if (!resource.exists()) {
207  0 throw new MojoExecutionException("Unable to locate " + formatterPreferences);
208    }
209  0 OutputStream out = null;
210  0 InputStream in = null;
211  0 try {
212  0 File temp = File.createTempFile("eclipse.prefs.", null);
213  0 temp.deleteOnExit();
214  0 out = new FileOutputStream(temp);
215  0 in = resource.getInputStream();
216  0 IOUtils.copy(in, out);
217  0 return temp.getAbsolutePath();
218    } catch (IOException e) {
219  0 throw new MojoExecutionException("Error copying resource " + formatterPreferences, e);
220    } finally {
221  0 IOUtils.closeQuietly(out);
222  0 IOUtils.closeQuietly(in);
223    }
224   
225    }
226   
 
227  0 toggle protected void addIfNotEmpty(List<String> list, String s) {
228  0 if (StringUtils.isEmpty(s)) {
229  0 return;
230    }
231  0 list.add(s);
232    }
233   
 
234  0 toggle public String getEclipseBinary() {
235  0 return eclipseBinary;
236    }
237   
 
238  0 toggle public void setEclipseBinary(String eclipseExecutable) {
239  0 this.eclipseBinary = eclipseExecutable;
240    }
241   
 
242  0 toggle public String getVm() {
243  0 return vm;
244    }
245   
 
246  0 toggle public void setVm(String vm) {
247  0 this.vm = vm;
248    }
249   
 
250  0 toggle public String getApplication() {
251  0 return application;
252    }
253   
 
254  0 toggle public void setApplication(String application) {
255  0 this.application = application;
256    }
257   
 
258  0 toggle public String[] getIncludes() {
259  0 return includes;
260    }
261   
 
262  0 toggle public void setIncludes(String[] includes) {
263  0 this.includes = includes;
264    }
265   
 
266  0 toggle public String[] getExcludes() {
267  0 return excludes;
268    }
269   
 
270  0 toggle public void setExcludes(String[] excludes) {
271  0 this.excludes = excludes;
272    }
273   
 
274  0 toggle public String[] getJavaBinaries() {
275  0 return javaBinaries;
276    }
277   
 
278  0 toggle public void setJavaBinaries(String[] binaries) {
279  0 this.javaBinaries = binaries;
280    }
281   
 
282  0 toggle public String getFormatterPreferences() {
283  0 return formatterPreferences;
284    }
285   
 
286  0 toggle public void setFormatterPreferences(String formatterPreferences) {
287  0 this.formatterPreferences = formatterPreferences;
288    }
289   
 
290  0 toggle public String[] getEclipseArgs() {
291  0 return eclipseArgs;
292    }
293   
 
294  0 toggle public void setEclipseArgs(String[] args) {
295  0 this.eclipseArgs = args;
296    }
297   
298    }