如何在Ant中引入第三方Jar包

在使用Ant时,免不了因为项目引用了第三方Jar包导致无法通过编译,可以在Ant脚本中添加以下元素来引用第三方库。

步骤:

 1、在build.xml的<project>元素中添加子元素,如下:

<path id="lib_classpath">
        <fileset dir="WebContent/WEB-INF/lib/">
            <include name="*.jar"/>
        </fileset>
</path>

 Note:请修改成自己的Lib路径。

 2、在你的<target>的子元素<javac>元素中添加子元素<classpath>,如下:

   <classpath refid="lib_classpath"/>

一个完整的示例:

<project name="JarLibProject" basedir="." default="all">

<!--property name="build.compiler" value="jikes"/>
<property name="build.compiler.emacs" value="true"/
-->
<property name="build.dir" value="classes"/>
<property name="dist.dir" value="dist"/>
<property name="logdir" value="logs"/>
<property name="source.dir" value="src"/>
<property name="junit.results" value="test-results"/>
<property name="cvs.repository" value="你的CVSROOT"/>
<property name="cvs.package" value="JarLibProject"/>

<path id="lib_classpath">
<!--pathelement location="${basedir}\jar\httpmime-4.1.1.jar"/>
<pathelement location="${basedir}\jar\signpost-commonshttp4-1.2.1.1.jar"/>
<pathelement location="${basedir}\jar\signpost-core-1.2.1.1.jar"/>
<pathelement location="${basedir}\jar\Analytics-Android-SDK-2.1.jar2"/
-->
<fileset dir="WebContent/WEB-INF/lib/">
<include name="*.jar"/>
</fileset>
</path>

<target name="init" description="Prepare for build">
<mkdir dir="${build.dir}"/>
<mkdir dir="${dist.dir}"/>
<mkdir dir="${logdir}"/>
</target>

<target name="clean" description="Clean all build products">
<delete dir="${build.dir}"/>
<delete dir="${dist.dir}"/>
<mkdir dir="${build.dir}"/>
<mkdir dir="${dist.dir}"/>
</target>

<target name="compile" depends="init" description="Compile application without cleaning">
<javac srcdir="${source.dir}" destdir="${build.dir}"
includes
="**/*.java" debug="true" deprecation="true">
<classpath refid="lib_classpath"/>
</javac>
</target>

<target name="test" depends="init" description="Run unit tests">
<delete dir="${junit.results}"/>
<mkdir dir="${junit.results}"/>
<junit fork="yes" haltonfailure="yes">
<classpath>
<pathelement location="${build.dir}"/>
</classpath>
<formatter type="plain" usefile="false"/>
<formatter type="xml"/>
<batchtest todir="${junit.results}">
<fileset dir="${build.dir}" includes="**/*Test.class"/>
</batchtest>
</junit>
</target>

<target name="jar">
<jar jarfile="${dist.dir}\JarLibProject.jar">
<fileset dir="${build.dir}"/>
</jar>
</target>

<target name="all" depends="init,clean,compile,test,jar" description="Build application"/>

<target name="checkout" description="Update package from CVS">
<cvs cvsroot="${cvs.repository}" package="${cvs.package}" dest=".."/>
</target>

<target name="masterbuild" depends="checkout,compile,test,jar"
description
="Cruise control master build"/>

<target name="cleanbuild" depends="clean,masterbuild" description="Cruise control clean build"/>

</project>

(文/oldjiang)

本文来源:http://www.cnblogs.com/oldjiang/archive/2012/02/08/CruiseControlJarLib.html


如果给你带来帮助,欢迎微信或支付宝扫一扫,赞一下。