关闭

ant 打可执行jar的build.xml

标签: jarantdeleteincludepathjava
5468人阅读 评论(0) 收藏 举报
分类:
网上有很多打可执行jar包的文章,但是没有说明如何让打的可执行jar包可以调用外部库。

先说一下我的目录结构
 |build.xml
|etc
|doc
|src -- com-- dyoldfish -- parser
|
|lib
|bin
|dest


下面的build.xml的说明

方法1、把别的jar包库,解压到咱们的源码目录下面,然后和咱们的源码一起打成可执行jar包

build.xml 如下:

<?xml version="1.0"?>
<project name="parser" default="compile" basedir=".">
    <target name="init">
        <mkdir dir="bin"> </mkdir>
        <mkdir dir="dest"> </mkdir>
    </target>

    <path id="classpath.local">
        <pathelement location="./lib/log4j-1.2.14.jar"/>
        <pathelement location="./lib/mysql-connector-java-5.0.7-bin.jar"/>
        <pathelement location="./lib/je-3.2.43.jar"/>
        <pathelement location="./lib/nekohtml.jar"/>
        <pathelement location="./lib/Tidy.jar"/>
        <pathelement location="./lib/xercesImpl.jar"/>
        <pathelement location="bin"/>
        <pathelement location="bin/com"/>
        <pathelement location="."/>
    </path>

    <target name="compile" depends="init">
        <javac destdir="bin" srcdir="src">
            <classpath refid="classpath.local"/>
        </javac>
    </target>

    <target name="run" depends="compile">
        <java classname="bin/Parser" classpath="classpath.local"> </java>
    </target>

    <target name="dest" depends="compile">
        <mkdir dir="dest"></mkdir>
        <jar jarfile="dest/parser.jar" basedir="bin"></jar>
    </target>

    <target name="clean">
        <delete dir="bin"> </delete>
        <delete dir="dest"> </delete>
    </target>

    <target name="unziplib">
        <unjar dest="bin">
            <fileset dir="lib" includes="*.jar" />
        </unjar>
    </target>

    <target name="jar" depends="compile,unziplib">
        <jar jarfile="dest/Parser.jar" basedir="bin" >
        <manifest>
            <attribute name="Main-class" value="com.dyoldfish.parser.Parser"/>
        </manifest>
    </jar>
    <copy  todir="dest">
        <fileset dir="doc">
            <include name="Parser.bat"/>
            <include name="clean.bat"/>
        </fileset>
    </copy>
  </target>

</project> 

方法2、把别的jar包库,放在咱们的可执行jar包外面,然后在咱们的jar包里面配置 classpath
build.xml如下:

<?xml version="1.0"?>
<project name="parser" default="compile" basedir=".">
    <target name="init">
        <mkdir dir="bin"> </mkdir>
        <mkdir dir="dest"> </mkdir>
    </target>

    <path id="classpath.local">
        <pathelement location="./lib/log4j-1.2.14.jar"/>
        <pathelement location="./lib/mysql-connector-java-5.0.7-bin.jar"/>
        <pathelement location="./lib/je-3.2.43.jar"/>
        <pathelement location="./lib/nekohtml.jar"/>
        <pathelement location="./lib/Tidy.jar"/>
        <pathelement location="./lib/xercesImpl.jar"/>
        <pathelement location="bin"/>
        <pathelement location="bin/com"/>
        <pathelement location="."/>
    </path>

    <target name="compile" depends="init">
        <javac destdir="bin" srcdir="src">
            <classpath refid="classpath.local"/>
            <!--include name="com/**"/-->
        </javac>
        <copy  todir="bin">
            <fileset dir="doc" includes="run.bat"/>
        </copy>
    </target>

    <target name="run" depends="compile">
        <java classname="bin/Parser" classpath="classpath.local"> </java>
    </target>

    <target name="dest" depends="compile">
        <mkdir dir="dest"></mkdir>
        <jar jarfile="dest/parser.jar" basedir="bin"></jar>
        <!--copy todir="dest"/-->
            <!--fileset dir="bin" includes="*.class" /-->
        <!--/copy-->
    </target>

    <target name="clean">
        <delete dir="bin"> </delete>
        <delete dir="dest"> </delete>
    </target>

    <target name="jar" depends="compile">
        <jar jarfile="dest/Parser.jar" basedir="bin"
        includes="com/**/*.class">
        <manifest>
            <attribute name="Main-class" value="com.dyoldfish.parser.Parser"/>
            <attribute name="Class-path" value=". ../lib/log4j-1.2.14.jar ../lib/mysql-connector-java-5.0.7-bin.jar ../lib/je-3.2.43.jar ../lib/nekohtml.jar ../lib/Tidy.jar ../lib/xercesImpl.jar"/>
        </manifest>
        <!--fileset dir="lib" includes="*.jar" /-->
    </jar>
    <copy  todir="dest">
        <fileset dir="doc" includes="Parser.bat"/>
    </copy>
  </target>

</project>



以上的调用方法都是:
          ant jar
    可执行jar包就编译好了
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:130354次
    • 积分:1551
    • 等级:
    • 排名:千里之外
    • 原创:32篇
    • 转载:8篇
    • 译文:0篇
    • 评论:19条
    文章分类
    最新评论