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

Tags: javascript,css,ant,minify,yui-compressor

Problem :

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.

Solution :

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 -->
            <pathelement location="c:/dev/lib/yuicompressor-2.4.7/build/yuicompressor.jar"/>

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

    CSS Howto..

    How to make an image hover over another?

    How to host html files like css and javascript files

    In firefox, how to change color of text in a treecell using javascript

    How to remove CSS Class in aspx pages source codes using Regex?

    How can my css div with the display flex property be alignd to the right when it's wrapping?

    How to create change image option on mousehover like in gmail account?

    How this switch button work by CSS?

    How to style author id in WordPress?

    How to save game information PHP/HTML/CSS/JS [closed]

    How to conditionally add css properties in jquery (coffeescript)

    How to apply css to LinkButton?

    How do I get started with jQuery? [closed]

    How do I center bootstrap's carousel? Not the images, the carousel itself

    How can I stop a CSS keyframe animation?

    3x3 CSS grid: how to make center div the only one with a fixed height/width?

    How to position contents according to zoom level?

    How to make css transform-origin property work in IE7 and IE8?

    How to use a custom toggle switch in jquery mobile

    How to give style to iframed page content using parent pages css?

    ASP.Net MVC: How to use razor variable in CSS file

    Third-Level Menu Items Are Always Showing

    How do I Add border to text in inputfield

    How to display a line between the items of an unorderer list that have random number of li every time

    How can I style a JavaFX menu and its items in CSS?

    Loop in Sass @for how to use $#{value}#{$i} correctly?

    How to give cross browser transparency to element's background only?

    How to prevent line breaks in list items using CSS

    How to force a div to scale at 100% width of the current windows Browser

    Showing responsive menu link that is not shown in regular navigation menu

    How to avoid display block for a particular div element using css