Java Ant Build Detailed

Source: Internet
Author: User

Reprint Address: http://www.cnblogs.com/wufengxyz/archive/2011/11/24/2261797.html

1, what is ant
Ant is the build tool
2, what is build
The concept can be found everywhere, in the image, you have to take the code from somewhere, compile, and then copy it to a place and so on, of course, not only with this, but mainly to do this
Benefits of 3,ant
Cross-platform-because Ant is implemented in Java, it's cross-platform
Simple to use--compared to Ant's brother make
The syntax is clear-it's also compared to make
Powerful--ant can do a lot of things, you may have spent a long time, you still do not know how much it can function. When you develop some ant plugins yourself, you will find it more functional.
4,ant's brother make
Many of the things that Ant does, most of which are once made, have been done by make, but the objects are different, making is more applied to C + +, and ant is more in Java. Of course it's not certain, but most people do.
First, build an ant environment
To use ant to build an ant environment first, the steps are simple:
1), install JDK, set Java_home, PATH, Class_path (these should be known to the people who read this article)
2), download ant address http://www.apache.org/Find a version you like, or simply the latest version
3), unzip ant you get a compressed package, unzip it, and put it in a directory as simple as possible, such as D:\ant-1.6 although you don't have to do this, it's good to do so.
4), set Ant_home, add the bin directory under Ant_home directory in PATH (I set: Ant_home:d:\apache-ant-1.8.2,path:%ant_home%\bin)
5), test your setup, start-and-run-->cmd into command line-and type ant carriage return if you see
Buildfile:build.xml does not exist!
Build failed
Well, congratulations, you've done your ant setup.
Second, experience ant
Just like every language has a helloworld, one of the simplest apps can make you feel ant.
1, first you need to know what you are going to do, what I want to do now is:
Write some programs
Compile them
Pack it up as a jar
Put them where they should be.
Run them
Here for the sake of simplicity only write a program, is the Helloworld.java program code is as follows:
Package test.ant;
public class helloworld{
public static void Main (string[] args) {
System.out.println ("Hello world1");
}
};
2, in order to achieve the above purpose, you can manually use Javac, copy, Jar, Java to complete, but consider if you have hundreds of classes, in multiple debugging, deployment, time and again Javac, copy, Jar,
Java that would be a hard job. Now look at how ant is doing it gracefully.
To run ant you need to have a build.xml although not necessarily called this name, but it is recommended that you do so
Below is a complete build.xml, then we'll explain each sentence in detail.
<?xml version= "1.0" encoding= "UTF-8"?>
<project name= "HelloWorld" default= "Run" basedir= "." >
<property name= "src" value= "src"/>
<property name= "Dest" value= "Classes"/>
<property name= "Hello_jar" value= "Hello1.jar"/>
<target name= "Init" >
<mkdir dir= "${dest}"/>
</target>
<target name= "Compile" depends= "init" >
<javac srcdir= "${src}" destdir= "${dest}"/>
</target>
<target name= "Build" depends= "compile" >
<jar jarfile= "${hello_jar}" basedir= "${dest}"/>
</target>
<target name= "Run" depends= "Build" >
<java classname= "Test.ant.HelloWorld" classpath= "${hello_jar}"/>
</target>
<target name= "clean" >
<delete dir= "${dest}"/>
<delete file= "${hello_jar}"/>
</target>
<target name= "Rerun" depends= "Clean,run" >
<ant target= "clean"/>
<ant target= "Run"/>
</target>
</project>
Explain:
<?xml version= "1.0" encoding= "UTF-8"?>
The first sentence in Build.xml has no practical meaning.
<project name= "HelloWorld" default= "Run" basedir= "." >
</project>
All ant content must be included in this, name is the name you give it, and Basedir is the root directory of the work. Represents the current directory. Default stands for what you want to do.
<property name= "src" value= "src"/>
Similar to the variables in the program, why do you think about the role of variables
<target name= "Compile" depends= "init" >
<javac srcdir= "${src}" destdir= "${dest}"/>
</target>
Write a target for everything you want to do, it has a name, depends is the target it relies on, and before executing this target such as compile, ant will first check if Init was ever executed, if
Direct execution of the compile, if not, it will first execute its dependent target such as init here, and then execute the target
As in our plan
Compile:
<target name= "Compile" depends= "init" >
<javac srcdir= "${src}" destdir= "${dest}"/>
</target>
To make a jar package:
<target name= "Build" depends= "compile" >
<jar jarfile= "${hello_jar}" basedir= "${dest}"/>
</target>
Run:
<target name= "Run" depends= "Build" >
<java classname= "Test.ant.HelloWorld" classpath= "${hello_jar}"/>
</target>
In order not to copy, we can define the target folder at the very beginning, so that ant directly puts the result in the target folder.
New folder:
<target name= "Init" >
<mkdir dir= "${dest}"/>
</target>
Added two target for more features
To delete a generated file
<target name= "clean" >
<delete dir= "${dest}"/>
<delete file= "${hello_jar}"/>
</target>
Run again, this shows how to invoke the other target inside a target
<target name= "Rerun" depends= "Clean,run" >
<ant target= "clean"/>
<ant target= "Run"/>
</target>
Well, the explanation is done, so check out your ant on the bottom.
Create a new SRC folder and put the Helloworld.java in the package directory
Do build.xml files, it is best to put these into a folder, in the cmd into the folder,
Type ant at the command line and you'll find that a task is complete. Each time you change the code, you only need to type ant again
Sometimes we may not want to run the program, just want to perform one of these steps one or two steps, for example, I just want to redeploy but do not want to run, type
Ant Build
Every task in ant can call the Ant + target name in this way.
OK, so a simple ant task is done.


First, when to use ant
Maybe you heard someone talk about ant, on impulse to learn ant, when you read the first instance above, perhaps you feel ant is really good, perhaps you feel ant is so much, to come to these conclusions can not say wrong, although ant is very useful,
But it's not always the best option in any case, such as having more simple, easy-to-use tools on Windows like Eclipse+myeclipse ECLIPSE+WTP and so on, whether it's compiling, deploying, running, using more than Ant.
Easy, convenient but in some cases it's a good place for ant to play:
1, when deploying on the server
When your program is developed and deployed to the server, it is not always possible to configure a eclipse+myeclipse because of the installation of a program, Ant is a good choice because it is small and easy to match.
You take your build.xml to any server, just make simple changes (some settings, such as directories), and then one or two commands to complete, isn't that a good thing.
2,linux, most of the time, program development is under Windows, but programs run on Linux or UNIX, on Linux or
Deployment on Unix (especially Unix) is a hassle, and this time the ant feature comes out because Ant is cross-platform and you can use it on most operating systems in Build.xml, with minimal modification.
3, when the server maintainer does not understand the programming
A lot of people have had this experience, people who use your program do not know how to write programs. You have to update the program because it needs to be re-deployed again and again because of bug fixes. This time you will find it difficult to teach a person. But
After having ant, you just need to tell him to enter one or two commands such as Ant xxx, everything OK.
These are some of the things I have encountered.
After reading the above situation, think about whether you need to use ant, if it is to continue.

Learn a little bit more complex ant
In the actual work process may have some of the following situations, a project into a number of modules, each team or department is responsible for a module, in order to test, they wrote a build.xml, and you are responsible for these modules to the
Used together, write a build.xml
You have two options at this time:
1, to write myself a build.xml, which would be a troublesome thing to
2, try to use the build.xml they have written to reduce their work
As an example:
Let's say you have three groups below, each with one section, one src and one written build.xml.
This time you get their src, what you need to do is build three folders Src1, SRC2, src3 respectively put their src and build.xml in, and then write a build.xml
<?xml version= "1.0" encoding= "UTF-8"?>
<project name= "main" default= "Build" basedir= "." >
<property name= "Bin" value= "${basedir}\bin"/>
<property name= "Src1" value= "${basedir}\src1"/>
<property name= "Src2" value= "${basedir}\src2"/>
<property name= "SRC3" value= "${basedir}\src3"/>
<target name= "Init" >
<mkdir dir= "${bin}"/>
</target>
<target name= "Run" >
<ant dir= "${src1}" target= "Run"/>
<ant dir= "${src2}" target= "Run"/>
<ant dir= "${SRC3}" target= "Run"/>
</target>
<target name= "clean" >
<ant dir= "${src1}" target= "clean"/>
<ant dir= "${src2}" target= "clean"/>
<ant dir= "${SRC3}" target= "clean"/>
</target>
<target name= "Build" depends= "Init,call" >
<copy todir= "${bin}" >
<fileset dir= "${src1}" >
<include name= "*.jar"/>
</fileset>
<fileset dir= "${src2}" >
<include name= "*.jar"/>
</fileset>
<fileset dir= "${SRC3}" >
<include name= "*.jar"/>
</fileset>
</copy>
</target>
<target name= "rebuild" depends= "Build,clean" >
<ant target= "clean"/>
<ant target= "Build"/>
</target>
</project>
OK, your job is done.

OK, above you completed the task, but do you have some feelings, in those build.xml, most of them are duplicated, and change the directory need to change a lot of things. Is it possible to make the job a little better, the answer is yes.
Introduction of two things:
1,propery
2,xml include
These two things have a function, is to be able to build.xml <propery/> In the content of the separation, common use
In addition, they each have their own characteristics:
Propery is characterized by simple maintenance and simple key-value pairs, because not everyone likes the format of XML
The feature of XML include is that it can not only extract attributes, but also target.
Or a previous example:
For example, we want to put Src1 src2 SRC3 These three attributes from the XML, you can create a new file called All.properties
What's Inside
Src1=d:\\study\\ant\\src1
Src2=d:\\study\\ant\\src2
Src3=d:\\study\\ant\\src3
Then your Build.xml file can be written like this, others just need to change the configuration file, and don't want to change your build.xml file.
<?xml version= "1.0" encoding= "UTF-8"?>
<project name= "main" default= "Build" basedir= "." >
<property file= "All.properties"/>
<property name= "Bin" value= "${basedir}\bin"/>
<target name= "Init" >
<mkdir dir= "${bin}"/>
</target>
<target name= "Run" >
<ant dir= "${src1}" target= "Run"/>
<ant dir= "${src2}" target= "Run"/>
<ant dir= "${SRC3}" target= "Run"/>
</target>
<target name= "clean" >
<ant dir= "${src1}" target= "clean"/>
<ant dir= "${src2}" target= "clean"/>
<ant dir= "${SRC3}" target= "clean"/>
</target>
<target name= "Build" depends= "Init,call" >
<copy todir= "${bin}" >
<fileset dir= "${src1}" >
<include name= "*.jar"/>
</fileset>
<fileset dir= "${src2}" >
<include name= "*.jar"/>
</fileset>
<fileset dir= "${SRC3}" >
<include name= "*.jar"/>
</fileset>
</copy>
</target>
<target name= "rebuild" depends= "Build,clean" >
<ant target= "clean"/>
<ant target= "Build"/>
</target>
<target name= "Test" >
<ant dir= "${src1}" target= "Test"/>
<ant dir= "${src2}" target= "Test"/>
<ant dir= "${SRC3}" target= "Test"/>
</target>
</project>
If you look at it yourself, you'll see a target like this.
<target name= "Test" >
<ant dir= "${src1}" target= "Test"/>
<ant dir= "${src2}" target= "Test"/>
<ant dir= "${SRC3}" target= "Test"/>
</target>
Sometimes you want to add a few target to each group's build.xml, one way is to write each inside, and then call here
But there's a better way.
You can write a include.xml file that reads as follows
<?xml version= "1.0" encoding= "UTF-8"?>
<property name= "src" value= "src"/>
<property name= "Dest" value= "Classes"/>
<target name= "Test" >
<ant target= "Run"/>
</target>
Then change the Build.xml file for your three groups, adding the following to each
<!--include a XML file, it can common propery, can be also a target--
<! DOCTYPE Project [
<! ENTITY share-variable SYSTEM "file:. /include.xml ">
]>
&share-variable;
It becomes the same as it looks.
At this time, you just add propery in Include.xml, add target, three build.xml will add these propery and target simultaneously
And it doesn't make the build.xml of three groups more complicated.
<?xml version= "1.0" encoding= "UTF-8"?>
<!--include a XML file, it can common propery, can be also a target--
<! DOCTYPE Project [
<! ENTITY share-variable SYSTEM "file:. /include.xml ">
]>
<project name= "HelloWorld" default= "Run" basedir= "." >
<!--use the include--
&share-variable;
<!--defined the property-->
<!--via include
<property name= "src" value= "src"/>
<property name= "Dest" value= "Classes"/>
-
<property name= "Hello_jar" value= "Hello1.jar"/>
<!--define the Op-->
<target name= "Init" >
<mkdir dir= "${dest}"/>
</target>
<target name= "Compile" depends= "init" >
<javac srcdir= "${src}" destdir= "${dest}"/>
</target>
<target name= "Build" depends= "compile" >
<jar jarfile= "${hello_jar}" basedir= "${dest}"/>
</target>
<target name= "Run" depends= "Build" >
<java classname= "Test.ant.HelloWorld" classpath= "${hello_jar}"/>
</target>
<target name= "clean" >
<delete dir= "${dest}"/>
<delete file= "${hello_jar}"/>
</target>
<target name= "Rerun" depends= "Clean,run" >
<ant target= "clean"/>
<ant target= "Run"/>
</target>
</project>

With that in mind, you'll know how to write a good ant, but you'll find that when you really want to do it, you can't immediately make good build.xml because you know too little of the default command provided by Ant. This
Time if you want to complete the task and improve yourself, there are many ways:
1, many open-source programs come with Build.xml to see how they write
2,ant's document, which contains a detailed list of Ant's various default commands, and its rich
3,google, never forget it.
OK, after that, as you write more ant builds, the more commands you know, the more powerful ant is in your hands.
This is a process of gradual accumulation.

Ant's example is very easy to find, all kinds of open source framework will have a build.xml take a closer look, there will be great gains
Another often used, but in the open source framework of the build.xml is generally not the CVS
If you are using remote CVS, you can use the
<xml version= "1.0" encoding= "Utf-8"?>
<project>
<property name= "Cvsroot" value= ":p server:wang:@192.168.1.2:/cvsroot"/>
<property name= "Basedir" value= "/tmp/testant/"/>
<property name= "Cvs.password" value= "Wang"/>
<property name= "Cvs.passfile" value= "${basedir}/ant.cvspass"/>
<target name= "Initpass" >
<cvspass cvsroot= "${cvsroot}" password= "${cvs.password}" passfile= "${cvs.passfile}"/>
</target>
<target name= "Checkout" depends= "Initpass" >
<cvs cvsroot= "${cvsroot}" command= "Checkout" cvsrsh= "SSH" package= "MyProject" dest= "${basedir}"
Passfile= "${cvs.passfile}"/>
</target>
</project>

Ant is supported in eclipse, so you can write build.xml directly in eclipse
Because eclipse provides hints, auto-replenishment, which allows you to do more with less.
Using the method, you only need to build a project and then create a file called Build.xml. And then you can write your ant build inside.
But always remember that http://www.apache.org/can always find what you need.

Java Ant Build Detailed

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.