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

≡放荡痞女 提交于 2020-01-02 01:19:26

问题


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?


回答1:


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>



回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/10454014/determine-which-file-has-an-error-when-running-yui-compressor-from-ant

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!