Ant is a sub-project of Apache's Java sub-project "Jakarta". You can select the current version, window version
After decompression, ant_home is used for convenient access. Make sure that you have set java_home.
Set ant_home = D: \ Java \ kit \ ant \ jakarta-ant-1.5.1 This Is My directory
Hello ant
We need to develop a Java class: the content has only one sentence, and the "Hello ant" string is output. And use ant to compile and run the job. This example is just to run ant without additional things.
The following is the "Hello. Ant. helloant. Java" file.
Package hello. ant; public class helloant {public static void main (string [] ARGs) {system. Out. println ("Hello ant, ant's first contact, great! ");}}
Write 1 file in the project root directory (hello-Ant \): ant executes the configuration file build. xml
"Build. xml" File
<? XML version = "1.0" encoding = "gb2312"?> <! -- A project that can contain multiple task groups --> <project default = "Main" basedir = "."> <! -- A task group in the project can contain many tasks (task: javac, Java...) --> <target name = "Main"> <! -- Compile --> <javac srcdir = "src \ main \ Hello \ ant" destdir = "build \ Classes"/> <! -- Run --> <Java classname = "hello. ant. helloant "> <classpath> <pathelement Path =" build \ Classes "/> </classpath> </Java> </Target> </Project>
Okay, all done. Oh, no, it hasn't been run yet.
Enter the hello-ant directory under DOS, that is, the directory where build. XML is located. We need to execute it using ant tool,
Run: % ant_home %/bin/ant-file build. xml. Use ant to run the configuration file build. XML in the current directory.
Or: ant-file build. xml if you set % ant_home %/bin to path
This time OK. Here is the answer:
Command Prompt window
D: \ temp \ hello-ant> ant-file build. xmlbuild build. xmlmain: [javac] compiling 1 source file to D: \ temp \ hello-Ant \ build \ Classes [Java] Hello ant, ant's first contact, great! Build successfultotal time: 2 secondsd: \ temp \ hello-ant>
Check the build/classes directory. You can see the compiled files here:
Build/classes/Hello/ANT/helloant. Class.
Hello ant
We need to improve build. xml so that it can do more:
Define global variables
Initialization, mainly to create a directory
Compile (existing)
Package as jar
Create API documentation
Generate distribution products
Everything is balanced. You need ant to do more for you. Of course, you need to be a little tired, but you only need to do it once. The building after code modification will be completed in a "one-click" manner, let's create a simple example of hello. You can do J2EE exercises by yourself.
We need to expand the directory structure to make it more like a thing:
: \ SRC, \ docs, \ Lib is the file structure of your own organization, \ build, \ dist is the final product dynamically generated by ant. \ SRC Source File: Java source, source, JSP Source, xml configuration ..... \ SRC \ main java source \ SRC \ window, UNIX, liunx execution, we only have one simple: Run. BAT: Java hello. ant. helloant \ docs handwritten instruction document \ jar of the library required by the Lib program, such as J2EE. jar, mail, jar... \ build uses ant to dynamically generate handwritten instruction documents for the build directory \ build \ Classes compiled class files \ build \ docs copy "\ Docs, and the API document \ build \ Lib generated by ANT put our own helloant. class package finished hello-ant.jar \ Dist \ bin copy "\ SRC \" to execute the file \ Dist \ docs copy "\ build \ Docs" \ Dist \ Lib in addition to copy "\ build \ hello-ant under lib" In addition to. jar, we should also copy the jar required by the "\ Lib" program. We do not have it here.
These are my foreign document organizations. You can organize them according to your interests.
Prepare necessary files:
Hello. Ant. helloant. Java
SRC \. bat
@ Echo offecho ============================================ ============================ echo, set environmentecho first. echo java_home: % java_home % echo ======================================== =========================% java_home % \ bin \ Java-classpath .. \ Lib \ hello-ant.jar hello. ant. helloantpause
\ Docs \ index.html write a handwritten document at will
Hello ant software project manual docs
--------------------------------------------------------------------------------
Access API documentation
\ Build. xml configuration file
<? XML version = "1.0" encoding = "gb2312"?> <! -- ===================================================== ================================ Hello-ant project, learn about the ant tool's 2nd build files. refer to build for ant's jakarta-ant-1.6alpha. xmlcopyright (c) 2002 the Neusoft Software Foundation. all rightsreserved. ========================================================== =========================================== --> <! -- The document structure is: <project> <property/> definition of global variables <property/>... <target name = "1"> task group <javac> </javac> A javac task... <onetask> </onttask> one other task </Target> <target name = "2"> <javac> </javac>... <onetask> </onttask> </Target> </Project> A project represents a project. Default: basedir of the target (Task Group) with the name "Dist": basedir: Baseline path. --> <Project default = "Dist" basedir = "."> <! -- ===================================================== ========================================== Define property tasks) we recommend that you define the path and name as a global variable here: Define <property name = "A" = "hello"/> and use it like this: <property name = "B" = "$ {A}/B"/> now: B = "Hello/B" ==================================== =========================================================== --> <! -- Main system environment attributes --> <property environment = "env"/> <! -- Take window, Unix... environment variable --> <property name = "Java. home "=" $ {Env. java_home} "/> <property name =" ant. home "=" $ {Env. ant_home} "/> <! -- Main app environment attributes --> <property name = "app. name "=" hello-ant "/> <property name =" app. jar "=" $ {app. name }. jar "/> <property name =" app. copyright "=" Copyright (c) 2002 the Neusoft Software Foundation. all rights reserved. "/> <! -- Src attribute in app --> <property name = "src. dir "=" src "/> <property name =" src. main "=" $ {SRC. dir}/main "/> <property name =" src. "=" $ {SRC. dir}/"/> <! -- Lib used by the app --> <property name = "Lib. dir" = "lib"/> <! -- App build directory --> <property name = "build. dir "=" build "/> <property name =" build. classes "=" $ {build. dir}/classes "/> <property name =" build.doc s "=" $ {build. dir}/Docs "/> <property name =" build.doc S. API "=" zookeeper build.doc s}/API "/> <property name =" build. lib "=" $ {build. dir}/lib "/> <! -- App's dist (distribution) directory --> <property name = "Dist. dir "=" Dist "/> <property name =" Dist. bin "=" $ {Dist. dir}/bin "/> <property name =" dist.doc s "=" $ {Dist. dir}/Docs "/> <property name =" Dist. lib "=" $ {Dist. dir}/lib "/> <! -- In the app's docs directory --> <property name = "docs. dir" = "Docs"/> <! -- After defining a set of paths, you can reuse these paths by ID, for example: <javac srcdir = "src/main" destdir = "build/classes"> <classpath refID = "classpath"/> </javac> --> <path id = "classpath"> <! -- This project only has one Java and does not use classpath. Here is an example --> <pathelement location = "$ {build. classes} "/> <pathelement Path =" $ {Java. home}/lib/tools. jar "/> </path> <! -- ===================================================== ========================================= Init prepare the directory (File tasks) the main directory structure is usually not changed, generate them together =============================== --> <target name = "init"> <! -- Clear the previous directory --> <Delete dir = "$ {build. dir} "fail =" false "/> <Delete dir =" $ {Dist. dir} "fail =" false "/> <! -- Prepare the directory --> <mkdir dir = "$ {build. dir} "/> <mkdir dir =" $ {build. classes} "/> <mkdir dir =" zookeeper build.doc s} "/> <mkdir dir =" zookeeper build.doc S. API} "/> <mkdir dir =" $ {build. lib} "/> <mkdir dir =" $ {Dist. dir} "/> <mkdir dir =" $ {Dist. bin} "/> <mkdir dir =" $ {Dist. lib} "/> </Target> <! -- ===================================================== ============================ Build the code (compile tasks, file tasks) ========================================================== ===========================================--> <target name = "build" depends = "init "> <! -- Compile --> <javac srcdir = "$ {SRC. main} "destdir =" $ {build. classes} "> <classpath refID =" classpath "/> </javac> </Target> <! -- ===================================================== ============================ Archive tasks) create the project jars: xxx1.jar and xxx2.jar ==================================== ============================= --> <target name = "jars" depends = "build"> <jar basedir = "$ {build. classes} "jarfile =" $ {build. lib}/$ {app. jar} "/> </Target> <! -- ===================================================== ====================================== Creates the API documentation ========== ========================================================== =============================--> <target name = "javadocs" depends = "jars" Deion = "--> creates API documentation "> <! -- Copy docs manual... --> <copy todir = "cmdname build.doc s}"> <fileset dir = "$ {docs. dir} "/> </copy> <javadoc packagenames =" hello. ant. * "sourcepath =" $ {SRC. main} "defaultexcludes =" yes "destdir =" too many build.doc S. API} "author =" true "version =" true "use =" true "windowtitle =" docs API "> <doctitle> <! [CDATA [
Build. XML is a little more, but it is actually quite simple: (for more detailed comments, please refer to it. Here I will briefly talk about it)
A build. xml contains a complete XML Description of automatic processing of a project, which consists of three types:
<Project>
1. Definitions of global variables
<Property/>
2. Task Group
<Target>
3. Many single tasks... such as copy, delete, javac, jar...
<Task1/>
<Task2/>
<Task3/>
</Target>
</Project>