diff --git a/src/main/java/com/cj/jshintmojo/Mojo.java b/src/main/java/com/cj/jshintmojo/Mojo.java index 2dd9024..4793d17 100644 --- a/src/main/java/com/cj/jshintmojo/Mojo.java +++ b/src/main/java/com/cj/jshintmojo/Mojo.java @@ -87,6 +87,11 @@ public class Mojo extends AbstractMojo { */ private String ignoreFile = ""; + /** + * @parameter property="customJSHint" + */ + private File customJSHint = null; + /** * @parameter property="jshint.version" */ @@ -119,13 +124,21 @@ public Mojo(String options, String globals, File basedir, List directori this.reportFile = reportFile; this.ignoreFile = ignoreFile; } - - 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()); if (this.excludes.isEmpty() || (this.ignoreFile != null && !this.ignoreFile.isEmpty())) { diff --git a/src/main/java/com/cj/jshintmojo/jshint/JSHint.java b/src/main/java/com/cj/jshintmojo/jshint/JSHint.java index f0dc9e9..2dd0978 100644 --- a/src/main/java/com/cj/jshintmojo/jshint/JSHint.java +++ b/src/main/java/com/cj/jshintmojo/jshint/JSHint.java @@ -1,37 +1,48 @@ package com.cj.jshintmojo.jshint; -import com.cj.jshintmojo.util.Rhino; +import java.io.File; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.CharEncoding; import org.mozilla.javascript.EcmaError; import org.mozilla.javascript.NativeArray; +import java.io.IOException; import org.mozilla.javascript.NativeObject; import java.io.InputStream; import java.io.Serializable; import java.util.ArrayList; import java.util.List; +import com.cj.jshintmojo.util.Rhino; +import org.apache.commons.io.FileUtils; public class JSHint { private final Rhino rhino; - public JSHint (String jshintCode) { + public JSHint(File customJSHint) throws IOException { + rhino = createRhino(FileUtils.readFileToString(customJSHint, "UTF-8")); + } + + public JSHint(String jshintCode) { + rhino = createRhino(resourceAsString(jshintCode)); + } - rhino = new Rhino (); + private static Rhino createRhino(final String code) { + Rhino result = new Rhino(); try { - rhino.eval ( + result.eval( "print=function(){};" + - "quit=function(){};" + - "arguments=[];"); + "quit=function(){};" + + "arguments=[];"); - rhino.eval (commentOutTheShebang (resourceAsString (jshintCode))); + result.eval(commentOutTheShebang(code)); } catch (EcmaError e) { - throw new RuntimeException ("Javascript eval error:" + e.getScriptStackTrace (), 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; }