Ant使用范例

By | 2月 4, 2016

1. 设置basedir

<?xml version="1.0"?>
<project name="projectStudy" default="sayBaseDir" basedir="D:\myec">
       <target name="sayBaseDir" description="打印提示信息">
              <echo message="The base dir is: ${basedir}"/>
       </target>
</project>

2. 创建目录

<?xml version="1.0"?>
<project name="propertyStudy" default="mkdir" description="创建目录">
    <target name="mkdir">
        <mkdir dir="mkdirTest"/>
    </target>
</project>

3. 设置property

<?xml version="1.0"?>
<project name="propertyStudy" default="example">
    <property name="name" value="amigo"/>
    <property name="age" value="25"/>
    <target name="example">
        <echo message="name: ${name}, age: ${age}"/>
    </target>
</project>

4. 拷贝文件和拷贝文件夹

<?xml version="1.0"?>
<project name="propertyStudy" default="copydir">
    <target name="copyfile" description="拷贝文件">
        <copy file="antStudy.txt" tofile="copy.txt"/>
    </target>

    <target name="copydir" description="拷贝目录">
        <copy todir="../AntProject/src_copy">
              <fileset dir="src"/>
         </copy>
    </target>
</project>

5. 删除文件和删除文件夹

<?xml version="1.0"?>
<project name="propertyStudy" default="deldir">
    <target name="delfile" description="删除文件">
        <delete file="src_copy/com/cyjch/test/AntTest.java" />
    </target>

    <target name="deldir" description="删除目录">
        <delete dir="../AntProject/src_copy" />
    </target>

    <target name="delBakAndEmpty" description="删除空目录及备份目录">
        <delete includeEmptyDirs="true">
            <fileset dir="." includes="**/*.bak" />
        </delete>
    </target>
</project>

6. 编译java程序

<?xml version="1.0"?>
<project name="javacTest" default="compile" basedir=".">
    <target name="clean" description="删除编译后产生的目录及文件">
        <delete dir="build" />
    </target>

    <target name="compile" depends="clean" description="编译源文件">
        <mkdir dir="build/classes" />
        <javac srcdir="src" destdir="build/classes" />
    </target>
</project>

7. 编译并运行java程序

<?xml version="1.0"?>
<project name="javaTest" default="run" basedir=".">
    <target name="clean" description="清除目录">
        <delete dir="build" />
    </target>

    <target name="compile" depends="clean" description="编译">
        <mkdir dir="build/classes" />
        <javac srcdir="src" destdir="build/classes" />
    </target>

    <target name="run" depends="compile" description="运行">
        <java classname="AntTest">
            <classpath>
                <pathelement path="build/classes" />
            </classpath>
        </java>
    </target>
</project>

8. 生成JAR包

<?xml version="1.0"?>
<project name="javaTest" default="jar" basedir=".">
    <target name="jar" description="打JAR包">
        <jar destfile="helloworld.jar" basedir="build/classes">
            <manifest>
                <attribute name="Main-class" value="HelloWorld" />
            </manifest>
        </jar>
    </target>
</project>

9. 生成WAR包

<?xml version="1.0"?>
<!--默认为打包操作-->
<project name="structured" default="archive">
    <description>编译并打包一个应用程序</description>
    <!--在工程目录下创建输出目录:build/classes 和 dist-->
    <target name="init" description="创建目录">
        <mkdir dir="build/classes" />
        <mkdir dir="dist" />
    </target>

    <!--编译输出目录:srcdir-源目录和destdir-目标目录-->
    <target name="compile" depends="init" description="编译">
        <javac srcdir="src" destdir="build/classes" />
    </target>

    <!--打包 , depends依赖哪些target-->
    <target name="archive" depends="compile" description="打包">
        <war destfile="dist/antwebproject.war" basedir="build/classes" />
        <jar destfile="dist/project.jar" basedir="build/classes" />
    </target>

    <!--清理删除-->
    <target name="clean" depends="init" description="清理">
        <delete dir="build" />
        <delete dir="dist" />
    </target>
</project>

10. Web project打成War包

<?xml version="1.0"?>
<project name="antwebproject" default="war" basedir=".">
    <!--定义常量,任务可以引用-->
    <property name="classes" value="build/classes" />
    <property name="build" value="build" />
    <property name="lib" value="WebRoot/WEB-INF/lib" />

    <!-- 删除build路径-->
    <target name="clean">
        <delete dir="build" />
    </target>

    <!-- 建立build/classes路径,并编译class文件到build/classes路径下-->
    <target name="compile" depends="clean">
        <mkdir dir="${classes}" />
        <javac srcdir="src" destdir="${classes}" />
    </target>

    <!-- 打war包-->
    <target name="war" depends="compile">
        <war destfile="${build}/AntProject.war" webxml="WebRoot/WEB-INF/web.xml">
            <!-- 拷贝WebRoot下除了WEB-INF和META-INF的两个文件夹 , **意为:匹配0或者更多的目录-->
            <fileset dir="WebRoot" includes="**/*.jsp" />
            <!-- 拷贝lib目录下的jar包-->
            <lib dir="${lib}" />
            <!-- 拷贝build/classes下的class文件-->
            <classes dir="${classes}" />
        </war>
    </target>
</project>