Ant > Sample Ant Build File - WAR
Sample Ant Build File - WAR
I am using the Spring SimpleFormController example to illustrate the build process. The figure below shows the structure of the web application.
我用的是Spring SimpleFormController example 来阐明build处理过程. Web应用结构配置如下:
All the classes inside the src directory should be compiled and placed in a separate build/classes directory. The created war file will be placed inside the dist directory.
So first we create the build/classes and the dist directory. The init target does this job.
编译后src下的所有的类文件应该放到单独的build/classes目录中.war文件放在dist目录里.所以第一步我们要创建build/classes和dist目录. init target来做这件事情.
<target name="init"> <mkdir dir="build/classes"/> <mkdir dir="dist" /> </target>
The next step is to compile all the classes in the src directory and place them in the build/classes directory. To do this first you need to add all the lib files inside the "WebContent/WEB-INF/lib" directory to the classpath.
下一步是编译src目录下的所有类.并把它们放到build/classes目录中.前提是你需要把WebContent/WEB-INF/lib 所有lib文件添加到classpath中.
<path id="compile.classpath"> <fileset dir="WebContent/WEB-INF/lib"> <include name="*.jar"/> </fileset> </path>
The target compile uses the javac task to compile the java classes and it depends on the target init, because only when you have the directory ready you can place the classes inside them.
compile target 用的是javac 任务编译java类并且依赖init target.因为只有目录已经创建了才能将class放进去.
<target name="compile" depends="init" > <javac destdir="build/classes" debug="true" srcdir="src"> <classpath refid="compile.classpath"/> </javac> </target>
The path we created earlier will be refered here using the <classpath> element.
前面已经创建好了path.在这里引用<classpath>元素
Now we can create the war file using the war task. To create the war file you need to specify the web directory, lib directory and the classes directory. The destfile attribute specifies the war file location and the webxml attribute specifies the web.xml file location.
现在我们可以用war任务创建war文件.创建war文件你需要指定web目录,lib目录和classes目录.destfile 属性指定war文件的位置.webxml属性指定web.xml文件的位置.
<target name="war" depends="compile"> <war destfile="dist/AntExample.war" webxml="WebContent/WEB-INF/web.xml"> <fileset dir="WebContent"/> <lib dir="WebContent/WEB-INF/lib"/> <classes dir="build/classes"/> </war> </target>
You can use the clean target to clean the project. The clean target deletes the build and the dist directory.
你可以用clean target 来clean工程.clean target 删除build和dist目录.
<target name="clean"> <delete dir="dist" /> <delete dir="build" /> </target>
The complete build.xml file is shown below.
完整的build.xml文件如下.
<?xml version="1.0" ?> <project name="AntExample1" default="war"> <path id="compile.classpath"> <fileset dir="WebContent/WEB-INF/lib"> <include name="*.jar"/> </fileset> </path> <target name="init"> <mkdir dir="build/classes"/> <mkdir dir="dist" /> </target> <target name="compile" depends="init" > <javac destdir="build/classes" debug="true" srcdir="src"> <classpath refid="compile.classpath"/> </javac> </target> <target name="war" depends="compile"> <war destfile="dist/AntExample.war" webxml="WebContent/WEB-INF/web.xml"> <fileset dir="WebContent"/> <lib dir="WebContent/WEB-INF/lib"/> <classes dir="build/classes"/> </war> </target> <target name="clean"> <delete dir="dist" /> <delete dir="build" /> </target> </project>You can download the build file here.
Build file :
Download
|
Project :
Download
|