Use ant in eclipse
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-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 = <Ant Extract directory & gt;, Path = ...; % Ant_home %/bin, switch to the hello directory at the command line prompt, and simply type ant.