Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine which file has an error when running YUI compressor from Ant

We compress our javascript (and css files) with YUI compressor during our ant build task running on our Jenkins CI Server. However, it is very difficult to determine which js files YUI compressor is having errors with. We see a bunch of things like:

[minify-js] [ERROR] 3:35:unterminated string literal
[minify-js] 
[minify-js] [ERROR] 3:35:syntax error
[minify-js] 
[minify-js] [ERROR] 4:8:syntax error
[minify-js] 
[minify-js] [ERROR] 1:0:Compilation produced 3 syntax errors.
[minify-js] org.mozilla.javascript.EvaluatorException: Compilation produced 3 syntax errors.
[minify-js]     at com.yahoo.platform.yui.compressor.YUICompressor$1.runtimeError(YUICompressor.java:135)
[minify-js]     at org.mozilla.javascript.Parser.parse(Parser.java:410)
[minify-js]     at org.mozilla.javascript.Parser.parse(Parser.java:355)
[minify-js]     at com.yahoo.platform.yui.compressor.JavaScriptCompressor.parse(JavaScriptCompressor.java:312)
[minify-js]     at com.yahoo.platform.yui.compressor.JavaScriptCompressor.(JavaScriptCompressor.java:533)
[minify-js]     at com.yahoo.platform.yui.compressor.YUICompressor.main(YUICompressor.java:112)
[minify-js]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[minify-js]     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
[minify-js]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[minify-js]     at java.lang.reflect.Method.invoke(Method.java:616)
[minify-js]     at com.yahoo.platform.yui.compressor.Bootstrap.main(Bootstrap.java:20)
[minify-js] Result: 2

in the output, but I have no idea which of the hundreds of JS files the error is coming from. Our ant task looks like:

<target name="minify-js">
    <apply executable="yuicompressor" parallel="false" dest="${global.dir}/" taskname="minify-js" force="true">
        <fileset dir="${global.dir}/" includes="**/*.js">
            <exclude name="*.min.js" />
        </fileset>
        <arg value="--type=js" />
        <srcfile />
        <arg value="-o" />
        <targetfile />
        <mapper type="identity" />
    </apply>
</target>

Not being an expert on Ant or YUI compressor, is there something we can do so that the filename where there error is happening is output somewhere?

like image 687
Kris Erickson Avatar asked May 04 '12 18:05

Kris Erickson


3 Answers

I don't know how the yuicompressor works, I assume that it works on one file at a time.

If this is true, you can do it with for from ant-contrib. You would need to install ant-contrib prior.

<taskdef resource="net/sf/antcontrib/antcontrib.properties" classpath="${global.dir}/bin_data/ant-contrib-0.6.jar"/>
<for param="file">
  <path>
    <fileset dir="${global.dir}/" includes="**/*.js">
      <exclude name="*.min.js" />
    </fileset>
  </path>
  <sequential>
    <echo>youcompressor for @{file}</echo> <!-- Will output each file and help debugging -->
    <exec executable="yuicompressor"> <!-- I took the args from the official documentation-->
      <arg value="--type=js" />
      <arg value="-o" />
      <arg value="'.js$:-min.js'" />
      <arg value="@{file}" />
    </exec>
  </sequential>
</for>
like image 199
oers Avatar answered Oct 19 '22 21:10

oers


Try using this option:

-v, --verbose Display informational messages and warnings.

There is a nice entry in the documentation for a cases like yours:

Don't hesitate to use the -v option. Although not a replacement for JSLint, it will output some helpful hints when it senses that something might be wrong with your code.

like image 20
Łukasz Rżanek Avatar answered Oct 19 '22 20:10

Łukasz Rżanek


Did you try

<apply … verbose="true">

Whether to print a summary after execution or not. Since Ant 1.6.

Ideally it would print a statement before attempting to execute on the file, but having a summary afterwards at least lets you see the last successful file, which should point you to the broken file as the filesets are usually alphanumerically sequenced.

like image 2
Synesso Avatar answered Oct 19 '22 19:10

Synesso