Improve support for m2e (Maven/Eclipse integration) (fixes #319)

This commit is contained in:
Sam Harwell 2014-01-16 15:41:40 -06:00
parent 3ba70ba1a7
commit f2966aaa80
6 changed files with 88 additions and 25 deletions

View File

@ -92,6 +92,12 @@
<version>2.2</version> <version>2.2</version>
</dependency> </dependency>
<dependency>
<groupId>org.sonatype.plexus</groupId>
<artifactId>plexus-build-api</artifactId>
<version>0.0.7</version>
</dependency>
<!-- <!--
The version of ANTLR tool that this version of the plugin controls. The version of ANTLR tool that this version of the plugin controls.
We have decided that this should be in lockstep with ANTLR itself, other We have decided that this should be in lockstep with ANTLR itself, other
@ -134,6 +140,11 @@
<build> <build>
<defaultGoal>install</defaultGoal> <defaultGoal>install</defaultGoal>
<resources>
<resource>
<directory>resources</directory>
</resource>
</resources>
<plugins> <plugins>

View File

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<goals>
<goal>antlr4</goal>
</goals>
</pluginExecutionFilter>
<action>
<execute>
<runOnIncremental>true</runOnIncremental>
<runOnConfiguration>true</runOnConfiguration>
</execute>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>

View File

@ -33,8 +33,11 @@ import org.antlr.v4.runtime.misc.NotNull;
import org.antlr.v4.tool.ANTLRMessage; import org.antlr.v4.tool.ANTLRMessage;
import org.antlr.v4.tool.ANTLRToolListener; import org.antlr.v4.tool.ANTLRToolListener;
import org.apache.maven.plugin.logging.Log; import org.apache.maven.plugin.logging.Log;
import org.sonatype.plexus.build.incremental.BuildContext;
import org.stringtemplate.v4.ST; import org.stringtemplate.v4.ST;
import java.io.File;
/** /**
* This implementation of {@link ANTLRToolListener} reports messages to the * This implementation of {@link ANTLRToolListener} reports messages to the
* {@link Log} instance provided by Maven. * {@link Log} instance provided by Maven.
@ -44,6 +47,7 @@ import org.stringtemplate.v4.ST;
public class Antlr4ErrorLog implements ANTLRToolListener { public class Antlr4ErrorLog implements ANTLRToolListener {
private final Tool tool; private final Tool tool;
private final BuildContext buildContext;
private final Log log; private final Log log;
/** /**
@ -51,8 +55,9 @@ public class Antlr4ErrorLog implements ANTLRToolListener {
* *
* @param log The Maven log * @param log The Maven log
*/ */
public Antlr4ErrorLog(@NotNull Tool tool, @NotNull Log log) { public Antlr4ErrorLog(@NotNull Tool tool, @NotNull BuildContext buildContext, @NotNull Log log) {
this.tool = tool; this.tool = tool;
this.buildContext = buildContext;
this.log = log; this.log = log;
} }
@ -87,6 +92,11 @@ public class Antlr4ErrorLog implements ANTLRToolListener {
} }
log.error(outputMsg); log.error(outputMsg);
if (message.fileName != null) {
String text = message.getMessageTemplate(false).render();
buildContext.addMessage(new File(message.fileName), message.line, message.charPosition, text, BuildContext.SEVERITY_ERROR, message.getCause());
}
} }
/** /**
@ -105,5 +115,10 @@ public class Antlr4ErrorLog implements ANTLRToolListener {
} }
log.warn(outputMsg); log.warn(outputMsg);
if (message.fileName != null) {
String text = message.getMessageTemplate(false).render();
buildContext.addMessage(new File(message.fileName), message.line, message.charPosition, text, BuildContext.SEVERITY_WARNING, message.getCause());
}
} }
} }

View File

@ -39,6 +39,7 @@ import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.logging.Log; import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.LifecyclePhase; import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.plugins.annotations.Parameter;
@ -49,11 +50,13 @@ import org.codehaus.plexus.compiler.util.scan.SimpleSourceInclusionScanner;
import org.codehaus.plexus.compiler.util.scan.SourceInclusionScanner; import org.codehaus.plexus.compiler.util.scan.SourceInclusionScanner;
import org.codehaus.plexus.compiler.util.scan.mapping.SourceMapping; import org.codehaus.plexus.compiler.util.scan.mapping.SourceMapping;
import org.codehaus.plexus.compiler.util.scan.mapping.SuffixMapping; import org.codehaus.plexus.compiler.util.scan.mapping.SuffixMapping;
import org.sonatype.plexus.build.incremental.BuildContext;
import java.io.BufferedWriter; import java.io.BufferedWriter;
import java.io.File; import java.io.File;
import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.StringWriter; import java.io.StringWriter;
import java.io.Writer; import java.io.Writer;
import java.net.URI; import java.net.URI;
@ -181,6 +184,9 @@ public class Antlr4Mojo extends AbstractMojo {
@Parameter(defaultValue = "${basedir}/src/main/antlr4/imports") @Parameter(defaultValue = "${basedir}/src/main/antlr4/imports")
private File libDirectory; private File libDirectory;
@Component
private BuildContext buildContext;
public File getSourceDirectory() { public File getSourceDirectory() {
return sourceDirectory; return sourceDirectory;
} }
@ -375,6 +381,12 @@ public class Antlr4Mojo extends AbstractMojo {
// Iterate each grammar file we were given and add it into the tool's list of // Iterate each grammar file we were given and add it into the tool's list of
// grammars to process. // grammars to process.
for (File grammarFile : grammarFiles) { for (File grammarFile : grammarFiles) {
if (!buildContext.hasDelta(grammarFile)) {
continue;
}
buildContext.removeMessages(grammarFile);
getLog().debug("Grammar file '" + grammarFile.getPath() + "' detected."); getLog().debug("Grammar file '" + grammarFile.getPath() + "' detected.");
String relPathBase = findSourceSubdir(sourceDirectory, grammarFile.getPath()); String relPathBase = findSourceSubdir(sourceDirectory, grammarFile.getPath());
@ -452,7 +464,7 @@ public class Antlr4Mojo extends AbstractMojo {
public CustomTool(String[] args) { public CustomTool(String[] args) {
super(args); super(args);
addListener(new Antlr4ErrorLog(this, getLog())); addListener(new Antlr4ErrorLog(this, buildContext, getLog()));
} }
@Override @Override
@ -486,8 +498,8 @@ public class Antlr4Mojo extends AbstractMojo {
URI relativePath = project.getBasedir().toURI().relativize(outputFile.toURI()); URI relativePath = project.getBasedir().toURI().relativize(outputFile.toURI());
getLog().debug(" Writing file: " + relativePath); getLog().debug(" Writing file: " + relativePath);
FileWriter fw = new FileWriter(outputFile); OutputStream outputStream = buildContext.newFileOutputStream(outputFile);
return new BufferedWriter(fw); return new BufferedWriter(new OutputStreamWriter(outputStream));
} }
} }
} }

View File

@ -32,6 +32,7 @@ package org.antlr.v4.tool;
import org.antlr.v4.runtime.misc.NotNull; import org.antlr.v4.runtime.misc.NotNull;
import org.antlr.v4.runtime.misc.Nullable; import org.antlr.v4.runtime.misc.Nullable;
import org.stringtemplate.v4.ST;
import java.util.Arrays; import java.util.Arrays;
public class ANTLRMessage { public class ANTLRMessage {
@ -77,6 +78,31 @@ public class ANTLRMessage {
return args; return args;
} }
public ST getMessageTemplate(boolean verbose) {
ST messageST = new ST(getErrorType().msg);
messageST.add("verbose", verbose);
Object[] args = getArgs();
for (int i=0; i<args.length; i++) {
String attr = "arg";
if ( i>0 ) attr += i + 1;
messageST.add(attr, args[i]);
}
if ( args.length<2 ) messageST.add("arg2", null); // some messages ref arg2
Throwable cause = getCause();
if ( cause!=null ) {
messageST.add("exception", cause);
messageST.add("stackTrace", cause.getStackTrace());
}
else {
messageST.add("exception", null); // avoid ST error msg
messageST.add("stackTrace", null);
}
return messageST;
}
@Nullable @Nullable
public Throwable getCause() { public Throwable getCause() {
return e; return e;

View File

@ -96,30 +96,11 @@ public class ErrorManager {
} }
public ST getMessageTemplate(ANTLRMessage msg) { public ST getMessageTemplate(ANTLRMessage msg) {
ST messageST = new ST(msg.getErrorType().msg); ST messageST = msg.getMessageTemplate(tool.longMessages);
ST locationST = getLocationFormat(); ST locationST = getLocationFormat();
ST reportST = getReportFormat(msg.getErrorType().severity); ST reportST = getReportFormat(msg.getErrorType().severity);
ST messageFormatST = getMessageFormat(); ST messageFormatST = getMessageFormat();
messageST.add("verbose", tool.longMessages);
Object[] args = msg.getArgs();
for (int i=0; i<args.length; i++) {
String attr = "arg";
if ( i>0 ) attr += i + 1;
messageST.add(attr, args[i]);
}
if ( args.length<2 ) messageST.add("arg2", null); // some messages ref arg2
Throwable cause = msg.getCause();
if ( cause!=null ) {
messageST.add("exception", cause);
messageST.add("stackTrace", cause.getStackTrace());
}
else {
messageST.add("exception", null); // avoid ST error msg
messageST.add("stackTrace", null);
}
boolean locationValid = false; boolean locationValid = false;
if (msg.line != -1) { if (msg.line != -1) {
locationST.add("line", msg.line); locationST.add("line", msg.line);