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 automatically compile, test, package, and deploy a series of tasks.

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"?>
<Roject name = "Hello World" default = "Doc">

<-- Properies -->
<Roperty name = "src. dir" value = "src"/>
<Roperty name = "report. dir" value = "report"/>
<Roperty name = "classes. dir" value = "classes"/>
<Roperty name = "Lib. dir" value = "lib"/>
<Roperty name = "Dist. dir" value = "Dist"/>
<Roperty name = "Doc. dir" value = "Doc"/>

<-- Define classpath -->
<Ath id = "Master-classpath">
<Ileset file = "$ {Lib. dir}/*. Jar"/>
<Athelement Path = "$ {classes. dir}"/>
<Path>

<-- Initialize a task -->
<Arget name = "init">
<Target>

<-- Compile -->
<Arget name = "compile" depends = "init" Description = "compile the source files">
<Kdir dir = "$ {classes. dir}"/>
<Avac srcdir = "$ {SRC. dir}" destdir = "$ {classes. dir}" target = "1.4">
<Lasspath refID = "Master-classpath"/>
<Javac>
<Target>

<-- Test -->
<Arget name = "test" depends = "compile" Description = "Run JUnit test">
<Kdir dir = "$ {report. dir}"/>
<Unit printsummary = "on"
Haltonfailure = "false"
Failureproperty = "tests. Failed"
Showoutput = "true">
<Lasspath refID = "Master-classpath"/>
<Ormatter type = "plain"/>
<Atchtest todir = "$ {report. dir}">
<Ileset dir = "$ {classes. dir}">
<Nclude name = "**/* test. *"/>
<Fileset>
<Batchtest>
<JUnit>
<Ail if = "tests. Failed">
**************************************** *******************
* *** One or more tests failed! Check the output ...****
**************************************** *******************
<Fail>
<Target>

<-- Package into jar -->
<Arget name = "pack" depends = "test" Description = "make. jar file">
<Kdir dir = "$ {Dist. dir}"/>
<Ar destfile = "$ {Dist. dir}/Hello. Jar" basedir = "$ {classes. dir}">
<Xclude name = "**/* test. *"/>
<Xclude name = "**/test *. *"/>
<Jar>
<Target>

<-- Output API documentation -->
<Arget name = "Doc" depends = "pack" Description = "create API Doc">
<Kdir dir = "$ {Doc. dir}"/>
<Avadoc destdir = "$ {Doc. dir }"
Author = "true"
Version = "true"
Use = "true"
Windowtitle = "test API">
<Ackageset dir = "$ {SRC. dir}" defaultexcludes = "yes">
<Nclude name = "example/**"/>
<Packageset>
<Octitle> [CDATA [<1> ello, test <Ottom> [CDATA [<> ll rights reserved. <I>]> bottom>
<Ag 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-Projects/Hello/build. xml

Init:

Compile:
[Mkdir] created dir: F:/eclipse-Projects/Hello/classes
[Javac] compiling 2 source files to F:/eclipse-Projects/Hello/classes

Test:
[Mkdir] created dir: F:/eclipse-Projects/Hello/Report
[JUnit] running example. hellotest
[JUnit] tests run: 1, failures: 0, errors: 0, time elapsed: 0.02 Sec

Pack:
[Mkdir] created dir: F:/eclipse-Projects/Hello/Dist
[Jar] building jar: F:/eclipse-Projects/Hello/Dist/Hello. Jar

DOC:
[Mkdir] created dir: F:/eclipse-Projects/Hello/doc
[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-Projects/Hello/doc/stylesheet.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 = <nt decompress directory> Path = ...; % Ant_home %/bin, switch to the hello directory at the command line prompt, and simply type ant.

Related Article

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.