How to introduce third-party Jar packages in Ant

Source: Internet
Author: User

When Ant is used, the third-party Jar package cannot be compiled because the project references a third-party Jar package. You can add the following elements to the Ant script to reference the third-party library.

Steps:

1. Add sub-elements to the <project> element of build. xml, as follows:

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

Note: change it to your own Lib path.

2. Add a child element <classpath> to your <target> child element <javac> as follows:

 

<classpath refid="lib_classpath"/>

 

A complete example:

<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 = "Your 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>


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.