如何在用于 javascript 和 css 的 Ant 构建脚本中使用 YUI Compressor

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16595802/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 05:16:37  来源:igfitidea点击:

How to use YUI Compressor in Ant build script for javascript and css

javascriptcssantminifyyui-compressor

提问by Michael Freake

After a couple of days searching for how to use the YUI Compressor in an Ant build script I have finally got it working. Many old examples (<2010) exist for creating an Ant task and using that within your build script, but that was overkill for me.

经过几天搜索如何在 Ant 构建脚本中使用 YUI 压缩器,我终于让它工作了。许多旧示例(<2010)用于创建 Ant 任务并在构建脚本中使用它,但这对我来说太过分了。

Many of the examples are also old and require some greater knowledge of Ant or configuring Ant tasks. The solution below is simply what was quick, easy and effective for me.

许多示例也很旧,需要更多的 Ant 知识或配置 Ant 任务。下面的解决方案对我来说是快速、简单和有效的。

回答by Michael Freake

The below was added to one of my <target>tags to have allthe javascript files in a single directory compressed. These files retain their original name. To do this for CSS simply switch the 'js' to 'css' and update the paths accordingly.

以下已添加到我的一个<target>标签中,以将所有javascript 文件压缩在一个目录中。这些文件保留其原始名称。要为 CSS 执行此操作,只需将“js”切换为“css”并相应地更新路径。

This was done with YUI Compressor 2.4.7 and I run the Ant build script it in Eclipse Juno without any changes to class paths or other modifications of settings.

这是使用 YUI Compressor 2.4.7 完成的,我在 Eclipse Juno 中运行 Ant 构建脚本,没有对类路径或其他设置进行任何更改。

<!-- Minimizing Javascript files -->
    <echo message="Compressing Javascript files at location: ${build.root}/resources/js/*.js" />
    <java jar="c:/dev/lib/yuicompressor-2.4.7/build/yuicompressor.jar" fork="true">
        <arg value="${build.root}/resources/js/*.js" /> <!-- input path for JS files -->
        <!--<arg value="-v" /> --><!-- Turn on verbose -->
        <arg value="-o" />
        <arg value="'.js$:.js'" />
        <arg value="${build.root}/resources/js/*.js" /> <!-- output path for JS files -->
        <classpath>
            <pathelement location="c:/dev/lib/yuicompressor-2.4.7/build/yuicompressor.jar"/>
        </classpath>
    </java>

Please feel free to improve this answer. The solution above works for me, but I'm no expert.

请随时改进此答案。上面的解决方案对我有用,但我不是专家。

回答by Victor Lyuboslavsky

I'm using the following solution to minify files in place since I got the FileNotFoundExceptionwith the previous answer.

自从我得到FileNotFoundException上一个答案以来,我正在使用以下解决方案来缩小文件。

To minify CSS, replace jswith cssbelow.

要缩小 CSS,请替换jscss以下内容。

<target name="compress" description="compress the JS files">
    <copy todir="temp/js" overwrite="yes">
        <fileset dir="original/js"/>
    </copy>
    <apply executable="java" parallel="false" dest="temp/js">
        <fileset dir="temp/js" includes="**/*.js" />
          <arg line="-jar"/>
          <arg path="test_lib/yuicompressor-2.4.8.jar" />
          <arg line="-v"/>
          <srcfile/>
          <arg line="-o"/>
          <mapper type="glob" from="*.js" to="*-min.js"/>
          <targetfile/>
    </apply>
    <move todir="original/js" overwrite="true">
        <fileset dir="temp/js" />
        <mapper type="glob" from="*-min.js" to="*.js"/>
    </move>
</target>

回答by PRASAD SARATH KRISHNA

I tried Victor's code. There was no temporary directory actually needed. I used this code and it worked for me.

我试过维克多的代码。实际不需要临时目录。我使用了这段代码,它对我有用。

    <apply executable="java" parallel="false" >
                <fileset dir="${build.root}/resources/js" includes="**/*.js" />
                    <arg line="-jar"/>
                    <arg path="${basedirectory}/yuicompressor-2.4.8.jar" />
                    <srcfile/>
                    <arg value="-o" />
                    <arg value="'.js$:.js'" />
                    <!-- output path for JS files -->
                    <arg value="${build.root}/resources/js/*.js" />
                    <arg line="--nomunge" />
                    <arg line="--preserve-semi" />              
            </apply>

回答by Praburam S

You Can Compress All Js Files that available in a particular folder without copying to the temp folder.

您可以压缩特定文件夹中可用的所有 Js 文件,而无需复制到临时文件夹。

<property name="js.source" value="js/combine" />    
<property name="js.target" value="js/compress" />
<fileset dir="${yuicompressor.lib}">
        <include name="yui/yuicompressor-2.4.z8.jar"/>
</fileset>
<target name="minifyjs" description="compress the JS files">
    <delete includeEmptyDirs="true">
      <fileset dir="${js.target}" includes="**/*" defaultexcludes="no"/>
    </delete>
    <apply executable="java" parallel="false" verbose="true" failonerror="yes">
        <fileset dir="${js.source}" includes="**/*.js" excludes="**/*-min.js, **/*.min.js"/>
          <arg line="-jar"/>
          <arg path="${yuicompressor.lib}" />
          <srcfile/>
          <arg line="-o"/>
          <targetfile/>
          <mapper type="glob" from="*.js" to="${js.target}/*.js"/>
          <arg line="--charset"/>
          <arg line="utf-8"/>
    </apply>
</target>

The above code is working fine for me.

上面的代码对我来说工作正常。

回答by Ian Jones

I'd use this ant task: http://code.google.com/p/yui-compressor-ant-task/or this one: https://github.com/parambirs/ant-yui-compressorwhich seems neater than apply.

我会使用这个蚂蚁任务:http: //code.google.com/p/yui-compressor-ant-task/或这个:https: //github.com/parambirs/ant-yui-compressor看起来更整洁比申请。