Ant task for JavaScript compression

2 min read >

Ant task for JavaScript compression

Engineering Insights & Enterprise solutions

By compressing javascript files page loading time is significantly reduced.

Below I’ve written an ant task that can be used in a build file of a project to compress javascript source files.

Before using it, be sure you have custom_rhino.jar and ant-contrib.jar added to your classpath.

<target name="js-compress">

		<property name="input.dir" value=""/>

		<property name="output.dir" value=""/>

		<echo message="compress files from ${input.dir}" />

		<for param="file">


				<fileset dir="${input.dir}" includes="**/*.js">

					<depth max="2"/>




				<echo message="file: @{file}"/>

				<antcall target="js-file-compress">

					<param name="input" value="@{file}"/>

					<param name="output" value="@{file}"/>





<target name="js-file-compress" description="compress js files">

		<property name="input" value=""/>

		<property name="output" value=""/>

		<java jar="lib/rhino/custom_rhino.jar" fork="true">

			<arg value="-c"/>

			<arg file="${input}"/>

			<redirector output="${output}"/>


Ant task can be used as follows Supposing js deploy directory is in web/js:
<antcall target="js-compress">
<param name="input.dir" value="web/js"/>
 <param name="output.dir" value="web/js"/> 

Get in touch

We are always happy to talk





165 Splaiul Unirii, Timpuri Noi Square,
TN Office 2 building, 4th floor,
District 3, Bucharest, Romania, 030134