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">

			<path>

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

					<depth max="2"/>

				</fileset>

			</path>

			<sequential>

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

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

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

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

				</antcall>

			</sequential>

		</for>

	</target>


<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}"/>

		</java>

	</target>
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"/> 
</antcall>