9

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.

Michael Freake
  • 1,197
  • 2
  • 14
  • 35

5 Answers5

18

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.

Michael Freake
  • 1,197
  • 2
  • 14
  • 35
  • How to compress all .js file into one .js file? ? because above script compress .js files into their own file... – Simply Innovative May 25 '13 at 09:50
  • 2
    It gives FileNotFoundException for - /home/shwetanka/projects/webapp/webapp/content/js/mobile/*.js though path exists. – Shwetanka Sep 11 '13 at 07:59
  • @Shwetanka If you have a question you should create your own question thread and refer to this answer indicating the problem. – Michael Freake Sep 12 '13 at 14:52
  • I was luckily/unluckily given my ant script however after a while of working on the project, the yui compressor is filing the build of the project due to some js file. Is there a way to show/log the culprit file? – Dark Star1 Oct 02 '13 at 08:32
  • @DarkStar1 unfortunately I do not know of any log option that shows you the file name of the file that is currently being compressed. You could try running your ant build and explicitly specifying the name of the javascript file instead of '*.js' as shown above. – Michael Freake Oct 03 '13 at 21:31
  • classpath seems to be a bit superfluous, otherwise it looks fine – Niek Oct 22 '13 at 09:07
  • Getting `java.io.FileNotFoundException: /var/lib/jenkins/workspace/register/public/js/*.js (No such file or directory)` with YUI Compressor 2.4.7 on Ubuntu. – Tobi G. Feb 27 '17 at 12:36
  • How should I tweak the provided solution to exclude the already available '".min.js"' file from getting compressed again. I tried adding 'excludes' attribute with value as '"*/.min.js"' to '' but that does not seem to work. – Rajasri.J Aug 08 '17 at 12:25
  • Hey, I'm not sure of the answer to your question, but I'm wondering if you are experiencing any issue cause by an attempt to compress the file further? – Michael Freake Aug 08 '17 at 23:53
5

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>
Victor Lyuboslavsky
  • 9,882
  • 25
  • 87
  • 134
  • I was having the same problem with an Ant script based on the one from the html5boilerplate project. Your solution of going to a temp directory first seems to have fixed it. – Barrie Jul 08 '14 at 06:59
2

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>

  • How should I tweak the provided solution to exclude the already available '".min.js"' file from getting compressed again. I tried adding 'excludes' attribute with value as '"*/.min.js"' as well as "**/min*" to '' but that does not seem to work. – Rajasri.J Aug 08 '17 at 12:54
  • It is "exclude" and not "excludes". – PRASAD SARATH KRISHNA Aug 08 '17 at 14:18
0

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

Ian Jones
  • 1,998
  • 1
  • 17
  • 17
0

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

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

Praburam S
  • 155
  • 2
  • 9