Java build tool-ant Application Guide (1)

Source: Internet
Author: User

Java build tool-ant Application Guide (1)

This article uses the latest ant 1.5.1 For example, this article introduces the installation and configuration, basic applications, and some advanced topics of this excellent build tool. The latest ant is a http://jakarta.apache.org/ant.

Ant is a Java-based build tool. Theoretically, it is similar to make in C, but it is superior to make. Most existing build tools, such as make, gnumake, nmake, and jam, have one or more shortcomings, for example, it is prone to errors because it depends on a specific platform, the configuration file is too complex, or the format cannot be checked. Compared with these tools, ant's two features determine that it is an excellent build tool:

1. Java-based implementation. It has good cross-platform performance. At the same time, you can add new Java classes to expand ant functions without having to know different scripting languages on different platforms.

2. XML-based configuration file. Ant uses the XML tree to describe the relationship between target and task. The file structure is clear and easy to read and write, and XML format control is used to avoid build operation failures caused by configuration file errors.

Installation and configuration

Antinstallation is very simple, just remove the jakarta-ant-1.5.1-bin.zip downloaded from the network to a directory (the following assumes that the installation is in Directory D:/jakarta-ant-1.5.1 ). Next, you need to configure environment variables:

Set ant_home = D:/Jakarta-ant- 1.5.1 // Note that the ant installation directory is not the bin subdirectory
Set Path = % PATH %; % ant_home %/bin;

Before configuring environment variables, make sure that the java_home system variables are correctly set. Enter the ant command. The following output indicates that the ant tool has been successfully installed:

Buildfile: Build. XML does not exist!
Build failed

The prompt indicates that the build. xml configuration file does not exist in the current directory, but it indicates that ant is successfully running.

Quick Start

Let's use helloworld, the simplest and most classic example, to feel ant.

// Helloworld. Java
Package com. sharetop. antdemo;
Public class helloworld {
Public static void main (string ARGs []) {
System. Out. println ("Hello world .");
}
}

To allow ant to compile this file, you must first compile a build configuration file. In general, this file is named build. xml.

<? 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 = "Hello. 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 = "com. sharetop. antdemo. helloworld" classpath = "$ {hello_jar}"/>
</Target>
</Project>

Let's take a look at the content of this file. It describes the following information: The project name is helloworld. The project has four targets: init, compil, build, and run. The default value is run. Compile only has one task javac. The source file is located in the src directory, and the output class file should be placed in the classes directory. The build task is jar, and the generated JAR file is hello. Jar. When it is packaged, it uses classes as the root directory. Run runs the helloworld class and uses hello. jar as the classpath. There is a dependency between the four targets, which is specified by depends. That is, if target a depends on target B, target B is first executed before target a is executed. Therefore, from the output of running the default target (run) below, the execution sequence of the four targets is: init → compile → build → run. The file directory structure 1 is shown. The helloworld. Java file is located in the src/COM/sharetop/antdemo subdirectory.



Figure 1 directory structure of the ant_demo Application


Enter the command: ant in the command line and run it. The following output is displayed:

If the configuration file name is not build. XML, for example, build_front.xml, you can use the-buildfile command parameter to specify:

G:/mydoc/ant_demo> ant-buildfile build_front.xml

You can also run a specified target separately. For example, you can run the following command to compile and not package the target:

G:/mydoc/ant_demo> ant compile


Find the compiled helloworld. Class file in the corresponding directory.

Let's take a look at the build above. xml configuration file. Three attributes are defined at the beginning of the file, respectively specifying the source file output path, the class file output path, and the generated JAR file name, all the references to these paths are referenced by a $ {property name. Therefore, pay attention to the principle "directory definition and directory reference should be separated ". (To be continued)

 

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.