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 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
@author |
23 |
|
|
24 |
|
|
25 |
|
|
|
|
| 0% |
Uncovered Elements: 131 (131) |
Complexity: 31 |
Complexity Density: 0.33 |
|
26 |
|
public class EclipseFormatterMojo extends ExecMojo { |
27 |
|
private static final String FS = System.getProperty("file.separator"); |
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
private String[] javaBinaries = new String[] { "javaw.exe", "java.exe", "java" }; |
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
private String eclipseBinary; |
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
private String vm; |
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
private String application; |
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
private String formatterPreferences; |
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
private String[] eclipseArgs = new String[] { "-nosplash", "-verbose" }; |
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
|
83 |
|
|
84 |
|
private String[] includes = new String[] { "**/src/main/java", "**/src/test/java" }; |
85 |
|
|
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
|
91 |
|
private String[] excludes = new String[] { "**/.settings", "**/.svn" }; |
92 |
|
|
|
|
| 0% |
Uncovered Elements: 11 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
|
93 |
0
|
protected List<File> getSourceDirectories() {... |
94 |
0
|
DirectoryScanner scanner = new DirectoryScanner(); |
95 |
0
|
scanner.setBasedir(project.getBasedir()); |
96 |
0
|
scanner.setIncludes(includes); |
97 |
0
|
scanner.setExcludes(excludes); |
98 |
0
|
scanner.scan(); |
99 |
0
|
String[] includedDirs = scanner.getIncludedDirectories(); |
100 |
0
|
List<File> dirs = new ArrayList<File>(); |
101 |
0
|
for (String includedDir : includedDirs) { |
102 |
0
|
File file = new File(project.getBasedir().getAbsolutePath() + FS + includedDir); |
103 |
0
|
dirs.add(file); |
104 |
|
} |
105 |
0
|
return dirs; |
106 |
|
} |
107 |
|
|
|
|
| 0% |
Uncovered Elements: 28 (28) |
Complexity: 3 |
Complexity Density: 0.12 |
|
108 |
0
|
@Override... |
109 |
|
public void execute() throws MojoExecutionException { |
110 |
0
|
File file = new File(eclipseBinary); |
111 |
0
|
if (!file.exists()) { |
112 |
0
|
throw new MojoExecutionException(eclipseBinary + " does not exist"); |
113 |
|
} |
114 |
0
|
getLog().info("Eclipse Location: " + file.getAbsolutePath()); |
115 |
0
|
getLog().info("Java VM: " + getJavaBinary()); |
116 |
0
|
getLog().info("Scanning directory: " + project.getBasedir()); |
117 |
0
|
StringBuilder sb = new StringBuilder(); |
118 |
0
|
for (String include : includes) { |
119 |
0
|
sb.append(include + " "); |
120 |
|
} |
121 |
0
|
getLog().info("Includes: " + sb.toString()); |
122 |
0
|
sb = new StringBuilder(); |
123 |
0
|
for (String exclude : excludes) { |
124 |
0
|
sb.append(exclude + " "); |
125 |
|
} |
126 |
0
|
getLog().info("Excludes: " + sb.toString()); |
127 |
0
|
List<File> dirs = getSourceDirectories(); |
128 |
|
|
129 |
0
|
if (dirs.size() == 0) { |
130 |
0
|
getLog().info("No directories containing source code were located"); |
131 |
0
|
return; |
132 |
|
} else { |
133 |
0
|
getLog().info("Located " + dirs.size() + " source directories:"); |
134 |
0
|
for (File dir : dirs) { |
135 |
0
|
getLog().info(dir.getAbsolutePath()); |
136 |
|
} |
137 |
|
} |
138 |
|
|
139 |
0
|
super.setExecutable(quote(eclipseBinary)); |
140 |
0
|
super.setArguments(getEclipseArguments(dirs)); |
141 |
|
|
142 |
0
|
super.execute(); |
143 |
|
} |
144 |
|
|
|
|
| 0% |
Uncovered Elements: 13 (13) |
Complexity: 3 |
Complexity Density: 0.33 |
|
145 |
0
|
protected String getJavaBinary() throws MojoExecutionException {... |
146 |
0
|
if (!StringUtils.isEmpty(vm)) { |
147 |
0
|
return vm; |
148 |
|
} |
149 |
0
|
String javaHome = System.getProperty("java.home"); |
150 |
0
|
String binaryHome = javaHome + FS + "bin"; |
151 |
0
|
for (String binary : javaBinaries) { |
152 |
0
|
File file = new File(binaryHome + FS + binary); |
153 |
0
|
if (file.exists()) { |
154 |
0
|
return file.getAbsolutePath(); |
155 |
|
} |
156 |
|
} |
157 |
0
|
throw new MojoExecutionException( |
158 |
|
"No Java VM location was supplied, and we could not locate one using the System property 'java.home'"); |
159 |
|
} |
160 |
|
|
|
|
| 0% |
Uncovered Elements: 12 (12) |
Complexity: 1 |
Complexity Density: 0.08 |
|
161 |
0
|
protected List<String> getEclipseArguments(List<File> dirs) throws MojoExecutionException {... |
162 |
0
|
List<String> args = new ArrayList<String>(); |
163 |
0
|
args.add("-application"); |
164 |
0
|
args.add(quote(application)); |
165 |
0
|
args.add("-vm"); |
166 |
0
|
args.add(quote(getJavaBinary())); |
167 |
0
|
args.add("-config"); |
168 |
0
|
args.add(quote(getConfigAbsolutePath())); |
169 |
0
|
for (String arg : eclipseArgs) { |
170 |
0
|
addIfNotEmpty(args, arg); |
171 |
|
} |
172 |
0
|
for (File dir : dirs) { |
173 |
0
|
args.add(quote(dir.getAbsolutePath())); |
174 |
|
} |
175 |
0
|
return args; |
176 |
|
} |
177 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
178 |
0
|
protected String quote(String s) {... |
179 |
0
|
return '"' + s + '"'; |
180 |
|
} |
181 |
|
|
|
|
| 0% |
Uncovered Elements: 22 (22) |
Complexity: 4 |
Complexity Density: 0.22 |
|
182 |
0
|
protected String getConfigAbsolutePath() throws MojoExecutionException {... |
183 |
0
|
File file = new File(formatterPreferences); |
184 |
0
|
if (file.exists()) { |
185 |
0
|
return file.getAbsolutePath(); |
186 |
|
} |
187 |
0
|
ResourceLoader loader = new DefaultResourceLoader(); |
188 |
0
|
Resource resource = loader.getResource(formatterPreferences); |
189 |
0
|
if (!resource.exists()) { |
190 |
0
|
throw new MojoExecutionException("Unable to locate " + formatterPreferences); |
191 |
|
} |
192 |
0
|
OutputStream out = null; |
193 |
0
|
InputStream in = null; |
194 |
0
|
try { |
195 |
0
|
File temp = File.createTempFile("eclipse.prefs.", null); |
196 |
0
|
out = new FileOutputStream(temp); |
197 |
0
|
in = resource.getInputStream(); |
198 |
0
|
IOUtils.copy(in, out); |
199 |
0
|
return temp.getAbsolutePath(); |
200 |
|
} catch (IOException e) { |
201 |
0
|
throw new MojoExecutionException("Error copying resource " + formatterPreferences, e); |
202 |
|
} finally { |
203 |
0
|
IOUtils.closeQuietly(out); |
204 |
0
|
IOUtils.closeQuietly(in); |
205 |
|
} |
206 |
|
|
207 |
|
} |
208 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
209 |
0
|
protected void addIfNotEmpty(List<String> list, String s) {... |
210 |
0
|
if (StringUtils.isEmpty(s)) { |
211 |
0
|
return; |
212 |
|
} |
213 |
0
|
list.add(s); |
214 |
|
} |
215 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
216 |
0
|
public String getEclipseBinary() {... |
217 |
0
|
return eclipseBinary; |
218 |
|
} |
219 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
220 |
0
|
public void setEclipseBinary(String eclipseExecutable) {... |
221 |
0
|
this.eclipseBinary = eclipseExecutable; |
222 |
|
} |
223 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
224 |
0
|
public String getVm() {... |
225 |
0
|
return vm; |
226 |
|
} |
227 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
228 |
0
|
public void setVm(String vm) {... |
229 |
0
|
this.vm = vm; |
230 |
|
} |
231 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
232 |
0
|
public String getApplication() {... |
233 |
0
|
return application; |
234 |
|
} |
235 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
236 |
0
|
public void setApplication(String application) {... |
237 |
0
|
this.application = application; |
238 |
|
} |
239 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
240 |
0
|
public String[] getIncludes() {... |
241 |
0
|
return includes; |
242 |
|
} |
243 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
244 |
0
|
public void setIncludes(String[] includes) {... |
245 |
0
|
this.includes = includes; |
246 |
|
} |
247 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
248 |
0
|
public String[] getExcludes() {... |
249 |
0
|
return excludes; |
250 |
|
} |
251 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
252 |
0
|
public void setExcludes(String[] excludes) {... |
253 |
0
|
this.excludes = excludes; |
254 |
|
} |
255 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
256 |
0
|
public String[] getJavaBinaries() {... |
257 |
0
|
return javaBinaries; |
258 |
|
} |
259 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
260 |
0
|
public void setJavaBinaries(String[] binaries) {... |
261 |
0
|
this.javaBinaries = binaries; |
262 |
|
} |
263 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
264 |
0
|
public String getFormatterPreferences() {... |
265 |
0
|
return formatterPreferences; |
266 |
|
} |
267 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
268 |
0
|
public void setFormatterPreferences(String formatterPreferences) {... |
269 |
0
|
this.formatterPreferences = formatterPreferences; |
270 |
|
} |
271 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
272 |
0
|
public String[] getEclipseArgs() {... |
273 |
0
|
return eclipseArgs; |
274 |
|
} |
275 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
276 |
0
|
public void setEclipseArgs(String[] args) {... |
277 |
0
|
this.eclipseArgs = args; |
278 |
|
} |
279 |
|
|
280 |
|
} |