Java Ant build. xml

Source: Internet
Author: User

Java Ant build. xml

1. What is ant
Ant is a build tool
2. What is building
The concept can be found everywhere. For the image, you need to take the code from somewhere, compile it, copy it to somewhere, and so on. Of course, this is not only the same, but it is mainly used to do this.
3. ant benefits
Cross-platform-Because ant is implemented in java, It is cross-platform
Easy to use-compared with ant's brother make
Clear syntax-same as make
Powerful functions-ant can do a lot of things. You may have used it for a long time and still don't know how many features it can have. When you develop some ant plug-ins, you will find more functions.
4. ant's brother make
Ant has done many things, most of which were once done by make. However, unlike objects, make applies more to c/c ++ and ant applies more to Java. Of course this is not certain, but most people do.
1. Build an ant Environment
To use ant, you must first build an ant environment. The procedure is simple:
1) install jdk and set JAVA_HOME, PATH, and CLASS_PATH (these should be known to those who read this article)
2) download the ant address http://ant.apache.org/to find your latest version.
3) decompress ant. You get a compressed package and put it in a directory as simple as possible, such as C: \ ant-1.9. Although you do not have to do this, but doing so is advantageous.
4), set ANT_HOME, and add the bin directory under the ANT_HOME directory in PATH (I set ANT_HOME: C: \ ant-1.9, PATH: % ANT_HOME % \ bin)
5) test your settings and start --> Run --> cmd to enter the command line --> enter ant and press Enter. If you see
Buildfile: build. xml does not exist!
Build failed
Congratulations, you have completed ant settings.


2. 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.


























Explanation:

The first sentence in build. xml has no practical significance.


All content of ant must be included in this file. name is the name you give it. basedir is the root directory of the job. It indicates the current directory. Default indicates what to do by default.

Similar to the variables in the program, why do you think about the functions of the variables?



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
If not, execute compile directly. If not, execute the target on which it depends, for example, init, and then execute the target
Such as our plan
Compile:



Jar package:



Run:



To avoid copying, we can define the target folder at the beginning, so that ant can directly put the results in the target folder.
Create a folder:



Two more targets are added for more functions.
Delete the generated file




Run again. This shows how to call other targets in a target.




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.


I. When to use ant
Maybe you have heard about ant and are eager to learn ant. When you finish reading the first instance above, you may feel ant is so good, maybe you feel ant is so bad, we can't make any mistakes when drawing these conclusions, although ant is very useful,
However, it is not always the best choice in any situation. For example, there are more simple and easy-to-use tools on windows, such as eclipse + myeclipse eclipse + wtp, deployment, run more than ant
Easy and convenient, but in some cases it is a good place for ant to play:
1. During server deployment
When your program development is complete and deployment personnel need to deploy it on the server, you cannot configure an eclipse + myeclipse because of installing a program, ant is a good choice at this time, because it is small and easy to configure
You have taken your build. xml to any server, you only need to make simple modifications (some settings, such as directories), and then one or two commands are completed. Isn't that a good thing.
2. in linux, This is often the case. program development is in windows, but the program must run on linux or unix.
Deploying on unix (especially unix) is troublesome. At this time, ant features come out again, because ant is cross-platform and you are building. xml can be used in most operating systems and does not need to be modified.
3. When the server maintainer does not understand Programming
Many people have had such experiences. People who use your programs do not know how to write programs. Because of version updates, You need to redeploy the program again and again to fix bugs. At this time, you will find it so difficult to teach a person. However
If there is ant, you just need to tell him to input one or two commands such as ant xxx. Everything is OK.
The above are some situations I have encountered.
After reading the above information, consider whether you need to use ant.

Learn more about ant with a little complexity
In actual work, the following situations may occur: A project is divided into multiple modules, and each group or department is responsible for one module. To test, they write a build. xml, and you are responsible for combining these modules
Write a build. xml file.
At this time, you have two options:
1. re-write a build. xml file by yourself, which will be troublesome.
2. Try to use the build. xml they have already written to reduce your work.
For example:
Suppose you have three groups, each of which is responsible for one part. Each of them has a src and a well-written build. xml.
At this time, you get their src. What you need to do is to create three folders: src1, src2, and src3. Put their src and build. xml respectively, and then write a build. xml file.





































OK, your task is finished.

OK, you have completed the task above, but do you have some feelings? In those build. xml, most of them are repeated, and you need to change a lot of things once to change the directory. Is it better for your work? The answer is yes.
Two things are introduced:
1, propery
2. xml include
Both of these two items have a function, that is, they can be used in build. xml. The content is separated and used 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.








































If you look at it yourself, you will see such a 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:






Then change the build. xml file of the three groups, and add the following content to each file:


]>
& 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.



]>


& Share-variable;
























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










Passfile = "$ {cvs. passfile}"/>

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

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.