2

I am currently using YUI to compress JavaScript files via Ant:

<apply executable="java" parallel="false">
    <fileset dir="." includes="${build.web.dir}/js/*.js"/>
    <arg line="-jar"/>
    <arg path="yuicompressor-2.4.7.jar"/>           
    <srcfile/>
    <arg line="-o"/>
    <mapper type="glob" from="*.js" to="*-min.js"/>
    <targetfile/>      
</apply>

However the newly created *-min.js files now have newer "Last Modified" dates. This becomes a problem when I rollout the files using RSYNC which compares the last modified date to determine whether or not the file should be updated.

Ideally I would like to preserve the last modified date so the rollout doesn't update all the files unnecessarily and also overwriting newer files on the server (It has happened before).

4
  • I fail to understand. If you redeploy the app, don't you want the newer files to de deployed? What's the point in generating these files if you want to keep the previous ones?
    – JB Nizet
    Jan 31, 2012 at 17:16
  • Yes I do want the newer files deployed but I don’t know which files are newer when compression takes place therefore all files are compressed. This means that every file will have the last modified date set to the current time resulting in all files being transferred. Ideally I would like to only transfer the files that have updated and prevent files that are somehow newer on the server from being overwritten. Jan 31, 2012 at 17:43
  • transfering minified JS files shouldn't be very long, and if a file on the server is newer than the one you just built, you have a serious problem. How would you guarantee that this newer file on the server is compatible with the new deployment?
    – JB Nizet
    Jan 31, 2012 at 17:51
  • Speed is not the issue it’s more about control and knowing exactly what files have changed so you can track changes as they are deployed. I agree having a newer file on the server is a serious problem which is why preserving the modified dates allows me to see this and investigate further. Jan 31, 2012 at 17:56

2 Answers 2

1

Suggest you look into Ant selectors, most likely the depend selector. They will let you restrict the compression to only those files where the uncompressed javascript is newer than the previous compressed version, if you see what I mean.

For example, something like:

<apply executable="java" parallel="false">
    <fileset dir="." includes="${build.web.dir}/js/*.js"
                     excludes="${build.web.dir}/js/*-min.js">
        <depend targetdir=".">
            <globmapper from="*.js" to="*-min.js"/>
        </depend>
    </fileset>
    <arg line="-jar"/>
    <arg path="yuicompressor-2.4.7.jar"/>           
    <srcfile/>
    <arg line="-o"/>
    <mapper type="glob" from="*.js" to="*-min.js"/>
    <targetfile/>      
</apply>
5
  • The other problem I see with that suggestion is the files I am comparing against are on the server. When the code is deployed it gets checked out into a clean directory before being minimised and punted over to the server via RSYNC. RSYNC then checks the last modified times to determine whether or not to update the file on the server. At the moment this is pointless since all files have the current time set as their last modified dates. It's starting to look like I might need to call some external programs in order to restore the last modified date. Jan 31, 2012 at 19:09
  • How about an rsync from the server before carrying out the minimise, followed by rsync to the server of changed only. Jan 31, 2012 at 20:00
  • Nice idea which does seem possible with a few extra steps. The main issue being that when you do that second rsync update with the minimised version, the file then becomes newer than the one in the repository. On the next deploy rsync will need to avoid that file if it hasn't changed because I would want it to be flagged as a warning, that a file is newer on the server that needs investigating. If a solution isn't possible with ANT then further workarounds with rsync is possible with a slight issue that files won't be completely in sync, time wise. Jan 31, 2012 at 21:42
  • It seems to me that you need to capture information at each build time, so that on the next build you can verify that the server hasn't been independently updated. You can also then determine what has changed since the previous build. You might consider creating a tarball of build artefacts (minimised js, etc.) at each build and checking them in to your source code control. Each one with a new build number. Jan 31, 2012 at 23:27
  • 1
    Incidentally, there is a task touch that can be used to set the last modified time of a file or files using Ant. Jan 31, 2012 at 23:30
1

Thanks to @martin-clayton I was able to use the Touch Task to restore the newly created minified files to their original Last Modified dates.

The following is a parameterised ant call allowing both CSS and JS files to be easily minified:

<target name="minify-filetype" >
    <echo>Minimise all ${filetype} files</echo>
    <apply executable="java" parallel="false">
        <fileset dir="." includes="${build.web.dir}/${filetype}/*.${filetype}"/>
        <arg line="-jar"/>
        <arg path="../../../etc/ant/trunk/lib/yuicompressor-2.4.7.jar"/>            
        <srcfile/>
        <arg line="-o"/>
        <mapper type="glob" from="*.${filetype}" to="*-min.${filetype}"/>
        <targetfile/>      
    </apply>

    <echo>Convert minified files back to original Last Modified dates</echo>
    <touch>
        <fileset dir="." includes="${build.web.dir}/${filetype}/*.${filetype}"
                         excludes="${build.web.dir}/${filetype}/*-min.${filetype}"/>
        <mapper type="glob" from="*.${filetype}" to="*-min.${filetype}"/>
    </touch>

    <!-- moving *-min.js and creating *.js files (overwriting orginal and deleting *-min) -->
    <move todir="${build.web.dir}/${filetype}/" overwrite="true" preservelastmodified="true">
        <fileset dir="${build.web.dir}/${filetype}/" />
        <mapper type="glob" from="*-min.${filetype}" to="*.${filetype}"/>
    </move>
</target>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.