Java Build tool ant small Note (i)

Source: Internet
Author: User

Ant Introduction 

Ant is a Java-based build tool. It is theoretically similar to the make tool, but overcomes some of the inherent flaws of make.

Traditional make is a build tool based on the operating system shell, although it is also possible to extend the make tool based on the working OS, but it is difficult to build across platforms. Ant is based on Java extensions and is defined by a task defined in target in XML. Each of these tasks is a class that implements a specific task interface. Ant also provides shell commands that the exec task allows to invoke different operating systems.

Ant main Elements Introduction

Ant uses an XML file to define the build process, and each build file will include a project and at least one target. Target will contain the task element. Each task element can be referenced by an id attribute.

1.Projects

The project element has three properties: Name, Default, Basedir. It can also contain a description element to describe the project.

The name of the Name:project, not the required attribute;

Default: The target to execute is not specified when the build is executed;

Basedir: Sets the base directory, then all subsequent relative positions are set to the base path, and if not set, the directory containing the build file is the base directory;

<?XML version= "1.0" encoding= "UTF-8"?><Projectname= "Projectexample"default= "Default"Basedir=".">    <Description>Learn how to use Project</Description>    <Targetname= "Default"  >        <Echo>${basedir}</Echo>    </Target>    <Targetname= "Echobasedir">            <Echo>${basedir}</Echo>        </Target></Project>
View Code

2.Targets

Each project defines one or more target.target as the set of tasks to be executed. You can specify the target to execute when you start the ant execution, and if you do not specify the default target/of the project settings A target can use depends to define other target that it relies on, such as a packaged target that relies on a compiled target.

<?XML version= "1.0" encoding= "UTF-8"?><!--====================================================================== June 1, 2015 9:35:09                                                                     Targetexample description Hou ======================================================================  -<Projectname= "Targetexample"default= "Default">    <Description>Description</Description>    <!--================================= Target:default =================================  -    <Targetname= "Default"depends= "Depends"Description= "description">        <Echo>excuting default target</Echo>    </Target>    <!---------------------target:depends----------------------------  - - -  -    <Targetname= "Depends">        <Echo>Excuting depends target</Echo>    </Target></Project>
View Code

3.Tasks

A task is a piece of code that can be executed. A task can have multiple properties (attribute). The values of these properties (attribute) can refer to the property, and the referenced property is parsed into a specific value before the task is executed. The general structure of the task is as follows

<name attribute1= "value1" attribute2= "value2" .../>

Ant has built-in some common tasks and also provides a convenient extension mechanism.

<?XML version= "1.0" encoding= "UTF-8"?><!--====================================================================== June 1, 2015 9:59:23                                                                     Taskexample description Hou ======================================================================  -<Projectname= "Taskexample"default= "Default">    <Description>Description</Description>        < Propertyname= "DirName"value= "Taskdir"></ Property>    <!--================================= Target:default ================================= /c0> -    <Targetname= "Default"Description= "description">        <Echo>List current directory</Echo>        <execexecutable= "ls"></exec>        <Echo>Make [${dirname}] Directory</Echo>        <mkdirdir= "${dirname}"/>        <Echo>List current directory</Echo>         <execexecutable= "ls"></exec>        <Deletedir= "${dirname}"></Delete>            </Target>        </Project>
View Code

4.Properties

A property is an important way to achieve a custom build process by setting a string that can be reused during the build process. Typically, you define attributes directly in the build file, or you can define properties externally. The name of the property is case sensitive. The reference to the property is implemented by ${property name}. The properties in ant are similar to fields in Java, but cannot be changed once the property is set.

There are many predefined properties in Ant. First, the Java environment settings are used for all system properties that run ant, such as ${user.home}. In addition to these attributes, Ant also defines its own set of properties, including ${ant.version}, This property contains the Ant version, and ${basedir}, which is the absolute path to the project directory (defined by the directory that contains the generated files, or by the optional Basedir attribute of the project element). Properties are often used to refer to files or directories on a file system, but for platforms that use different path separators (for example,/and \), this can cause problems across different platforms. The Location property of Ant is specifically designed to include file system paths in a platform-agnostic manner.

<Projectname= "Propertiesexample"default= "Dist"Basedir=".. /">    <Description>Simple propertiesexample Build file</Description>  <!--set global Properties for this build -  < Propertyname= "src" Location= "src"/>  < Propertyname= "Build" Location= "Build"/>  < Propertyname= "Dist" Location= "Dist"/>  <Targetname= "Init">    <!--Create The time stamp -    <Tstamp/>    <!--Create The build directory structure used by compile -    <mkdirdir= "${build}"/>  </Target>  <Targetname= "Compile"depends= "Init"Description= "Compile the source" >    <!--Compile the Java code from ${SRC} to ${build} -    <JavacSrcdir= "${src}"Destdir= "${build}"/>  </Target>  <Targetname= "Dist"depends= "Compile"Description= "Generate the Distribution" >    <!--Create the distribution directory -    <mkdirdir= "${dist}/lib"/>    <!--Put everything in ${build} into the Wufengtinghai.alm-${dstamp}.jar file -    <JarJarfile= "${dist}/lib/wufengtinghai.alm-${dstamp}.jar"Basedir= "${build}"/>  </Target>  <Targetname= "clean"Description= " Clean up" >    <!--Delete The ${build} and ${dist} directory trees -    <Deletedir= "${build}"/>    <Deletedir= "${dist}"/>  </Target></Project>
View Code

Run Ant

Ant works as a command line, and can naturally be used by Unix/linux's shell and Windows CMD. It can also be called through the Java IDE, such as Eclipse.

Call ant directly from the command line so that ant looks for the default build.xml from the current directory and executes the build process using the default target. You can also specify the build file and the target name by Ant-f Buildfilename TargetName.

Eclipse provides strong support for Ant. Syntax highlighting/smart hints/outline view is supported first.

Select the build file in Eclipse, right-select "Run as"-"Ant Build" and execute.

Java Build tool ant small Note (i)

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.