An ant tutorial for beginners

Source: Internet
Author: User

ant introduction
1. To build an ant environment, you must first build an ant environment. The procedure is simple:
1) install JDK and set java_home, path, and class_path (which should be known to anyone reading Article )
2) download the ant address www.apache.org to find a version you like, or download the latest version.
3) decompress ant and decompress it, and put it in a directory as simple as possible, such as D: \ ant-1.6 although you do not have to do this, but it is advantageous to do so.
4), set the bin directory in the ant_home path to add the ant_home directory
5), and test your settings, start --> Run --> cmd to enter the command line --> type 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 helloworld in every language, one of the simplest applications can make people feel ant
1. First of all, you need to know what you want to do. What I want to do now is:
compile some Programs
compile them
package it into a jar package
place them in
Run them
here, only one program is written for the sake of simplicity, helloworld. the Code of the Java program is as follows:
package test. ant;
public class helloworld {
Public static void main (string [] ARGs) {
system. out. println ("Hello world1");
}< BR >};

2. To achieve the preceding purpose, you can manually use javac, copy, jar, and Java. However, if you have hundreds of classes, during deployment, javac, copy, jar, and Java will be 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"?>
<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>

Explanation:
<? XML version = "1.0" encoding = "UTF-8"?>
The first sentence in build. XML has no practical significance.

<Project name = "helloworld" default = "run" basedir = ".">
</Project>
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.

<Property name = "src" value = "src"/>
Similar to the variables in the program, why do you think about the functions of the variables?

<Target name = "compile" depends = "init">
<Javac srcdir = "$ {SRC}" destdir = "$ {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

Such as our plan
Compile:
<Target name = "compile" depends = "init">
<Javac srcdir = "$ {SRC}" destdir = "$ {DEST}"/>
</Target>

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>
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:
<Target name = "init">
<Mkdir dir = "$ {DEST}"/>
</Target>
Two more targets are added for more functions.
Delete the generated file
<Target name = "clean">
<Delete dir = "$ {DEST}"/>
<Delete file = "$ {hello_jar}"/>
</Target>
Run again. This shows how to call other targets in a target.
<Target name = "Rerun" depends = "clean, Run">
<Ant target = "clean"/>
<Ant target = "run"/>
</Target>

Okay. The explanation is complete. Check your ant.
Create a SRC folder and put helloworld. Java in the directory according to the package.
Complete the build. xml file
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.
 
 

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.