Ant survey posture (1)

Source: Internet
Author: User

Ant survey posture

1. Install the ant environment (win environment)
1. Download the ant compressed package and decompress it on D: \ apache-ant-1.9.0.
2. Set the environment variable ant_home D: \ apache-ant-1.9.0 to add "% ant_home % \ bin" to the path environment variable (if you have a Java environment)
3. Run CMD and enter ant. The prompt is:
Buildfile: Build. XML does not exist!
Build failed

Indicates OK.

Ii. Hello World
1. Create a new project and create a class named helloworld. The Code is as follows:
Package test. ant; // pay attention to the Package Structure
Public class helloworld {
Public static void main (string [] ARGs ){
System. Out. println ("Hello yang2 ...");
}
}
2. Create a build. xml file under the root directory of the current project (similar to the src directory), and copy the following content to the build. xml file.
<? 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>
3. Enter the current project directory in cmd, enter ant, and press enter to execute the ant script. As follows:
D: \ springintegrationworkspace \ anttest> ant
Buildfile: D: \ springintegrationworkspace \ anttest \ build. xml

Init:

Compile:
[Javac] D: \ springintegrationworkspace \ anttest \ build. xml: 10: Warning: 'encloud.com
Eantruntime 'was not set, defaulting to build. sysclasspath = last; set to false fo
R repeatable builds

Build:

Run:
[Java] Hello yang2...

Build successful
Total time: 0 seconds

D: \ springintegrationworkspace \ anttest>

4. The configuration items in build. XML are described in detail below:
<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.