这两天写了点java的东西,难免用到ant,网上找了下资料。官网上的manual不能说不好,只是查找东西实在不方便,而且简单用用ant的话,它里面的很多东西都没必要了解。最后还是找了篇博客,分享了下ant的知识,而且还是做成pdf的格式的。拿来看下:

Ant中文手册2.0.zip 来自:这里

附上我自己写的一个简单build.xml,实现功能很简单,就是清理、创建文件夹,编译,制作jar包,执行jar包:

[codesyntax lang="xml"]

<?xml version="1.0" encoding="UTF-8" ?>
<project name="JSS" default="project.build" basedir=".">
    <description>Simple Server</description>
    <!-- properties -->
    <property environment="dev" />
    <property file="build.properties" />

    <!-- global properties -->
    <property name="src.dir" value="${basedir}/src" />
    <property name="lib.dir" value="${basedir}/lib" />
    <property name="build.dir" value="${basedir}/build" />
    <property name="dist.dir" value="${basedir}/dist" />

    <!-- jcp project -->
    <property name="jcp.src.dir" value="${src.dir}/jcp" />
    <property name="jcp.classes.dir" value="${build.dir}/jcp" />
    <property name="jcp.jar.file" value="${dist.dir}/jcp.jar" />
    <property name="jcp.config.dir" value="${src.dir}/jcp/config" />
    <property name="jcp.config.dist" value="${jcp.classes.dir}/jcp/config" />

    <!-- jml project -->
    <property name="jml.src.dir" value="${src.dir}/jml" />
    <property name="jml.classes.dir" value="${build.dir}/jml" />
    <property name="jml.jar.file" value="${dist.dir}/jml.jar" />
    <property name="jml.main.class" value="jml.main.Main" />
    <property name="jml.sql.dir" value="${src.dir}/jml/sql" />
    <property name="jml.sql.dist" value="${jml.classes.dir}/jml/sql" />

    <!-- jss project -->
    <property name="jss.src.dir" value="${src.dir}/jss" />
    <property name="jss.classes.dir" value="${build.dir}/jss" />
    <property name="jss.jar.file" value="${dist.dir}/jss.jar" />
    <property name="jss.main.class" value="jss.main.Main" />
    <property name="jss.run.timer.args" value="HTTP_REQUEST_TIMER" />
    <property name="jss.run.listener.args" value="HTTP_REDIRECT_LISTENER" />
    <property name="jss.run.mem.args" value="MEMCACHED_LISTENER_TIMER" />

    <!-- set lib ref path -->
    <path id="lib.ref.path">
        <fileset dir="${lib.dir}">
            <include name="**/*.jar" />
        </fileset>
    </path>

    <!-- clear project folders -->
    <target name="clear">
        <echo level="info" message="Clearing build folder..." />
        <delete dir="${build.dir}" />
        <echo level="info" message="Clearing destination folder..." />
        <delete dir="${dist.dir}" />
        <echo level="info" message="Clearing old dependency jars..." />
        <delete file="${jcp.jar.file}" />
        <delete file="${jml.jar.file}" />
    </target>
    <!-- create project folders -->
    <target name="init" depends="clear">
        <echo level="info" message="Making build folder..." />
        <mkdir dir="${build.dir}" />
        <mkdir dir="${jcp.classes.dir}" />
        <mkdir dir="${jml.classes.dir}" />
        <mkdir dir="${jss.classes.dir}" />
        <echo level="info" message="Making destination folder..." />
        <mkdir dir="${dist.dir}" />
    </target>

    <!-- jcp compile & jar -->
    <target name="jcp.compile">
        <echo level="info" message="Compiling jcp source codes..." />
        <javac srcdir="${jcp.src.dir}" destdir="${jcp.classes.dir}" debug="on" includeAntRuntime="no">
            <classpath refid="lib.ref.path" />
        </javac>
        <echo level="info" message="Copying jcp config files..." />
        <copy todir="${jcp.config.dist}">
            <fileset dir="${jcp.config.dir}">
                <exclude name="**/*.java" />
            </fileset>
        </copy>
    </target>
    <target name="jcp.jar" depends="jcp.compile">
        <echo level="info" message="Making jcp jar file..." />
        <manifestclasspath property="jcp.jar.classpath" jarfile="${jcp.jar.file}">
            <classpath refid="lib.ref.path"/>
        </manifestclasspath>
        <jar destfile="${jcp.jar.file}" basedir="${jcp.classes.dir}">
            <manifest>
                <attribute name="Class-Path" value="${jcp.jar.classpath}"/>
            </manifest>
        </jar>
        <copy file="${jcp.jar.file}" todir="${lib.dir}" />
    </target>

    <!-- jml compile & jar -->
    <target name="jml.compile">
        <echo level="info" message="Compiling jml source codes..." />
        <javac srcdir="${jml.src.dir}" destdir="${jml.classes.dir}" debug="on" includeAntRuntime="no">
            <classpath refid="lib.ref.path" />
        </javac>
        <echo level="info" message="Copying sql files..." />
        <copy todir="${jml.sql.dist}">
            <fileset dir="${jml.sql.dir}">
                <exclude name="**/*.java" />
            </fileset>
        </copy>
    </target>
    <target name="jml.jar" depends="jml.compile">
        <echo level="info" message="Making jml jar file..." />
        <manifestclasspath property="jml.jar.classpath" jarfile="${jml.jar.file}">
            <classpath refid="lib.ref.path"/>
        </manifestclasspath>
        <jar destfile="${jml.jar.file}" basedir="${jml.classes.dir}">
            <manifest>
                <attribute name="Main-Class" value="${jml.main.class}" />
                <attribute name="Class-Path" value="${jml.jar.classpath}"/>
            </manifest>
        </jar>
        <copy file="${jml.jar.file}" todir="${lib.dir}" />
    </target>

    <!-- jss compile & jar -->
    <target name="jss.compile">
        <echo level="info" message="Compiling jss source codes..." />
        <javac srcdir="${jss.src.dir}" destdir="${jss.classes.dir}" debug="on" includeAntRuntime="no">
            <classpath refid="lib.ref.path" />
        </javac>
    </target>
    <target name="jss.jar" depends="jss.compile">
        <echo level="info" message="Making jss jar file..." />
        <manifestclasspath property="jss.jar.classpath" jarfile="${jss.jar.file}">
            <classpath refid="lib.ref.path"/>
        </manifestclasspath>
        <jar destfile="${jss.jar.file}" basedir="${jss.classes.dir}">
            <manifest>
                <attribute name="Main-Class" value="${jss.main.class}" />
                <attribute name="Class-Path" value="${jss.jar.classpath}"/>
            </manifest>
        </jar>
    </target>

    <!-- project build -->
    <target name="project.build">
        <subant failonerror="false" buildpath=".">
            <target name="init" />
            <target name="jcp.jar" />
            <target name="jml.jar" />
            <target name="jss.jar" />
        </subant>
    </target>

    <!-- jml run -->
    <target name="jml.run" depends="jml.jar">
        <java jar="${jml.jar.file}" fork="yes" />
    </target>

    <!-- jss http request timer -->
    <target name="jss.run.timer" depends="jss.jar">
        <java jar="${jss.jar.file}" fork="yes">
            <arg line="${jss.run.timer.args}" />
        </java>
    </target>
    <!-- jss http redirect listener -->
    <target name="jss.run.listener" depends="jss.jar">
        <java jar="${jss.jar.file}" fork="yes">
            <arg line="${jss.run.listener.args}" />
        </java>
    </target>
    <!-- jss memcached listener -->
    <target name="jss.run.mem" depends="jss.jar">
        <java jar="${jss.jar.file}" fork="yes">
            <arg line="${jss.run.mem.args}" />
        </java>
    </target>
</project>

[/codesyntax]

另,还有篇文章ant脚本实例写得很详细:http://java.chinaitlab.com/ANT/528799.html