ANT打包可执行JAR

<?xml version="1.0" encoding="UTF-8" ?>
<project name="emchat_server" default="build" basedir=".">
    <property name="src" value="${basedir}/src/main/java"/>
    <property name="resources" value="${basedir}/src/main/resources"/>
    <property name="dest" value="${basedir}/dest"/>
    <property name="jar_name" value="emchat_server.jar"/>
    <property name="jar_path" value="${basedir}/${jar_name}"/>
    <property name="libs" value="${basedir}/lib"/>


    <path id="ref" description="jar包路径">
        <fileset dir="${libs}">
            <include name="*.jar"/>
        </fileset>
    </path>

    <!-- 路径转换 -->
    <pathconvert property="mf.classpath" pathsep=" ">
        <!--mapper,对路径组合方式进行控制-->
        <mapper>
            <!--chainedmapper 作用是联合多个mapper-->
            <chainedmapper>
                <!--过滤文件,将路径去掉,只保留文件名-->
                <flattenmapper/>
                <!--过滤+转换器,将所有的文件名前面都加上一个lib,我们知道lib目录下面有jar包,
                    lib/*的作用其实是将jar包名与路径进行组合形成如:lib/google.jar这样的相对路径
                 -->
                <globmapper from="*" to="./libs/*"/>
            </chainedmapper>
        </mapper>
        <!--按照mapper定义的格式组合lib目录下面的所有jar文件,形成诸如lib/jar1.jar lib/jar2.jar的字符串-->
        <fileset dir="${libs}">
            <include name="*.jar"/>
        </fileset>
    </pathconvert>

    <echo>${mf.classpath}</echo>
    <echo>${javac1.6}</echo>

    <target name="clean">
        <delete dir="${dest}"/>
        <delete file="${jar_path}"/>
    </target>

    <target name="init" depends="clean">
        <mkdir dir="${dest}"/>
    </target>

    <target name="compile" depends="init">
        <javac srcdir="${src}" destdir="${dest}" includeantruntime="on" encoding="utf8" debug="on">
            <classpath>
                <fileset dir="${libs}">
                    <include name="*.jar"/>
                </fileset>
            </classpath>
        </javac>

        <copy todir="${dest}">
            <fileset dir="${resources}">
                <include name="**/*"/>
            </fileset>
        </copy>
    </target>

    <target name="build" depends="compile">
        <jar jarfile="${jar_path}" basedir="${dest}" includes="**/*.*">
            <manifest>
                <attribute name="Class-Path"
                           value=". ${mf.classpath}"/>
                <attribute name="Main-Class" value="com.engc.server.Server"/>
            </manifest>
        </jar>
        <!-- 清空dest文件夹 -->
        <delete includeEmptyDirs="true">
            <fileset dir="${dest}" includes="**/*" defaultexcludes="no"/>
        </delete>
        <copy todir="${dest}/libs">
            <fileset dir="${libs}">
                <include name="**/*.jar"/>
            </fileset>
        </copy>
        <move todir="${dest}">
            <file file="${jar_path}"></file>
        </move>

        <touch file="${dest}/run.bat">
        </touch>

        <echo file="${dest}/run.bat">java -jar ${jar_name}</echo>

    </target>

</project>
相关文章
相关标签/搜索