| 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 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | 0 | public class EclipseFormatterMojo extends ExecMojo { |
| 28 | 0 | private static final String FS = System.getProperty("file.separator"); |
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | 0 | 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 | 0 | private String[] eclipseArgs = new String[] { "-nosplash", "-verbose" }; |
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | 0 | private String[] includes = new String[] { "**/src/main/java" }; |
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | 0 | private String[] excludes = new String[] { "**/.settings", "**/.svn" }; |
| 93 | |
|
| 94 | |
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 | |
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 | 0 | } |
| 115 | |
|
| 116 | |
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 | 0 | } |
| 131 | |
|
| 132 | |
@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 | 0 | } |
| 153 | |
|
| 154 | |
protected boolean fileExists(String filename) { |
| 155 | 0 | return new File(filename).exists(); |
| 156 | |
} |
| 157 | |
|
| 158 | |
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 | |
|
| 182 | |
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 | |
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 | |
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 | 0 | } 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 | |
protected void addIfNotEmpty(List<String> list, String s) { |
| 228 | 0 | if (StringUtils.isEmpty(s)) { |
| 229 | 0 | return; |
| 230 | |
} |
| 231 | 0 | list.add(s); |
| 232 | 0 | } |
| 233 | |
|
| 234 | |
public String getEclipseBinary() { |
| 235 | 0 | return eclipseBinary; |
| 236 | |
} |
| 237 | |
|
| 238 | |
public void setEclipseBinary(String eclipseExecutable) { |
| 239 | 0 | this.eclipseBinary = eclipseExecutable; |
| 240 | 0 | } |
| 241 | |
|
| 242 | |
public String getVm() { |
| 243 | 0 | return vm; |
| 244 | |
} |
| 245 | |
|
| 246 | |
public void setVm(String vm) { |
| 247 | 0 | this.vm = vm; |
| 248 | 0 | } |
| 249 | |
|
| 250 | |
public String getApplication() { |
| 251 | 0 | return application; |
| 252 | |
} |
| 253 | |
|
| 254 | |
public void setApplication(String application) { |
| 255 | 0 | this.application = application; |
| 256 | 0 | } |
| 257 | |
|
| 258 | |
public String[] getIncludes() { |
| 259 | 0 | return includes; |
| 260 | |
} |
| 261 | |
|
| 262 | |
public void setIncludes(String[] includes) { |
| 263 | 0 | this.includes = includes; |
| 264 | 0 | } |
| 265 | |
|
| 266 | |
public String[] getExcludes() { |
| 267 | 0 | return excludes; |
| 268 | |
} |
| 269 | |
|
| 270 | |
public void setExcludes(String[] excludes) { |
| 271 | 0 | this.excludes = excludes; |
| 272 | 0 | } |
| 273 | |
|
| 274 | |
public String[] getJavaBinaries() { |
| 275 | 0 | return javaBinaries; |
| 276 | |
} |
| 277 | |
|
| 278 | |
public void setJavaBinaries(String[] binaries) { |
| 279 | 0 | this.javaBinaries = binaries; |
| 280 | 0 | } |
| 281 | |
|
| 282 | |
public String getFormatterPreferences() { |
| 283 | 0 | return formatterPreferences; |
| 284 | |
} |
| 285 | |
|
| 286 | |
public void setFormatterPreferences(String formatterPreferences) { |
| 287 | 0 | this.formatterPreferences = formatterPreferences; |
| 288 | 0 | } |
| 289 | |
|
| 290 | |
public String[] getEclipseArgs() { |
| 291 | 0 | return eclipseArgs; |
| 292 | |
} |
| 293 | |
|
| 294 | |
public void setEclipseArgs(String[] args) { |
| 295 | 0 | this.eclipseArgs = args; |
| 296 | 0 | } |
| 297 | |
|
| 298 | |
} |