Download presentation
Presentation is loading. Please wait.
Published byDustin Glenn Modified over 9 years ago
1
Chapter 1 Introducing Ant
2
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
3
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 process For 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
4
Core concepts of Ant Build files Ant uses XML files called build files to describe how to build a project A build file contains one project Each build file describe how to build one project Very large projects may be composed of multiple smaller projects, each with its own build file Each project contains multiple targets Within the build file’s single project, you declare different targets Targets can be activities such as compiling the source, running test
5
Core concepts of Ant Target can depend on other targets When declaring a target, you can declare which targets have to be built first For example, this can ensure that the source gets compiled before the tests are run and built Targets contain tasks Inside targets, you declare what work is needed to complete that stage of the build process
6
Core concepts of Ant Tasks do the work A task is a XML element Behind each task is a Java class that performs the work described by the task’s attributes and nested data New tasks extend Ant The fact that it is easy to extend Ant with new classes is one of its core strength
7
Sample build file Ant build file build.xml Properties file build.properties
8
Properties File build.properties tomcat.home=c:/tomcat-6.0.26 application=counter Note: Do not use “\” when you set up the path for tomcat.home even for Windows box.
9
<javac srcdir="src" destdir="classes" /> Create directory structure Compile Deployment build.xml
11
C:\j2ee_class\MyCounter>ant init Buildfile: 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 SUCCESSFUL Total time: 0 seconds C:\j2ee_class\MyCounter>ant init Buildfile: C:\j2ee_class\MyCounter\build.xml init: BUILD SUCCESSFUL Total 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):
13
C:\j2ee_class\MyCounter>ant Buildfile: 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 SUCCESSFUL Total time: 0 seconds C:\j2ee_class\MyCounter>ant Buildfile: C:\j2ee_class\MyCounter\build.xml init: compile: deploy: BUILD SUCCESSFUL Total 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.
14
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
15
C:\j2ee_class\MyCounter>ant Buildfile: 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 SUCCESSFUL Total time: 1 second C:\j2ee_class\MyCounter>ant clean Buildfile: 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 SUCCESSFUL Total time: 0 seconds Then, do ant Do ant clean first
16
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
17
Enhancement of the build file If you want to be able to start tomcat from Ant, you can add the following tasks
18
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?
19
Here is what you can do to restart tomcat after copying files to tomcat 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
20
What makes Ant so special Ant is free and open source Ant makes it easy to bring developers into a project It is well-known and widely supported It 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
21
What makes Ant so special It enables continuous integration It becomes possible to have a machine rebuild and retest the application on a regular basis For example, rebuild and test when something checked in the code repository It can run inside Integrated Development Environment
22
Alternatives to Ant IDE (such as Eclipse, NetBeans, etc.) Limitations build tool provided by IDEs: Not flexible Hard to add complex operations Cannot scale well to integrate many different subprojects However, it is possible to integrate Ant with IDEs Make Original build tool for Unix/Linux
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.