Search code examples
javajavascriptmavenyui

YUI compressor compress and replace on build


I was trying to find the js/css compressing tool for the maven project, so I found this thread - Maven Javascript Compressor

It works all fine, but there is one issue I'm still trying to figure out.

When you don't set the suffix the yui, it sets *-min by default and creates a new file right by the old one. So, for example if I had scripts.js after the maven build with that plugin I will have both scripts.js and scripts-min.js files. I need just the *-min file. If I set the <nosuffix> to true, it sais that the compression was successful, but it doesn't override the file.

Is there a way I could have only the compressed files with the same name?


Solution

  • Ended up writing this ant script for the maven ant plugin:

    <delete includeEmptyDirs="false">
        <fileset dir="${basedir}/target/${project.build.finalName}/resources/js" excludes="**/*-min.js" />          
    </delete>
    <move todir="${basedir}/target/${project.build.finalName}/resources/js" includeemptydirs="false">
        <fileset dir="${basedir}/target/${project.build.finalName}/resources/js" />
        <mapper type="glob" from="*-min.js" to="*.js" />
    </move>
    
    <delete includeEmptyDirs="false">
    <fileset dir="${basedir}/target/${project.build.finalName}/resources/css"
        excludes="**/*-min.css, **/*.ttf, **/*.png, **/*.jpg" />
    </delete>
    <move todir="${basedir}/target/${project.build.finalName}/resources/css" includeemptydirs="false">
        <fileset dir="${basedir}/target/${project.build.finalName}/resources/css" />
        <mapper type="glob" from="*-min.css" to="*.css" />
    </move>
    

    Brief explanation: the script first deletes the old not-compressed files and then renames the *-min.js and *-min.css files to simply *.js and *.css

    Works fine, just tested. Also, if you have some different files in the /js or /css folders (not .js or .css), make sure you don't have them deleted and put those extentions in the exclude property.