First look at the directory structure of the project:
Put the Java source program in the source directory, and put the compiled class file in the classes directory. The files in these two directories must be organized according to the package structure. Of course, the classes directory does not need to be created in advance. This can be done by ant. The build. xml file I wrote is stored in the hello directory:
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <project name = "hello" default = "compile" basedir = ". "> <br/> <property name =" tomcat. lib "value ="/home/Albert/applications/Apache-Tomcat-5.5.26/common/lib "/> <br/> <property name =" DeST. dir "value =" WEB-INF/classes "/> <br/> <property name =" src. dir "value =" Source "/> <br/> <target name =" init "> <br/> <mkdir dir =" $ {DeST. dir} "/> <br/> </Target> <br/> <target name =" compile "depends =" init "> <br/> <javac srcdir =" $ {SRC. dir} "destdir =" $ {DeST. dir} "> <br/> <classpath> <br/> <fileset dir =" $ {tomcat. lib} "> <br/> <include name = "*. jar "/> <br/> </fileset> <br/> <fileset dir =" WEB-INF/lib "> <br/> <include name = "*. jar "/> <br/> </fileset> <br/> </classpath> <br/> </javac> <br/> </Target> <br/> <target name = "clean"> <br/> <Delete dir = "$ {DeST. dir} "/> <br/> </Target> <br/> </Project> <br/>
In ant's Build File build. XML, the most important thing is target ). A build. xml usually contains multiple targets, defining the building process of a software project and the dependency between each step. For example, for this project, "Compilation" must be completed after the classes directory is created. In addition, ant will compare the time stamp of the source file and the corresponding class file during compilation to determine whether the source file is updated after the last compilation and whether it needs to be re-compiled. After knowing this, you can give a general explanation of the meaning of each item.
The project element at the outermost layer defines the name of the project, default build target and basic directory (basedir, current directory "."). Basedir is used to set the relative path in the file. There are some property elements that you can consider as some variable definitions for the convenience of reference.
Three target elements are defined. The first target is named init. If you want to execute a target task, you only need to execute ant in the command line. If it is omitted, ant executes the default Target defined in the project element. Mkdir is used to define a task, that is, to create the classes directory. The defined property is referenced here. Note that the method of referencing property is: "$ {DeST. dir }".
The second is the compile target. Depends = "init" indicates that it depends on init. That is, when ant finds that the init directory is not complete (when the classes directory is not created), ant will first execute the init target. The compilation task is described using the javac element in the compile object. Srcdir and destdir specify the directory of the source file and the directory of the compiled class file respectively. Although there are subdirectories in the source directory because of the package structure, ant will compile all the Java source files in the source directory and create the corresponding Package Structure directory under the classes directory, place all class files in the correct position. All of this is described using these two parameters. The classpath element is used to set the classpath during compilation, so as to reference the classes in the servlet-api.jar and Struts library. I stole a secret and included all the jar files. You can use fileset to specify the Directory and the file name to be included (with wildcards.
The last target clean is to clear the compiled files and use the delete element. In this way, after ant clean is executed in the command line, the classes directory is deleted.
Set this build. XML is placed in the project directory hello. After ant is executed in the hello directory, all the files to be compiled will be automatically compiled and placed in the classes Directory, which is very convenient.