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 |
|
|
22 |
|
|
23 |
|
@author |
24 |
|
|
25 |
|
|
26 |
|
|
|
|
| 0% |
Uncovered Elements: 141 (141) |
Complexity: 36 |
Complexity Density: 0.36 |
|
27 |
|
public class EclipseFormatterMojo extends ExecMojo { |
28 |
|
private static final String FS = System.getProperty("file.separator"); |
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
private String[] javaBinaries = new String[] { "javaw.exe", "java.exe", "java" }; |
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
private String eclipseBinary; |
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
private String vm; |
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
private String application; |
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
private String formatterPreferences; |
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
private String[] eclipseArgs = new String[] { "-nosplash", "-verbose" }; |
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
|
83 |
|
|
84 |
|
|
85 |
|
private String[] includes = new String[] { "**/src/main/java" }; |
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
|
91 |
|
|
92 |
|
private String[] excludes = new String[] { "**/.settings", "**/.svn" }; |
93 |
|
|
|
|
| 0% |
Uncovered Elements: 11 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
|
94 |
0
|
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 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
109 |
0
|
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 |
|
|
|
|
| 0% |
Uncovered Elements: 11 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
|
116 |
0
|
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 |
|
|
|
|
| 0% |
Uncovered Elements: 17 (17) |
Complexity: 3 |
Complexity Density: 0.23 |
|
132 |
0
|
@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 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
154 |
0
|
protected boolean fileExists(String filename) {... |
155 |
0
|
return new File(filename).exists(); |
156 |
|
} |
157 |
|
|
|
|
| 0% |
Uncovered Elements: 17 (17) |
Complexity: 6 |
Complexity Density: 0.55 |
|
158 |
0
|
protected File getJavaBinary() throws MojoExecutionException {... |
159 |
|
|
160 |
0
|
if (!isEmpty(vm) && fileExists(vm)) { |
161 |
0
|
return new File(vm); |
162 |
|
} |
163 |
|
|
164 |
|
|
165 |
0
|
if (!isEmpty(vm) && !fileExists(vm)) { |
166 |
0
|
throw new MojoExecutionException(vm + " does not exist"); |
167 |
|
} |
168 |
|
|
169 |
|
|
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 |
|
|
|
|
| 0% |
Uncovered Elements: 12 (12) |
Complexity: 1 |
Complexity Density: 0.08 |
|
182 |
0
|
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 |
|
|
|
|
| 0% |
Uncovered Elements: 23 (23) |
Complexity: 4 |
Complexity Density: 0.21 |
|
199 |
0
|
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 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
227 |
0
|
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 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
234 |
0
|
public String getEclipseBinary() {... |
235 |
0
|
return eclipseBinary; |
236 |
|
} |
237 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
238 |
0
|
public void setEclipseBinary(String eclipseExecutable) {... |
239 |
0
|
this.eclipseBinary = eclipseExecutable; |
240 |
|
} |
241 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
242 |
0
|
public String getVm() {... |
243 |
0
|
return vm; |
244 |
|
} |
245 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
246 |
0
|
public void setVm(String vm) {... |
247 |
0
|
this.vm = vm; |
248 |
|
} |
249 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
250 |
0
|
public String getApplication() {... |
251 |
0
|
return application; |
252 |
|
} |
253 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
254 |
0
|
public void setApplication(String application) {... |
255 |
0
|
this.application = application; |
256 |
|
} |
257 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
258 |
0
|
public String[] getIncludes() {... |
259 |
0
|
return includes; |
260 |
|
} |
261 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
262 |
0
|
public void setIncludes(String[] includes) {... |
263 |
0
|
this.includes = includes; |
264 |
|
} |
265 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
266 |
0
|
public String[] getExcludes() {... |
267 |
0
|
return excludes; |
268 |
|
} |
269 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
270 |
0
|
public void setExcludes(String[] excludes) {... |
271 |
0
|
this.excludes = excludes; |
272 |
|
} |
273 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
274 |
0
|
public String[] getJavaBinaries() {... |
275 |
0
|
return javaBinaries; |
276 |
|
} |
277 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
278 |
0
|
public void setJavaBinaries(String[] binaries) {... |
279 |
0
|
this.javaBinaries = binaries; |
280 |
|
} |
281 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
282 |
0
|
public String getFormatterPreferences() {... |
283 |
0
|
return formatterPreferences; |
284 |
|
} |
285 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
286 |
0
|
public void setFormatterPreferences(String formatterPreferences) {... |
287 |
0
|
this.formatterPreferences = formatterPreferences; |
288 |
|
} |
289 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
290 |
0
|
public String[] getEclipseArgs() {... |
291 |
0
|
return eclipseArgs; |
292 |
|
} |
293 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
294 |
0
|
public void setEclipseArgs(String[] args) {... |
295 |
0
|
this.eclipseArgs = args; |
296 |
|
} |
297 |
|
|
298 |
|
} |