Install and use ant

Source: Internet
Author: User
Tags echo display echo message

Ant is a very good OpenSource sub-project of jakarta and a java-based compilation tool. The following describes how to install ant in linux:

  1. Download
SlaveHttp://ant.apache.org/bindownload.cgiYou can download the latest tar package: apache-ant-1.6.2.tar.gz, if it is a windows environment is a zip file, decompress, in the system environment variables set ANT_HOME to f: projectoolsapache-ant-1.6.2, and f: projectoolsapache-ant-1.6.2in directory added to classpath, then you can use

  2./lInstall and decompress the package to/usr.Under ocal
> Tar zxpvf apache-ant-1.6.2.tar.gz
> Ln-s apache-ant-1.6.2 ant

  3. Set the environment
Set ANT_HOME to the current user's. bash_profile file/home/admin/. bash_profile

[Admin @ tangtang home] $ su-admin
[Admin @ tangtang home] $ vi. bash_profile
Export ANT_HOME =/usr/local/ant
Export PATH =/usr/local/ant/bin: $ PATH

In windows, set % ANT_HOME % and add the full path of % ANT_HOME % in to % path %.

  4. Test
Run the ant command to test the running status.
[Admin @ tangtang home] $ ant
Buildfile: build. xml does not exist!
Build failed

[Admin @ tangtang home] $ ant-version
Apache Ant version 1.6.2 compiled on July 16 2004

If this error occurs: Exception in thread "main" java. lang. NoClassDefFoundError: org/apache/tools/ant/launch/Launcher
In linux, ant lib is installed by default, and/etc/ant is modified. in conf, ANT_HOME =/usr/share/ant correctly installs the ant address for you, such as/usr/local/ant.

  5. build script
In a linux environment, the following is an example of a build script:

# Build script
#! /Bin/sh
# Go to the parent directory
Cd 'dirname $0 '/..
# Obtain the current directory as PROJECT_HOME
PROJECT_HOME = 'pwd'
# Set JAVA_HOME
Export JAVA_HOME =/usr/cyber/java
# Obtain CLASSPATH
CLASSPATH1 = $ CLASSPATH
# Add CLASSPATH
CLASSPATH =$ {PROJECT_HOME} webdeskweb-INFconf: $ {PROJECT_HOME} webapps
WEB-INFclasses: $ CLASSPATH
# Ant build,-buildfileParameter, which is a custom build. xml file, $ @ is a parameter Array
/Usr/local/ant/bin/ant-buildfile $ {PROJECT_HOME}/build. xml "$ @"
# Build script ended

In a windows environment, the following is an example of the build. bat script:
# Build. bat
# Disable echo display
@ Echo off
# Set % JAVA_HOME %
If "% JAVA_HOME %" = "" set JAVA_HOME = f: oolsjava
# Set % ANT_HOME %
If "% ANT_HOME %" = "" set ANT_HOME = f: oolsant
# Setting PROJECT_HOME
Set PROJECT_HOME = % CD % ..
Set CLASSPATH_BAK = % CLASSPATH %
Set CLASSPATH =
# Execute build
% ANT_HOME % inant. bat-buildfile .. uilduild. xml % 1% 2% 3% 4% 5% 6% 7% 9

   6. build the configuration file
Under the $ {PROJECT_HOME}/build directory, You need to define two files: build. properties and build. xml.
The build. properties file defines some constants of build.
# Build. properties
Project = tangtang
Version = 1.1.1
# Use classic to compile, that is, use ant to compile
Build. compiler = classic
# Using jikes for compilation
# Build. compiler = jikes

Year = 2004
Debug = on
Optimize = on
Deprecation = on

OS = linux
Author = tangtang
Email = syvin_tom@hotmail.com
Url =Www.tangtang.org
Company = tangtang.org

The build. xml file is the main configuration file for ant compilation. ant is powerful and needs to be represented by corresponding configuration items.
<?xml version="1.0" encoding="gb2312"?>

<!-- Build file for project -->

<project name="cyber" default="jar" basedir=".">
<property file="${user.home}/.ant.properties" />
<!-- ant build properties -->
<property file="build.properties"/>

<property name="build.dir" value=".."/>
<property name="build.src" value="${build.dir}/webapps/WEB-INF/src/java"/>
<property name="build.dest" value="${build.dir}/webapps/WEB-INF/classes"/>
<property name="build.lib" value="${build.dir}/webapps/WEB-INF/lib"/>
<property name="build.ext" value="./lib"/>
<property name="build.tpl" value="${build.dir}/webapps/WEB-INF/templates"/>
<property name="build.encoding" value="gb2312"/>

<property name="src.java.dir" value="../src/java"/>
<property name="javadoc.destdir" value="../docs/api"/>
<property name="javadoc.link" value="http://www.tangtang.org/java/docs/api/"/>
<property name="final.name" value="${project}-${version}"/>
<property name="dist.root" value="${build.dir}/dist"/>
<property name="dist.dir" value="${dist.root}/${final.name}"/>

<path id="classpath">
<pathelement path="${java.class.path}/"/>
<fileset dir="${build.lib}">
<include name="*.jar"/>
</fileset>
<fileset dir="${build.ext}">
<include name="*.jar"/>
</fileset>
</path>

<property name="classpath" refid="classpath"/>
<!-- =================================================================== -->
<!-- prints the environment -->
<!-- =================================================================== -->
<target name="env">
<echo message="build.compiler = ${build.compiler}"/>
<echo message="java.home = ${java.home}"/>
<echo message="user.home = ${user.home}"/>
<!--echo message="java.class.path = ${java.class.path}"/-->
<echo message="classpath = ${classpath}"/>
</target>

<!-- =================================================================== -->
<!-- Prepares the build directory -->
<!-- =================================================================== -->
<target name="prepare" depends="env">

<tstamp/>
<filter token="year" value="${year}"/>
<filter token="version" value="${version}"/>
<filter token="date" value="${DSTAMP}"/>
<!--
<mkdir dir="${build.dir}"/>
<mkdir dir="${build.dest}"/>
<mkdir dir="${build.src}"/>
-->
<!-- chose a class thats from j2ee.

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.