Minify Javascript and CSS through Apache ant

First configure apache ant in your IDE Eclipse and then create a file with extension .xml

e.g In this Example I create file Build.xml File .

This xml file is the apache ant build file through which ant commands can run although Ant commands can also run from command prompt .

The first thing to do in this file is place a root tag <project></project> and write all code inside this tag . <target> tag is the main tag in which we define our tasks that is to be performed when we run this build file .

Now start Tutorial with the help of some example :

Minify js files :

  1. download yui compresser.jar file  from this link download YUI Compresser.
  2. place it in a directory you want .

<target name=”Minify javascript File”  >
<java jar=”D:\yuicompressor-2.4.6.jar” fork=”true”>
<arg value=”Input-File-name.js” />
<arg value=”-o” />
<arg value=”OutputFile-name.js” />
</java>
</target>

place path of the YUI Compressor in java jar field .

arg are the arguments values .

For More Details Check this Ant Targets

Now Run this by right Click on build.xml file and Run As Ant Build .

Note: If you click on second Ant build Then it gives you target options These are used for sequence of execution of targets .You can also set sequence of targets From properties .

image

Now Run this by right Click on build.xml file and Run As Ant Build .

image

Now Js files are Minified .

Minify CSS Files :

<target name=”Minify css”  >
<java jar=”D:\yuicompressor-2.4.6.jar” fork=”true”>
<arg value=”Input-File.css” />
<arg value=”-o” />
<arg value=”OutPut-Files.css” />
</java>
</target>

 

Compile java Files :

<path id=”compile.classpath”>
<fileset dir=”WebContent/WEB-INF/lib”>
<include name=”*.jar”/>
</fileset>
</path>

<target name=”compile”  >
<javac destdir=”build/classes” debug=”true” srcdir=”src”>
<classpath refid=”compile.classpath”/>
</javac>
</target>

Learn More about fileSet Check out this FileSet . In this we basically give set of files that we want to use in target Attribute.

Build War File :

<target name=”init”>
<mkdir dir=”WebContent/resources/example” />
<echo>Done</echo>
</target>

<target name=”war” >

<war destfile=”WebContent/resources/example/project.war” webxml=”WebContent/WEB-INF/web.xml”>
<fileset dir=”WebContent” />
<classes dir=”build/classes”/>
</war>
</target>

First Make directory In which you want to place Your war file From Init Target .

Destfile is the war file name .

webxml is the path of the project’s xml file.

FileSet Dir is the directory that contain jar ,html, xhtml or jsp files

classes Contain Java Class files of your whole project .

2 thoughts on “Minify Javascript and CSS through Apache ant”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.