How to Use ANT in Eclipse Quick Start Guide

Source: Internet
Author: User

Ant is a great batch processing command execution program on the Java platform. It can easily and automatically compile, test, package, and deploy a series of tasks, greatly improving the development efficiency. If you haven't started to use Ant now, you need to learn how to use it to bring your development level to a new level.

Ant has been integrated in Eclipse. We can directly run Ant in Eclipse.

The preceding Hello project is used as an example to create the following directory structure:


Create a new build. xml file and place it in the project root directory. Build. xml defines the batch processing command to be executed by Ant. Although Ant can also use other file names, compliance with standards makes development more standard and easy to communicate with others.

Generally, src stores Java source files, classes stores compiled class files, lib stores all jar files used for compilation and running, and web stores web files such as JSP files, dist stores the packaged jar file and doc stores the API documentation.

Create the build. xml file in the root directory and enter the following content:

<? Xml version = "1.0"?>
<Project name = "Hello world" default = "doc">

<! -- Properies -->
<Property name = "src. dir" value = "src"/>
<Property name = "report. dir" value = "report"/>
<Property name = "classes. dir" value = "classes"/>
<Property name = "lib. dir" value = "lib"/>
<Property name = "dist. dir" value = "dist"/>
<Property name = "doc. dir" value = "doc"/>

<! -- Define classpath -->
<Path id = "master-classpath">
<Fileset file = "$ {lib. dir}/*. jar"/>
<Pathelement path = "$ {classes. dir}"/>
</Path>

<! -- Initialize the task -->
<Target name = "init">
</Target>

<! -- Compile -->
<Target name = "compile" depends = "init" description = "compile the source files">
<Mkdir dir = "$ {classes. dir}"/>
<Javac srcdir = "$ {src. dir}" destdir = "$ {classes. dir}" target = "1.4">
<Classpath refid = "master-classpath"/>
</Javac>
</Target>

<! -- Test -->
<Target name = "test" depends = "compile" description = "run junit test">
<Mkdir dir = "$ {report. dir}"/>
<Junit printsummary = "on"
Haltonfailure = "false"
Failureproperty = "tests. failed"
Showoutput = "true">
<Classpath refid = "master-classpath"/>
<Formatter type = "plain"/>
<Batchtest todir = "$ {report. dir}">
<Fileset dir = "$ {classes. dir}">
<Include name = "**/* Test. *"/>
</Fileset>
</Batchtest>
</Junit>
<Fail if = "tests. failed">
**************************************** *******************
* *** One or more tests failed! Check the output ...****
**************************************** *******************
</Fail>
</Target>

<! -- Package it into jar -->
<Target name = "pack" depends = "test" description = "make. jar file">
<Mkdir dir = "$ {dist. dir}"/>
<Jar destfile = "$ {dist. dir}/hello. jar" basedir = "$ {classes. dir}">
<Exclude name = "**/* Test. *"/>
<Exclude name = "**/Test *. *"/>
</Jar>
</Target>

<! -- Output api documentation -->
<Target name = "doc" depends = "pack" description = "create api doc">
<Mkdir dir = "$ {doc. dir}"/>
<Javadoc destdir = "$ {doc. dir }"
Author = "true"
Version = "true"
Use = "true"
Windowtitle = "Test API">
<Packageset dir = "$ {src. dir}" defaultexcludes = "yes">
<Include name = "example/**"/>
</Packageset>
<Doctitle> <! [CDATA [<Bottom> <! [CDATA [<I> All Rights Reserved. </I>]> </bottom>
<Tag name = "todo" scope = "all" description = "To do:"/>
</Javadoc>
</Target>
</Project>

The preceding xml defines init (initialization), compile (Compilation), test (test), doc (generate document), and pack (Package) Tasks in sequence and can be used as templates.

Select the Hello Project, and then select "Project", "Properties", "Builders", "New ...", Select "Ant Build ":


Enter Name: Ant_Builder; Buildfile: build. xml; Base Directory: $ {workspace_loc:/Hello} (Press "Browse Workspace" to select the project root Directory), because junit is used. jar package, search for the Eclipse directory, and find junit. jar, copy it to the Hello/lib directory, and add it to the Classpath of Ant:

Then hook Ant_Build In the Builder panel and remove Java Builder:

Compile again to view the Ant output in the console:

Buildfile: F: eclipse-projectsHellouild.xml

Init:

Compile:
[Mkdir] Created dir: F: eclipse-projectsHelloclasses
[Javac] Compiling 2 source files to F: eclipse-projectsHelloclasses

Test:
[Mkdir] Created dir: F: eclipse-projectsHelloeport
[Junit] Running example. HelloTest
[Junit] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 0.02 sec

Pack:
[Mkdir] Created dir: F: eclipse-projectsHellodist
[Jar] Building jar: F: eclipse-projectsHellodisthello.jar

Doc:
[Mkdir] Created dir: F: eclipse-projectsHellodoc
[Javadoc] Generating Javadoc
[Javadoc] Javadoc execution
[Javadoc] Loading source files for package example...
[Javadoc] Constructing Javadoc information...
[Javadoc] Standard Doclet version 1.4.2 _ 04
[Javadoc] Building tree for all the packages and classes...
[Javadoc] Building index for all the packages and classes...
[Javadoc] Building index for all classes...
[Javadoc] Generating F: eclipse-projectsHellodocstylesheet.css...
[Javadoc] Note: Custom tags that cocould override future standard tags: @ todo. To avoid potential overrides, use at least one period character (.) in custom tag names.
[Javadoc] Note: Custom tags that were not seen: @ todo
BUILD SUCCESSFUL
Total time: 11 seconds

Ant executes initialization, compilation, testing, packaging, and generation of a series of API documentation tasks in turn, greatly improving the development efficiency. You can also add deployment and other tasks when developing a J2EE project in the future. In addition, even if you are out of the Eclipse environment, you only need to correctly install Ant and configure the environment variable ANT_HOME = <Ant Extract directory>, Path = ...; % ANT_HOME % in, switch to the Hello directory at the command line prompt, and simply type ant.

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.