ANT compilation build. xml, antbuild. xml

Source: Internet
Author: User

ANT compilation build. xml, antbuild. xml

1. Experience ant
Just like every language has HelloWorld, a simple application can make people feel Ant.
1. First, you need to know what you want to do. What I want to do now is:
Write some programs
Compile them
Package it into a jar package
Place them where they should be placed
Run them
For the sake of simplicity, write only one program, that 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. To achieve the preceding purpose, you can manually use javac, copy, jar, and java. However, if you have hundreds of classes, during deployment,
Java is a hard job. Now let's take a look at how ant completes them elegantly.
To run ant, you need a build. xml file. Although this name is not required, we recommend that you do this.
Below is a complete build. xml, and then we will explain each sentence in detail.
<? Xml version = "1.0" encoding = "UTF-8"?> -- The first sentence in build. xml has no practical significance.
<Project name = "HelloWorld" default = "run" basedir = ". "> -- all content of ant must be included in <project> </project>. name is the name you give it. basedir is the root directory of the job. indicates the current directory. Default indicates what to do by default.
<Property name = "src" value = "src"/> -- similar to the variables in the program, why do you think about the functions of the variables?
<Property name = "dest" value = "classes"/>
<Property name = "hello_jar" value = "hello1.jar"/>
<Target name = "init"> -- to avoid copying, we can define the target folder at the beginning so that ant can directly put the result in the target folder.
<Mkdir dir = "$ {dest}"/>
</Target>

-- Write everything you want to do as a target with a name. depends is the target on which it depends, before executing this target, for example, compile, ant will first check whether init has been executed. If so, execute compile directly, if not, the system will first execute the target on which it depends, for example, init, and then execute the target
<Target name = "compile" depends = "init"> -- compile:
<Javac srcdir = "$ {src}" destdir = "$ {dest}"/>
</Target>
<Target name = "build" depends = "compile"> -- create a jar package
<Jar jarfile = "$ {hello_jar}" basedir = "$ {dest}"/>
</Target>
<Target name = "run" depends = "build"> -- run
<Java classname = "test. ant. HelloWorld" classpath = "$ {hello_jar}"/>
</Target>
<Target name = "clean"> -- delete
<Delete dir = "$ {dest}"/>
<Delete file = "$ {hello_jar}"/>
</Target>
<Target name = "rerun" depends = "clean, run"> -- run again
<Ant target = "clean"/>
<Ant target = "run"/>
</Target>
</Project>

Okay. The explanation is complete. Check your ant.
Create a src folder and put HelloWorld. java in the directory according to the package.
Put the build. xml file in a folder and enter the folder in cmd,
Type ant in the command line and you will find that all tasks are completed. After changing the code, you only need to type ant again.
Sometimes we may not want to run the program, but just want to execute one or two of these steps. For example, I only want to redeploy and do not want to run the program, Type
Ant build
Every task in ant can call ant + target name like this.
Okay, so a simple ant task is completed.

 

Two things are introduced:
1, propery
2. xml include
Both of these two items have the function of separating the content in <propery/> In build. xml and using them together.
In addition, they have their own characteristics:
Propery is easy to maintain and requires only simple key-value pairs, because not all users like the xml format.
Xml include not only extracts attributes, but also targets.
Or the previous example:
For example, if we want to extract the three attributes src1 src2 src3 from xml, we can create a file named all. properties.
Content
Src1 = D :\\ study \ ant \ src1
Src2 = D :\\ study \ ant \ src2
Src3 = D :\\ study \ ant \ src3
Then you can write your build. xml file like this. Others only need to change the configuration file, instead of changing 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 will see such a target
<Target name = "test">
<Ant dir = "$ {src1}" target = "test"/>
<Ant dir = "$ {src2}" target = "test"/>
<Ant dir = "$ {src3}" target = "test"/>
</Target>
Sometimes you want to add several targets to build. xml of each group. One way is to write them in each group and then call
But there is a better way.
You can write an include. xml file with the following content:
<? 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 of the three groups, and add the following content to each file:
<! -- Include a xml file, it can be common propery, can be also a target -->
<! DOCTYPE project [
<! ENTITY share-variable SYSTEM "file: ../include. xml">
]>
& Share-variable;
As shown in the following figure:
At this time, you only need to add propery and target in include. xml, and these propery and target will be added at the same time in the three build. xml files.
It does not make the build. xml of the three groups more complex.
<? Xml version = "1.0" encoding = "UTF-8"?>
<! -- Include a xml file, it can be 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>

After you have mastered the above content, you will know how to write a good ant, but you will find that when you really want to do it, you cannot build it right away. xml, because you know too few ant default commands. this
If you want to complete the task and improve yourself, there are many ways:
1. Many open-source programs contain build. xml to see how they are written.
2. ant's document, which details various default commands of ant and its rich
3. google, never forget it
OK. As you write more and more ant builds, the more commands you know, and the more powerful ant is in your hands.
This is a process of gradual accumulation.

Ant's example is very easy to find. A variety of open-source frameworks will come with a build. xml for a closer look, there will be a lot of gains
Another one is often used, but in open-source framework build. xml, there is generally no cvs
If you are using remote cvs, you can use
<Xml version = "1.0" encoding = "UTF-8"?>
<Project>
<Property name = "cvsroot" value = ": pserver: 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 directly write build. xml in eclipse.
Because eclipse provides the prompt function and automatically supplements the function, it can help you get twice the result with half the effort.
To use this method, you only need to create a project and then create a file named build. xml. Then you can write your ant build in it.
But always remember http://www.apache.org/you can always find your own stuff

Java Ant build. xml

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.