Chapter 1 Introducing Ant. What is ant? Ant is a build tool Automate the tasks of compiling code,...

22
Chapter 1 Introducing Ant

Transcript of Chapter 1 Introducing Ant. What is ant? Ant is a build tool Automate the tasks of compiling code,...

Page 1: 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.

Chapter 1

Introducing Ant

Page 2: 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.

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

Page 3: 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.

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

Page 4: 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.

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

Page 5: 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.

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

Page 6: 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.

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

Page 7: 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.

Sample build file

Ant build filebuild.xml

Properties filebuild.properties

Page 8: 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.

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.

Page 9: 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.

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

Page 10: 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.
Page 11: 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.

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):

Page 12: 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.
Page 13: 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.

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.

Page 14: 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.

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>

Page 15: 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.

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

Page 16: 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.

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

Page 17: 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.

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>

Page 18: 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.

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?

Page 19: 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.

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

Page 20: 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.

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

Page 21: 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.

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

Page 22: 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.

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