Sample Ant Build File - WAR--reference

I am using the Spring SimpleFormController example to illustrate the build process. The figure below shows the structure of the web application.

All the classes inside the src directory should be compiled and placed in a separate build/classesdirectory. 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.

 
1. <target name="init">
2. <mkdir dir="build/classes"/>
3. <mkdir dir="dist" />
4. </target>

The next step is to compile all the classes in the src directory and place them in the build/classesdirectory. To do this first you need to add all the lib files inside the "WebContent/WEB-INF/lib" directory to the classpath

1. <path id="compile.classpath">
2. <fileset dir="WebContent/WEB-INF/lib">
3. <include name="*.jar"/>
4. </fileset>
5. </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.

 
1. <target name="compile" depends="init" >
2. <javac destdir="build/classes" debug="true" srcdir="src">
3. <classpath refid="compile.classpath"/>
4. </javac>
5. </target>

The path we created earlier will be refered here using the <classpath> element.

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.

 
1. <target name="war" depends="compile">
2. <war destfile="dist/AntExample.war" webxml="WebContent/WEB-INF/web.xml">
3. <fileset dir="WebContent"/>
4. <lib dir="WebContent/WEB-INF/lib"/>
5. <classes dir="build/classes"/>
6. </war>
7. </target>

You can use the clean target to clean the project. The clean target deletes the build and the dist directory. 

 
1. <target name="clean">
2. <delete dir="dist" />
3. <delete dir="build" />
4. </target>

 The complete build.xml file is shown below.

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

 

reference from:http://www.dzone.com/tutorials/java/ant/ant-sample-build-file-war-1.html

 

转载于:https://www.cnblogs.com/davidwang456/p/3725134.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值