Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

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.

like image 682
Michael Freake Avatar asked May 16 '13 19:05

Michael Freake


3 Answers

The below was added to one of my <target> tags to have all the 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.

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.

<!-- 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.

like image 173
Michael Freake Avatar answered Oct 27 '22 10:10

Michael Freake


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

To minify CSS, replace js with css below.

<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>
like image 21
Victor Lyuboslavsky Avatar answered Oct 27 '22 10:10

Victor Lyuboslavsky


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>

like image 43
PRASAD SARATH KRISHNA Avatar answered Oct 27 '22 08:10

PRASAD SARATH KRISHNA