Ant is a concise primer in eclipse

Source: Internet
Author: User
Tags cdata mkdir tag name web hosting
using ant in Eclipse

Ant is a great batch command execution program under the Java platform, and it's easy to automate compilation, testing, packaging, deployment, and so on, and greatly improve development efficiency. If you haven't started using ant now, start learning to use it quickly and make your development level a new one.

Ant is already integrated in Eclipse and we can run ant directly in Eclipse.

For an example of the Hello project that was established earlier, create the following directory structure:



Create a new build.xml, put it under the engineering root directory. Build.xml defines the batch command that ant will execute. Although Ant can also use other file names, following standards can make development more normative and easy to communicate with others.

Usually, SRC storage Java source files, classes store the compiled class files, Lib store compile and run all used jar files, web hosting JSP and other Web files, dist store the packaged jar file, Doc store API documents.

Then create the Build.xml file in the root directory and enter the following:

<?xml version= "1.0"?>
<project name= "Hello World" default= "Doc" >

<!--properies-->
<property name= "Src.dir" value= "src"/>
Name= "Report.dir" value= "<property"/>
<property name= "Classes.dir" value= "Classes"/>
<property name= "Lib.dir" value= "Lib"/>
<property name= "Dist.dir" value= "dist"/>
<property name= "Doc.dir" value= "Doc"/>

<!--definition Classpath-->
<path id= "Master-classpath" >
<fileset file= "${lib.dir}/*.jar"/>
<pathelement path= "${classes.dir}"/>
</path>

<!--initialization task-->
<target name= "Init" >
</target>

<!--compiling-->
<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>

<!--packaged into jars-->
<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 above XML, in turn, defines init (initialization), compile (compilation), Test (test), Doc (build document), Pack (package) task, and can be used as a template.

Select the Hello project and select "Project", "Properties", "Builders", "New ...", and select "Ant build":

Fill in the Name:ant_builder;buildfile:build.xml;base Directory:${workspace_loc:/hello} (click "Browse Workspace" to select the engineering root), With the Junit.jar package, search the Eclipse directory, find Junit.jar, copy it to the Hello/lib directory, and add it to Ant's classpath:

Then hook up the Ant_build in the Builder panel and remove the Java Builder:

Again, you can see Ant's Output at 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 to 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 could 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 performs initialization, compilation, testing, packaging, and generating API documents in sequence, which greatly improves the development efficiency. In the future development of the Java EE project, you can also join the deployment and other tasks. And, even out of the eclipse environment, if Ant is properly installed, configure the environment variable Ant_home=<ant extract directory >,Path=...; %ant_home%/bin, switch to the Hello directory at the command-line prompt, and simply type ANT.

First download ant in Http://jakarta.apache.org/builds, then unzip to local, I compress it into c:/ant directory

modifying properties in environment variables

Increase the------c:/ant/bin before the Path property, so you can use ant

The Build.xml file contains one project and at least one target element, and the target element contains one or more task elements, and the task is an executable code.

The root element is project, three properties Name,default,basedir

Name Specifies the project names

default Specifies the target element for the project

BASEDIR Specifies the base path for the project, if it is ".", which is represented as the path of the Build.xml

<project name= "Bookstore" default= "about" basedir= "." >

<target name= "Init" >
<tstamp/>

<!--initialize the name of each variable-->
<property name= "Build" value= "built"/><!--compiled directory-->
<property name= "src" value= "src"/><!--the location of the source file-->

<!--the package--> of the environment variables used

<property environment= "Myenv"/>
<property name= "Servletpath" value= "${myenv". Catalina_home}/common/lib/servlet-api.jar "/>
<property name= "Mysqlpath" value= "Web-inf/lib/mysqldriver.jar"/>

<mkdir dir= "${build}"/><!--call the previous directory parameter is ${xxx}-->
<mkdir dir= "${build}/web-inf"/>
<mkdir dir= "${build}/web-inf/classes"/>

<copy todir= "${build}" ><!--copy directory files to the new directory-->
<fileset dir= "${basedir}" ><!--the original directory is the path to the Basedir parameter-->
<include name= "*.jsp"/>
<include name= "*.bmp"/>
<include name= "web-inf/**"/>
<exclude name= "Build.xml"/><!--do not copy this file-->
</fileset>
</copy>
</target>

<target name= "Compile" depends= "init" > <!--it relies on the execution of Init, so calling it executes first init-->

<javac srcdir= "${src}"
Destdir= "${build}/web-inf/classes"
Classpath= "${servletpath}:${mysqlpath}" >
</javac>
</target>

<target name= "Bookstorewar" depends= "compile" > <!--the task of generating the war-->

<war warfile= "${build}/bookstore.war" webxml= "${build}/web-inf/web.xml" >
<lib dir= "${build}/web-inf/lib"/>
<classes dir= "${build}/web-inf/classes"/>
<fileset dir= "${build}"/>
</war>
</target>

<target name= "About" > <!--default target-->
<echo>
This build.xml file contains targets
For Building Bookstore Web application
</echo>
</target>

</project>

we have this build.xml in the root of our application

so the way to run Ant is: Under DOS

1. Enter C:/myapp, our application directory input: Ant (will search the current path of the Build.xml file)

2. Direct Input Ant-buildfile C:/myapp/build.xml

3. Direct input Ant-buildfile c:/myapp/build.xml about

All three of these methods execute the target of about, and if you want to compile the Java file, we just

Ant-buildfile C:/myapp/build.xml Compile

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.