diff --git a/src/main/java/com/cj/jshintmojo/Mojo.java b/src/main/java/com/cj/jshintmojo/Mojo.java index 8693197..a001678 100644 --- a/src/main/java/com/cj/jshintmojo/Mojo.java +++ b/src/main/java/com/cj/jshintmojo/Mojo.java @@ -79,6 +79,11 @@ public class Mojo extends AbstractMojo { */ private String reportFile = ""; + /** + * @parameter property="customJSHint" + */ + private File customJSHint = null; + /** * @parameter expression="${jshint.version}" */ @@ -110,13 +115,21 @@ public Mojo(String options, String globals, File basedir, List directori this.reporter = reporter; this.reportFile = reportFile; } - - public void execute() throws MojoExecutionException, MojoFailureException { - getLog().info("using jshint version " + version); - final String jshintCode = getEmbeddedJshintCode(version); - - final JSHint jshint = new JSHint(jshintCode); + public void execute() throws MojoExecutionException, MojoFailureException { + final JSHint jshint; + if (customJSHint == null) { + getLog().info("using jshint version " + version); + final String jshintCode = getEmbeddedJshintCode(version); + jshint = new JSHint(jshintCode); + } else { + getLog().info("using customJSHint " + customJSHint); + try { + jshint = new JSHint(customJSHint); + } catch (IOException e) { + throw new MojoExecutionException("Could not load customJSHint", e); + } + } final Config config = readConfig(this.options, this.globals, this.configFile, this.basedir, getLog()); final Cache.Hash cacheHash = new Cache.Hash(config.options, config.globals, this.version, this.configFile, this.directories, this.excludes); diff --git a/src/main/java/com/cj/jshintmojo/jshint/JSHint.java b/src/main/java/com/cj/jshintmojo/jshint/JSHint.java index 23dd4ae..633532e 100644 --- a/src/main/java/com/cj/jshintmojo/jshint/JSHint.java +++ b/src/main/java/com/cj/jshintmojo/jshint/JSHint.java @@ -1,10 +1,13 @@ package com.cj.jshintmojo.jshint; +import java.io.File; import java.io.InputStream; +import java.io.IOException; import java.io.Serializable; import java.util.ArrayList; import java.util.List; +import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.mozilla.javascript.EcmaError; import org.mozilla.javascript.NativeArray; @@ -16,22 +19,30 @@ public class JSHint { private final Rhino rhino; + public JSHint(File customJSHint) throws IOException { + rhino = createRhino(FileUtils.readFileToString(customJSHint, "UTF-8")); + } + public JSHint(String jshintCode) { - - rhino = new Rhino(); + rhino = createRhino(resourceAsString(jshintCode)); + } + + private static Rhino createRhino(final String code) { + Rhino result = new Rhino(); try { - rhino.eval( + result.eval( "print=function(){};" + "quit=function(){};" + "arguments=[];"); - rhino.eval(commentOutTheShebang(resourceAsString(jshintCode))); + result.eval(commentOutTheShebang(code)); } catch (EcmaError e) { throw new RuntimeException("Javascript eval error:" + e.getScriptStackTrace(), e); } + return result; } - private String commentOutTheShebang(String code) { + private static String commentOutTheShebang(String code) { String minusShebang = code.startsWith("#!")?"//" + code : code; return minusShebang; }