From 85c2e2eb15fb9979f14a1735f726dc4ac414ff45 Mon Sep 17 00:00:00 2001 From: Clement Escoffier Date: Sat, 31 May 2014 17:52:44 +0200 Subject: [PATCH] Add information in the WatchingException to store the file, line, character and reason of the processing issue (#26). Also add the support of the Java Compiler. Signed-off-by: Clement Escoffier --- .../org/wisdom/maven/WatchingException.java | 28 +++++++++ .../wisdom/maven/mojos/JavaCompilerMojo.java | 60 +++++++++++++++---- .../wisdom/maven/utils/CompilerExecutor.java | 25 ++++++++ 3 files changed, 101 insertions(+), 12 deletions(-) diff --git a/core/wisdom-maven-plugin/src/main/java/org/wisdom/maven/WatchingException.java b/core/wisdom-maven-plugin/src/main/java/org/wisdom/maven/WatchingException.java index 6176c66de..d8f694edd 100644 --- a/core/wisdom-maven-plugin/src/main/java/org/wisdom/maven/WatchingException.java +++ b/core/wisdom-maven-plugin/src/main/java/org/wisdom/maven/WatchingException.java @@ -28,6 +28,9 @@ public class WatchingException extends Exception { private final File file; + private final int line; + private final int character; + /** * Creates a Watching Exception. * @@ -55,8 +58,14 @@ public WatchingException(String message, Throwable cause) { * @param cause the cause of the error, if known. */ public WatchingException(String message, File file, Throwable cause) { + this(message, file, -1, -1, cause); + } + + public WatchingException(String message, File file, int line, int character, Throwable cause) { super(message, cause); this.file = file; + this.line = line; + this.character = character; } /** @@ -68,4 +77,23 @@ public File getFile() { return file; } + /** + * Gets the line having triggered the issue. It's the line from the file returned by {@link #getFile()}. + * + * @return the line number, {@value -1} is there are no line specified. + */ + public int getLine() { + return line; + } + + /** + * Gets the character / row having triggered the issue. It's the character from the line returned by {@link + * #getLine()}, from the {@link #getFile()} file. + * + * @return the character number, {@value -1} is there are no character specified. + */ + public int getCharacter() { + return character; + } + } diff --git a/core/wisdom-maven-plugin/src/main/java/org/wisdom/maven/mojos/JavaCompilerMojo.java b/core/wisdom-maven-plugin/src/main/java/org/wisdom/maven/mojos/JavaCompilerMojo.java index e6adcd9a8..bcbfa9104 100644 --- a/core/wisdom-maven-plugin/src/main/java/org/wisdom/maven/mojos/JavaCompilerMojo.java +++ b/core/wisdom-maven-plugin/src/main/java/org/wisdom/maven/mojos/JavaCompilerMojo.java @@ -46,37 +46,77 @@ public class JavaCompilerMojo extends AbstractWisdomWatcherMojo implements Const private File classes; private CompilerExecutor compiler = new CompilerExecutor(); + /** + * Compiles the Java sources. + * + * @throws MojoExecutionException thrown on compilation error. + */ @Override public void execute() throws MojoExecutionException { classes = new File(buildDirectory, "classes"); compiler.execute(this); } + /** + * Checks whether or not an event on the given file should trigger the Java compilation. + * + * @param file the file + * @return {@literal true} if the given file is a JAva file and is contained in the Java source directory. + */ @Override public boolean accept(File file) { return WatcherUtils.isInDirectory(file, WatcherUtils.getJavaSource(basedir)); } + /** + * A new (accepted) file was created. This methods triggers the Java compilation. + * + * @param file the file + * @return {@literal true} + * @throws WatchingException thrown on compilation error. The thrown exception contains the file, line, + * character and reason of the compilation error. + */ @Override public boolean fileCreated(File file) throws WatchingException { + compile(); + return true; + } + + private void compile() throws WatchingException { try { execute(); } catch (MojoExecutionException e) { + if (e.getCause() != null + && e.getCause().getClass().getName().equals("org.apache.maven.plugin.compiler" + + ".CompilationFailureException")) { + throw CompilerExecutor.build(this, e.getCause()); + } throw new WatchingException("Compilation error", e); } - return true; } + /** + * A new (accepted) file was updated. This methods triggers the Java compilation. + * + * @param file the file + * @return {@literal true} + * @throws WatchingException thrown on compilation error. The thrown exception contains the file, line, + * character and reason of the compilation error. + */ @Override public boolean fileUpdated(File file) throws WatchingException { - try { - execute(); - } catch (MojoExecutionException e) { - throw new WatchingException("Compilation error", e); - } + compile(); return true; } + /** + * A new (accepted) file was deleted. This methods triggers the Java compilation. + * + * @param file the file + * @return {@literal true} + * @throws WatchingException thrown on compilation error. The thrown exception contains the file, line, + * character and reason of the compilation error. + */ @Override public boolean fileDeleted(final File file) throws WatchingException { // Delete the associated class file. @@ -96,14 +136,10 @@ public boolean accept(File dir, String name) { }, TrueFileFilter.INSTANCE); for (File clazz : files) { - clazz.delete(); + getLog().debug("Deleting " + clazz.getAbsolutePath() + " : " + clazz.delete()); } - try { - execute(); - } catch (MojoExecutionException e) { - throw new WatchingException("Compilation error", e); - } + compile(); return true; } } diff --git a/core/wisdom-maven-plugin/src/main/java/org/wisdom/maven/utils/CompilerExecutor.java b/core/wisdom-maven-plugin/src/main/java/org/wisdom/maven/utils/CompilerExecutor.java index aa3550106..32bdd811f 100644 --- a/core/wisdom-maven-plugin/src/main/java/org/wisdom/maven/utils/CompilerExecutor.java +++ b/core/wisdom-maven-plugin/src/main/java/org/wisdom/maven/utils/CompilerExecutor.java @@ -21,9 +21,13 @@ import org.apache.maven.plugin.MojoExecutionException; import org.codehaus.plexus.util.xml.Xpp3Dom; +import org.wisdom.maven.WatchingException; import org.wisdom.maven.mojos.AbstractWisdomMojo; +import java.io.File; import java.lang.reflect.InvocationTargetException; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import static org.twdata.maven.mojoexecutor.MojoExecutor.*; @@ -96,4 +100,25 @@ public static String getLongMessage(AbstractWisdomMojo mojo, Object exception) { } return null; } + + public static Pattern JAVA_COMPILATION_ERROR = Pattern.compile("(.*):\\[(.*),(.*)\\](.*)"); + + public static WatchingException build(AbstractWisdomMojo mojo, Throwable exception) { + String message = getLongMessage(mojo, exception); + if (message.contains("\n")) { + message = message.substring(0, message.indexOf("\n")).trim(); + } + + final Matcher matcher = JAVA_COMPILATION_ERROR.matcher(message); + if (matcher.matches()) { + String path = matcher.group(1); + String line = matcher.group(2); + String character = matcher.group(3); + String reason = matcher.group(4); + File file = new File(path); + return new WatchingException(reason, file, Integer.valueOf(line), Integer.valueOf(character), null); + } else { + return new WatchingException("Java Compilation Error", exception); + } + } }