Skip to content

Instantly share code, notes, and snippets.

@HerbertV
Last active December 28, 2015 05:19
Show Gist options
  • Save HerbertV/7448628 to your computer and use it in GitHub Desktop.
Save HerbertV/7448628 to your computer and use it in GitHub Desktop.
ANT snippets for concat, minfiy css and js and link replacing.
<!--
===========================================================================
Some ANT Snippets combine, minify and JS and CSS files.
===========================================================================
For deploying homepages with ANT.
Properties:
dir.src=your source folder
dir.deploy=your deploy folder
-->
<!--
===========================================================================
TASK DEFINITIONS
===========================================================================
-->
<!-- http://ant-contrib.sourceforge.net/ -->
<taskdef resource="net/sf/antcontrib/antlib.xml">
<classpath>
<pathelement location="ant-contrib/ant-contrib-1.0b3.jar"/>
</classpath>
</taskdef>
<!--
YUI Ant:
http://www.ubik-ingenierie.com/ubikwiki/index.php?title=Minifying_JS/CSS
YUI Compressor:
https://github.com/yui/yuicompressor/releases
-->
<taskdef name="yuicompress" classname="com.yahoo.platform.yui.compressor.YUICompressTask">
<classpath>
<pathelement path="antlib/YUIAnt.jar" />
<pathelement path="antlib/yuicompressor-2.4.8.jar" />
</classpath>
</taskdef>
<!--
===========================================================================
Target collect_and_concat
===========================================================================
-->
<target name="collect_and_concat"
description="combines css and js files.">
<concat destfile="min.js" fixlastline="yes">
<filelist refid="list_of_js_files_to_combine"/>
</concat>
<concat destfile="min.css" fixlastline="yes">
<filelist refid="list_of_css_files_to_combine"/>
</concat>
</target>
<!--
===========================================================================
Target minify
===========================================================================
-->
<target name="minify"
description="Minifiy all CSS and JS">
<yuicompress linebreak="300"
warn="false"
munge="yes"
preserveallsemicolons="true"
outputfolder="${dir.deploy}"
>
<fileset file="${dir.src}min.js"/>
</yuicompress>
<yuicompress linebreak="300"
warn="false"
munge="yes"
preserveallsemicolons="true"
outputfolder="${dir.deploy}"
>
<fileset file="${dir.src}min.css"/>
</yuicompress>
<delete file="${dir.src}min.css" />
<delete file="${dir.src}min.js" />
</target>
<!--
===========================================================================
Target replace_references
===========================================================================
mark in your css and js references with:
&lt;!--sourcejs--&gt;... your js links... &lt;!--sourcejsend--&gt;
&lt;!--sourcecss--&gt;... your js links... &lt;!--sourcecssend--&gt;
-->
<target name="replace_references">
<replaceregexp match="\&lt;!--sourcejs--\&gt;(.*?)\&lt;!--endsourcejs--\&gt;"
replace="\&lt;script src='min.js'\&gt;\&lt;/script\&gt;"
flags="gs">
<fileset name="${dir.deploy}*.html"/>
</replaceregexp>
<replaceregexp match="\&lt;!--sourcecss--\&gt;(.*?)\&lt;!--endsourcecss--\&gt;"
replace="\&lt;link href='min.css' rel='stylesheet'/\&gt;"
flags="gs">
<fileset file="${dir.deploy}*.html"/>
</replaceregexp>
</target>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment