使用Ant构建struts2 web工程,自动编译,打包成war 一、首先定义一些变量,如下,个人感觉有点类似makefile的写法 二、然后写几个target 三、最后直接运行target即可

c&c++语言通常使用make脚本来构建和管理自己的工程,同样java也有自己的构建工具(Ant),使用时需要写一个biuld.xml,有点类似c&c++里的makefile。

#==================== File and Directory Names ========================

app.name=AntDemo
app.version=0.1

source.home=./src
lib.home=./WebContent/WEB-INF/lib
lib.external=./lib_external
webapp.home=./WebContent
build.home=./build
dist.home=./dist

#==================== Compilation Control Options =====================

compile.debug=true
compile.deprecation=false
compile.optimize=true

二、然后写几个target

一般就clean,init,Compile,Build,Archive,具体如下

<?xml version="1.0"?>


<!-- ====================================================================== 
     Date:     June 2015
     
     Project:  Ant Demo
     
     Author:   Peter Chen
     ====================================================================== -->


    
<project name="AntDemo" default="archive" basedir=".">
    
    <description>
           a project of using ant
    </description>
    
    <property file="build.properties"/>

    <!-- ==================== Clean Target ==================================== -->

    <!--
        删除之前工程构建时产生的临时文件
    -->
    <target name="clean" description="Delete old directories and files">
        <delete dir="${dist.home}"/>
        <delete dir="${build.home}"/>
        <delete >
            <fileset dir="${source.home}" includes="**/*.class"/>
        </delete>
    </target>

    <!-- ==================== Init Target ================================== -->

    <!--
        新建build文件夹
    -->
    <target name="init" depends="clean"  description="Create build directory">
        
        <mkdir dir="${build.home}" />

    </target>
    
    <!-- ==================== Compile Target ================================== -->

    <!--
        编译源代码,将编译生成的class文件copy到${build.home}/WEB-INF/classes目录下
    -->
    <target name="compile" depends="init" description="Compile Java sources">
        
        
        <mkdir dir="${build.home}/WEB-INF/classes" />
        
        <javac srcdir="${source.home}"
               destdir="${build.home}/WEB-INF/classes"
               debug="${compile.debug}"
               deprecation="${compile.deprecation}"
               optimize="${compile.optimize}"
               source="1.7" target="1.7" includeantruntime="on">
            
            <classpath>
                <path>
                    <fileset dir="${lib.home}" />
                    <fileset dir="${lib.external}" />
                </path>
            </classpath>
        </javac>

    </target>
    
    <!-- ==================== Build Target ================================== -->

    <!--
        把非class文件拷贝到build目录下
    -->
    
    <target name="build" depends="compile" description="Copies all non Java classes to build directoy">
        <copy todir="${build.home}">
            <fileset dir="${webapp.home}" excludes="SVN,**/*.class" />
        </copy>
        <copy todir="${build.home}/WEB-INF/classes">
            <fileset dir="${source.home}" excludes="SVN,**/*.java" />
        </copy>
    </target>
    
    <!-- ==================== Archive Target ================================== -->

    <!--
         打包成war文件
    -->

    <target name="archive" depends="build" description="Create binary archive of all files in dist.home">
        
        <!-- Create war directory -->
        <mkdir dir="${dist.home}" />
        
        <!-- Create application WAR file -->
        <jar jarfile="${dist.home}/${app.name}.war" basedir="${build.home}" />

    </target>    

</project>

三、最后直接运行target即可

贴上demo的github地址:https://github.com/peterchenhdu/AntDemo/

有兴趣的可以下载下来看看,一个基于struts2的web工程,使用ant自动编译打包成war。