1. 程式人生 > >ant的安裝和使用

ant的安裝和使用

文件 環境 odi 3.1 home rgs package default name

1.ant的安裝

  1.1 添加環境變量:ANT_HOME=D:\software\ant\apache-ant-1.10.1

    在path中添加:%ANT_HOME%\bin

  1.2 測試是否安裝成功

    在cmd中輸入ant,如果出現如下提示表示安裝成功

    技術分享圖片

2.定義簡單的build.xml

  2.1 創建HelloWord.java

package test;
public class HelloWorld{
    public static void main(String[] args){
        System.out.println("hello world!");
    }
}

  2.2 創建build.xml

<?xml version="1.0" encoding="UTF-8" ?>

<!-- 定義project節點,和默認運行的target,和工作根目錄 -->
<project name="HelloWorld" default="run" basedir=".">

    <!-- 定義變量 -->
    <property name="src" value="src"/>
    <property name="dest" value="classes"/>
    <
property name="hello_jar" value="hello.jar"/> <!-- 定義target --> <target name="init"> <!-- 創建目錄 --> <mkdir dir="${dest}"/> </target> <!-- depends:依賴的target --> <target name="compile" depends="init"> <!-- 將srcdir目錄中的文件進行編譯,並將編譯後的文件放入到destdir目錄中
--> <javac srcdir="${src}" destdir="${dest}"/> </target> <target name="build" depends="compile"> <!-- 將basedir目錄中的文件打成jar包 --> <jar jarfile="${hello_jar}" basedir="${dest}"/> </target> <target name="run" depends="build"> <!-- 運行classname --> <java classname="test.HelloWorld" classpath="${hello_jar}"/> </target> <target name="clean"> <!-- 刪除dir目錄和file文件 --> <delete dir="${dest}"/> <delete file="${hello_jar}"/> </target> <target name="rerun" depends="clean,run"> <!-- 運行clean和run target --> <ant target="clean"/> <ant target="run"/> </target> </project>

3. 合並多個build.xml

  3.1 假設下邊有三個小組,每個小組負責一部分,他們每個下面都有src和build.xml

  3.2 創建一個總的build.xml

<?xml version="1.0" encoding="UTF-8"?>
<project name="main" default="build" basedir=".">
    <property name="bin" value="${basedir}\bin"/>
    <property name="src1" value="${basedir}\src1"/>
    <property name="src2" value="${basedir}\src2"/>
    <property name="src3" value="${basedir}\src3"/>
    <target name="init">
        <mkdir dir="${bin}"/>
    </target>
    <target name="run">
        <!-- 分別運行各個目錄下的target -->
        <ant dir="${src1}" target="run"/>
        <ant dir="${src2}" target="run"/>
        <ant dir="${src3}" target="run"/>
    </target>
    <target name="clean">
        <ant dir="${src1}" target="clean"/>
        <ant dir="${src2}" target="clean"/>
        <ant dir="${src3}" target="clean"/>
    </target>
    <target name="call">
        <ant dir="${src1}" target="build"/>
        <ant dir="${src2}" target="build"/>
        <ant dir="${src3}" target="build"/>
    </target>
    <target name="build" depends="init,call">
        <!-- 復制指定的文件到todir -->
        <copy todir="${bin}">
            <fileset dir="${src1}">
                <include name="*.jar"/>
            </fileset>
            <fileset dir="${src2}">
                <include name="*.jar"/>
            </fileset>
            <fileset dir="${src3}">
                <include name="*.jar"/>
            </fileset>
        </copy>
    </target>
    <target name="rebuild" depends="build,clean">
        <ant target="clean"/>
        <ant target="build"/>
    </target>
</project>

3 使用properties文件配置屬性,和公共xml

  3.1 創建all.properties設置變量

src1=D:\\software\\ant\\test\\test3\\src1
src2=D:\\software\\ant\\test\\test3\\src2
src3=D:\\software\\ant\\test\\test3\\src3

  3.2 創建include.xml,設置公共的變量和target

<?xml version="1.0" encoding="UTF-8"?>
<property name="src" value="src"/>
<property name="dest" value="classes"/>
<target name="test">
    <ant target="run"/>
</target>

  3.3 在總build中使用使用all.properties設置變量

<?xml version="1.0" encoding="UTF-8"?>
<project name="main" default="build" basedir=".">
    <!-- 讀取配置文件中的變量 -->
    <property file="all.properties"/>
    <property name="bin" value="${basedir}\bin"/>
    <target name="init">
        <mkdir dir="${bin}"/>
    </target>
    <target name="run">
        <ant dir="${src1}" target="run"/>
        <ant dir="${src2}" target="run"/>
        <ant dir="${src3}" target="run"/>
    </target>
    <target name="clean">
        <ant dir="${src1}" target="clean"/>
        <ant dir="${src2}" target="clean"/>
        <ant dir="${src3}" target="clean"/>
    </target>
    <target name="call">
        <ant dir="${src1}" target="build"/>
        <ant dir="${src2}" target="build"/>
        <ant dir="${src3}" target="build"/>
    </target>
    <target name="build" depends="init,call">
        <copy todir="${bin}">
            <fileset dir="${src1}">
                <include name="*.jar"/>
            </fileset>
            <fileset dir="${src2}">
                <include name="*.jar"/>
            </fileset>
            <fileset dir="${src3}">
                <include name="*.jar"/>
            </fileset>
        </copy>
    </target>

    <target name="rebuild" depends="build,clean">
        <ant target="clean"/>
        <ant target="build"/>
    </target>

    <target name="test">
        <ant dir="${src1}" target="test"/>
        <ant dir="${src2}" target="test"/>
        <ant dir="${src3}" target="test"/>
    </target>
</project>

  3.4 在每個小組的build.xml中引用include.xml的變量和target

<?xml version="1.0" encoding="UTF-8" ?>

<!-- 引入外部的xml,在本xml中就可以使用引入的xml中的變量和target -->
<!DOCTYPE project[
<!ENTITY share-variable SYSTEM "file:../include.xml">
]>

<project name="HelloWorld" default="run" basedir=".">
    <!-- 使用變量 -->
    &share-variable;

    <!-- 這兩個變量在公用的xml中已經定義,使用&share-variable就可直接使用
        <property name="src" value="src"/>
        <property name="dest" value="classes"/>
    -->
    <property name="hello_jar" value="hello1.jar"/>
    <target name="init">
        <mkdir dir="${dest}"/>
    </target>
    <target name="compile" depends="init">
        <javac srcdir="${src}" destdir="${dest}"/>
    </target>
    <target name="build" depends="compile">
        <jar jarfile="${hello_jar}" basedir="${dest}"/>
    </target>
    <target name="run" depends="build">
        <java classname="test.HelloWorld" classpath="${hello_jar}"/>
    </target>
    <target name="clean">
        <delete dir="${dest}"/>
        <delete file="${hello_jar}"/>
    </target>
    <target name="rerun" depends="clean,run">
        <ant target="clean"/>
        <ant target="run"/>
    </target>
</project>

ant的安裝和使用