Chapter 1 Introducing Ant. What is ant? Ant is a build tool Automate the tasks of compiling code,...
-
Upload
dustin-glenn -
Category
Documents
-
view
228 -
download
0
Transcript of Chapter 1 Introducing Ant. What is ant? Ant is a build tool Automate the tasks of compiling code,...
Chapter 1
Introducing Ant
What is ant?
Ant is a build tool Automate the tasks of compiling code, running test, and
packaging the results for redistribution It is written in Java Cross-platform and extensible Has an XML syntax
The original purpose of ant was to make it easier for people to compile Tomcat on different platforms
Soon it spread to other open source projects, and trickled out into helping Java developers in general
Software build process
Software build process is a means of going from your source – code and document – to the product you actually deliver
If you have a software project, you have a build processFor example
“hit the compile button on the IDE”“Using command line javac to compile your source code”
The above examples are not automcated
With Ant, you can delegate the work to the machine
Core concepts of AntBuild files
Ant uses XML files called build files to describe how to build a project
A build file contains one projectEach build file describe how to build one projectVery large projects may be composed of multiple smaller
projects, each with its own build file
Each project contains multiple targetsWithin the build file’s single project, you declare different
targetsTargets can be activities such as compiling the source,
running test
Core concepts of Ant
Target can depend on other targetsWhen declaring a target, you can declare which
targets have to be built firstFor example, this can ensure that the source gets
compiled before the tests are run and built
Targets contain tasksInside targets, you declare what work is needed
to complete that stage of the build process
Core concepts of Ant
Tasks do the workA task is a XML elementBehind each task is a Java class that performs
the work described by the task’s attributes and nested data
New tasks extend AntThe fact that it is easy to extend Ant with new
classes is one of its core strength
Sample build file
Ant build filebuild.xml
Properties filebuild.properties
Properties File build.properties
tomcat.home=c:/tomcat-6.0.26application=counter
Note: Do not use “\” when you set up the path for tomcat.home even for Windows box.
<?xml version="1.0" ?>
<project name="myproject" default="deploy">
<!-- ==== load properties from a properties file ==== -->
<property file="build.properties"/>
<!-- ==== create directories for development environment and deployment environment ==== -->
<target name="init">
<mkdir dir="etc" />
<mkdir dir="classes" />
<mkdir dir="src" />
<mkdir dir="web" />
<mkdir dir="${tomcat.home}/webapps/${application}" />
<mkdir dir="${tomcat.home}/webapps/${application}/WEB-INF" />
<mkdir dir="${tomcat.home}/webapps/${application}/WEB-INF/classes" />
</target>
<!-- ==== compile the java source ==== -->
<target name="compile" depends="init">
<javac srcdir="src"
destdir="classes" />
</target>
<!-- ==== deploy the files to Tomcat ==== -->
<target name="deploy" depends="compile">
<copy todir="${tomcat.home}/webapps/${application}/">
<fileset dir="web">
<include name="**/*.*" />
</fileset>
</copy>
<copy file="etc/web.xml" todir="${tomcat.home}/webapps/${application}/WEB-INF" />
<copy todir="${tomcat.home}/webapps/${application}/WEB-INF/classes">
<fileset dir="classes">
<include name="**/*.*" />
</fileset>
</copy>
</target>
</project>
Create directory structure
Compile
Deployment
build.xml
C:\j2ee_class\MyCounter>ant initBuildfile: C:\j2ee_class\MyCounter\build.xml
init: [mkdir] Created dir: C:\j2ee_class\MyCounter\etc [mkdir] Created dir: C:\j2ee_class\MyCounter\classes [mkdir] Created dir: C:\j2ee_class\MyCounter\src [mkdir] Created dir: C:\j2ee_class\MyCounter\web [mkdir] Created dir: c:\tomcat-6.0.26\webapps\counter [mkdir] Created dir: c:\tomcat-6.0.26\webapps\counter\WEB-INF [mkdir] Created dir: c:\tomcat-6.0.26\webapps\counter\WEB-INF\classes
BUILD SUCCESSFULTotal time: 0 seconds
C:\j2ee_class\MyCounter>ant initBuildfile: C:\j2ee_class\MyCounter\build.xml
init:
BUILD SUCCESSFULTotal time: 0 seconds
The first time when ant init was run:
If you run ant init again immediately after the first run (because the directories were already created, so nothing happened):
C:\j2ee_class\MyCounter>antBuildfile: C:\j2ee_class\MyCounter\build.xml
init:
compile: [javac] Compiling 1 source file to C:\j2ee_class\MyCounter\classes
deploy: [copy] Copying 1 file to c:\tomcat-6.0.26\webapps\counter\WEB-INF [copy] Copying 1 file to c:\tomcat-6.0.26\webapps\counter\WEB-INF\classes
BUILD SUCCESSFULTotal time: 0 seconds
C:\j2ee_class\MyCounter>antBuildfile: C:\j2ee_class\MyCounter\build.xml
init:
compile:
deploy:
BUILD SUCCESSFULTotal time: 0 seconds
The following figure shows what happens when ant was run the first time (You can type ant deploy also. But since deploy is default target, it is not necessary to use ant deploy). Also since init target was run previously, so nothing happens for init target.
If you type ant (or ant deploy) immediately after that, you will see nothing happens this time.
Enhancement of the build file
You can add a clean target if you want to have a clean build, that is, remove every file generated from compilation and every file deployed to Tomcat
<!-- ==== clean (for clean build) ==== -->
<target name="clean" depends="init">
<delete dir="classes" />
<delete dir="${tomcat.home}/webapps/${application}" />
</target>
C:\j2ee_class\MyCounter>antBuildfile: C:\j2ee_class\MyCounter\build.xml
init: [mkdir] Created dir: C:\j2ee_class\MyCounter\classes [mkdir] Created dir: c:\tomcat-6.0.26\webapps\counter [mkdir] Created dir: c:\tomcat-6.0.26\webapps\counter\WEB-INF [mkdir] Created dir: c:\tomcat-6.0.26\webapps\counter\WEB-INF\classes
compile: [javac] Compiling 1 source file to C:\j2ee_class\MyCounter\classes
deploy: [copy] Copying 1 file to c:\tomcat-6.0.26\webapps\counter\WEB-INF [copy] Copying 1 file to c:\tomcat-6.0.26\webapps\counter\WEB-INF\classes
BUILD SUCCESSFULTotal time: 1 second
C:\j2ee_class\MyCounter>ant cleanBuildfile: C:\j2ee_class\MyCounter\build.xml
clean: [delete] Deleting directory C:\j2ee_class\MyCounter\classes [delete] Deleting directory c:\tomcat-6.0.26\webapps\counter
BUILD SUCCESSFULTotal time: 0 seconds
Then, do ant
Do ant clean first
Enhancement of the build file
Why you may want a clean build?After Ant builds your project
if you do not change your source files, ant will not compile your source file (such as java source doe) again.
if you did not change your source files, copy task will not copy files to its destination (since there is no change)
In case you have some hard- to-find problem, or just want to rebuild everything cleanly, you can write a target for that purpose
Enhancement of the build file
If you want to be able to start tomcat from Ant, you can add the following tasks
<!-- ==== start tomcat ==== --><target name="tomcat-start">
<java jar="${tomcat.home}/bin/bootstrap.jar" fork="true"> <jvmarg value="-Dcatalina.home=${tomcat.home}"/> </java> </target>
<!-- ==== stop tomcat ==== --> <target name="tomcat-stop"> <java jar="${tomcat.home}/bin/bootstrap.jar" fork="true"> <jvmarg value="-Dcatalina.home=${tomcat.home}"/> <arg line="stop"/> </java> </target>
Enhancement of the build file
Question:
How about if you want to be able copy your class files along with other html, web.xml files to tomcat and then automatically stop and start tomcat?
Here is what you can do to restart tomcat after copying files to tomcat<!-- ==== deploy the files to Tomcat ==== -->
<target name="deploy" depends="compile">
<copy todir="${tomcat.home}/${application}/">
<fileset dir="web">
<include name="**/*.*" />
</fileset>
</copy>
<copy file="etc/web.xml" todir="${tomcat.home}/${application}/WEB-INF" />
<copy todir="${tomcat.home}/${application}/WEB-INF/classes">
<fileset dir="classes">
<include name="**/*.*" />
</fileset>
</copy>
<java jar="${tomcat.home}/bin/bootstrap.jar" fork="true">
<jvmarg value="-Dcatalina.home=${tomcat.home}"/>
<arg line="stop"/>
</java>
<java jar="${tomcat.home}/bin/bootstrap.jar" fork="true">
<jvmarg value="-Dcatalina.home=${tomcat.home}"/>
</java>
</target>
Please be noted that it may not always be desirable to restart Tomcat inside the target deploy since some static files deployed like HMTL files do not need tomcat to be restarted
Stop tomcat
Start tomcat
What makes Ant so special
Ant is free and open sourceAnt makes it easy to bring developers into a
projectIt is well-known and widely supportedIt integrates testing into the build processes
Ant let anyone who writes test integrate tests into the build process
Ant build file can mandate that the unit tests must all pass before the web application is deployed
What makes Ant so special
It enables continuous integrationIt becomes possible to have a machine rebuild
and retest the application on a regular basisFor example, rebuild and test when something
checked in the code repository
It can run inside Integrated Development Environment
Alternatives to Ant
IDE (such as Eclipse, NetBeans, etc.)Limitations build tool provided by IDEs:
Not flexibleHard to add complex operationsCannot scale well to integrate many different
subprojects
However, it is possible to integrate Ant with IDEs
MakeOriginal build tool for Unix/Linux