I like simplicity: ant-Java build tools
Hello ant
How do you manage your code, DOC files, compilation, configuration, and other development work? Manage the source directory, compile files, copy, and implement it yourself...
Write bat in window and Shell in UNIX?
Unfortunately, I won't write much at the end. Therefore, the process of creating an EJB is as follows:
Write code-> write a bat compile code-> organization file (ejb-jar.xml, JBoss. xml...)-> compress the file to generate JAR file->
Implementation... (and the same test process) (why not use jbuild ?)
Every modification in this process has to go through countless times of manual copy, paste... process, so tired and time-consuming. But I really don't want.
I have to re-learn about script writing in Unix or Linux, so I am too tired.
If you want to automatically execute predefined actions, but you don't want to be too tired, I think ant is a good tool:
It can be used on different platforms (cross-platform) in the same way ). It is made in Java. It is free and open-source. It is said that the Internet has become a fact Java
Building standards, I also heard that experts use it... ^_^
It took me two days to master its basic usage. I hope that you will spend half a day mastering it through my articles and experiments.
Content:
- Download and install
- Hello ant
- Hello ant
- Reference and download
Download and install
Download again? Is there a bunch of parameter variables?
Http://jakarta.apache.org/ant/index.html it is a sub-project of Apache's Java sub-project "Jakarta". You can choose
The current version. Currently, I am in version 1.5 and window. I will use it in the following sections.
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
That's all. It's easy.
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.
For file organization, create a directory and write helloant. Java
According to the document organization rules of foreigners, we also copy them.
| Hello. Ant. helloant. Java |
| Package hello. ant; </P> <p> public class helloant {<br/> Public static void main (string [] ARGs) {<br/> system. out. println ("Hello ant, ant's first contact, great! "); <Br/>}< br/>} |
Write 1 file in the project root directory (hello-ANT/): ant executes the configuration file build. xml
| Build. xml |
| <? XML version = "1.0" encoding = "gb2312"?> </P> <p> <! -- A project that can contain multiple task groups --> <br/> <project default = "Main" basedir = "."> </P> <p> <! -- A task group in the project can contain many tasks (task: javac, Java ...) --> <br/> <target name = "Main"> </P> <p> <! -- Compile --> <br/> <javac srcdir = "src/main/Hello/ant" destdir = "build/classes"/> </P> <p> <! -- Run --> <br/> <Java classname = "hello. ant. helloant "> <br/> <classpath> <br/> <pathelement Path =" build/classes "/> <br/> </classpath> <br/> </Java> </P> <p> </Target> <br/> </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. xml Buildfile: Build. xmlMain: [Javac] compiling 1 source file to D:/temp/Hello-ANT/build/classes [Java] Hello ant, ant's first contact, great! Build successful Total time: 2 seconds D:/temp/Hello-ant> |
Check the build/classes directory. You can see the compiled files here:
Build/classes/Hello/ANT/helloant. Class.
Hello ant
(This section is nonsense and can be skipped)
You may say: this is not enough to write a batch for a simple job at the end. xml and ant will waste all my time. I use jbuild or
Webshpere is not enough. How can you understand it? Anyway, most open-source projects on the Internet use ant. You can't give people *. JPX,
Moreover, such a tool is too expensive to handle (of course, the brother of D is not afraid of ^_^), and ant allows you to clearly manage and automate all things:
Compile-implement-test... ah, a little bit of trouble, but save the time you used to spend on fragmented copy and paste. And I found that management
The code quality has been improved.
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:
Ant processes the directory before Compilation:
Directories processed by ant:
In the figure:/src,/docs,/lib is the file structure of your own organization,/build,/Dist is the final product generated by ANT dynamically.
/SRC Source File: Java source, script source, JSP Source, xml configuration .....
/Src/main java source
/Src/script window, UNIX, and liunx execution script. We only have one simple execution script:
Run. BAT: Java hello. Ant. helloant
/Docs handwritten instruction document
/The jar of the library required by the Lib program, such as J2EE. jar, mail, jar...
/Build directory dynamically generated by ant
/Build/classes compiled class files
/Build/docs copy "/Docs" handwritten instruction documents, and the API documents generated by ant
/Build/lib place our own helloant. class packed finished hello-ant.jar
/Dist/bin copy "/src/script" execution File
/Dist/docs copy "/build/Docs" Document
/Dist/lib except for the hello-ant.jar under copy "/build/lib,
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 |
| Existing |
| /Src/script. bat |
| @ Echo off <br/> echo ================================== ======================================< br/> set environment for ECHO first <br/> echo. <br/> echo java_home: % java_home % <br/> echo ================================ ==================================</ P> <p> % java_home %/bin/Java-classpath .. /lib/hello-ant.jar hello. ant. helloant </P> <p> pause |
| /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"?> <Br/> <! -- <Br/> ======================================== =====================================================< br/> hello-ant project, learn about the ant tool's 2nd build files. </P> <p> refer to build of ant's jakarta-ant-1.6alpha. </P> <p> copyright (c) 2002 the Neusoft Software Foundation. all rights <br/> reserved. </P> <p >========================================= =======================================================< br /> --> </P> <p> <! -- <Br/> the document structure is: <br/> <project> <br/> <property/> definition of global variables <br/> <property/>... </P> <p> <target name = "1"> Task Group (tasks) <br/> <javac> </javac> A javac task <br/>... <br/> <onetask> </onttask> another task <br/> </Target> </P> <p> <target name = "2"> <br/> <javac> </javac> <br/>... <br/> <onetask> </onttask> <br/> </Target> <br/> </Project> </P> <p> A project represents a project, <br/> default: Run to target (Task Group) named "Dist" <br/> basedir: basedir path. <Br/> --> <br/> <project default = "Dist" basedir = "."> </P> <p> <! -- <Br/> ======================================== =======================================< br/> define a property tasks) <br/> we recommend that you define the paths and names as global variables here. <br/> for example: define <br/> <property name = "A" value = "hello"/> <br/> and use it like this: <br/> <property name = "B" value = "$ {A}/B"/> <br/> now: B = "Hello/B" <br/> ============================ =====================================================< br/> --> </P> <p> <! -- Main system environment attributes --> <br/> <property environment = "env"/> <! -- Take window, Unix... environment variable --> <br/> <property name = "Java. home "value =" $ {Env. java_home} "/> <br/> <property name =" ant. home "value =" $ {Env. ant_home} "/> </P> <p> <! -- Main app environment attributes --> <br/> <property name = "app. name "value =" hello-ant "/> <br/> <property name =" app. jar "value =" $ {app. name }. jar "/> <br/> <property name =" app. copyright "value =" Copyright (c) 2002 the Neusoft Software Foundation. all rights reserved. "/> </P> <p> <! -- Src attribute in app --> <br/> <property name = "src. dir "value =" src "/> <br/> <property name =" src. main "value =" $ {SRC. dir}/main "/> <br/> <property name =" src. script "value =" $ {SRC. dir}/script "/> </P> <p> <! -- Lib used by the app --> <br/> <property name = "Lib. dir" value = "lib"/> </P> <p> <! -- App build directory --> <br/> <property name = "build. dir "value =" build "/> <br/> <property name =" build. classes "value =" $ {build. dir}/classes "/> <br/> <property name =" build.doc s "value =" $ {build. dir}/Docs "/> <br/> <property name =" build.doc S. API "value =" ${build.doc s}/API "/> <br/> <property name =" build. lib "value =" $ {build. dir}/lib "/> </P> <p> <! -- App's dist (distribution) directory --> <br/> <property name = "Dist. dir "value =" Dist "/> <br/> <property name =" Dist. bin "value =" $ {Dist. dir}/bin "/> <br/> <property name =" dist.doc s "value =" $ {Dist. dir}/Docs "/> <br/> <property name =" Dist. lib "value =" $ {Dist. dir}/lib "/> </P> <p> <! -- App's docs directory --> <br/> <property name = "docs. dir" value = "Docs"/> </P> <p> <! -- <Br/> after defining a set of paths, you can reuse these paths by ID. For example: <br/> <javac srcdir = "src/main" destdir = "build/classes"> <br/> <classpath refID = "classpath"/> <br/> </ javac> <br/> --> <br/> <path id = "classpath"> <br/> <! -- This project only has one Java and does not use classpath. Here is an example --> <br/> <pathelement location = "$ {build. classes} "/> <br/> <pathelement Path =" $ {Java. home}/lib/tools. jar "/> <br/> </path> </P> <p> <! -- <Br/> ======================================== ==================================================< br/> init prepare the directory (file tasks) <br/> the main directory structure is usually not changed, generate them together <br/>======================================= =========================================< br/> --> <br/> <target name = "init"> <br/> <! -- Clear the previous directory --> <br/> <Delete dir = "$ {build. dir} "failonerror =" false "/> <br/> <Delete dir =" $ {Dist. dir} "failonerror =" false "/> </P> <p> <! -- Prepare the directory --> <br/> <mkdir dir = "$ {build. dir} "/> <br/> <mkdir dir =" $ {build. classes} "/> <br/> <mkdir dir =" zookeeper build.doc s} "/> <br/> <mkdir dir =" zookeeper build.doc S. API} "/> <br/> <mkdir dir =" $ {build. lib} "/> </P> <p> <mkdir dir =" $ {Dist. dir} "/> <br/> <mkdir dir =" $ {Dist. bin} "/> <br/> <mkdir dir =" $ {Dist. lib} "/> </P> <p> </Target> </P> <p> <! -- <Br/> ======================================== =====================================< br/> build the code (compile tasks, file tasks) <br/> ========================================== =================================< br/> --> <br/> <target name = "build" depends = "init"> <br/> <! -- Compile --> <br/> <javac srcdir = "$ {SRC. main} "destdir =" $ {build. classes} "> <br/> <classpath refID =" classpath "/> <br/> </javac> <br/> </Target> </P> <p> <! -- <Br/> ======================================== =====================================< br/> tasks) <br/> Create the project jars: xxx1.jar and xxx2.jar <br/> ============================== =========================================< br/> --> <br/> <target name = "jars" depends = "build"> <br/> <jar basedir = "$ {build. classes} "jarfile =" $ {build. lib}/$ {app. jar} "/> <br/> </Target> </P> <p> <! -- <Br/> ======================================== ===================================< br/> creates the API documentation <br/> ========================================== =================================< br/> --> <br/> <target name = "javadocs" <br/> depends = "jars" <br/> description = "--> creates the API documentation"> <br/> <! -- Copy docs manual... --> <br/> <copy todir = "${build.doc s}"> <br/> <fileset dir = "$ {docs. dir} "/> <br/> </copy> </P> <p> <javadoc packagenames =" hello. ant. * "<br/> sourcepath =" $ {SRC. main} "<br/> defaultexcludes =" yes "<br/> destdir =" too many build.doc S. API} "<br/> author =" true "<br/> Version =" true "<br/> Use =" true "<br/> windowtitle =" docs API"> <br/> <doctitle> <! [CDATA [<p> Hello ant docs API </p>]> </doctitle> <br/> <bottom> <! [CDATA [<I >$ {app. copyright} </I>]> </bottom> <br/> <tag name = "Todo" Scope = "all" Description = "to do: "/> <br/> </javadoc> <br/> </Target> </P> <p> <! -- <Br/> ======================================== ===================================< br/> Create the distribution that can run (archive tasks) <br/> copy the copy file from various directories <br/> ==================== ========================================================== ========< br/> --> <br/> <target name = "Dist" depends = "javadocs"> <br/> <! -- Copy bin execution file --> <br/> <copy todir = "$ {Dist. bin} "> <br/> <fileset dir =" $ {SRC. script}/"/> <br/> </copy> <br/> <copy todir =" ${dist.doc s} "> <br/> <fileset dir =" ${build.doc s} /"/> <br/> </copy> <br/> <! -- Copy lib file --> <br/> <copy todir = "$ {Dist. lib} "> <br/> <fileset dir =" $ {build. lib}/"/> <br/> </copy> </P> <p> </Target> <br/> <! -- <Br/> ======================================== ===================================< br/> cleans everything (File tasks) <br/> for example, you can delete files in build, let's play it for you. <br/>============================================== =====================================================< br/> --> </P> <p> </Project> |
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>
Reference and download:
Program: 1st hello-ant
Program in this article: 2nd advanced hello-ant
Ant latest download:
Http://jakarta.apache.org/ant/index.html
For details about how to compile ant, refer to the following two sections in the ant manual,
Http://jakarta.apache.org/ant/manual/using instructions
Http://jakarta.apache.org/ant/manual/coretasklist.html core tasks
You can check other things as well. However, I think it is a waste of time.
Http://jakarta.apache.org/ant/manual/index.html manual Index
Ant/manual/using translated by huihoo.com
Http://www.huihoo.com/java/ant.html
Construct an application with antAuthor: Yu Binbin
Http://developer.ccidnet.com/pub/disp/Article? Columnid = 295 & ArticleID = 27619 & pageno = 1
IBM uses ant and JUnit for incremental development-uses unit tests to gradually improve code
Http://www-900.ibm.com/developerWorks/cn/java/j-ant/index.shtml
About Author:
Chen Peng, Neusoft Xi'an company. As an avid programmer, I hope to grow and improve every day and share my happiness and knowledge with you.
Contact him in the following ways: email chen56@msn.com