Ant learning Summary

Source: Internet
Author: User
Tags echo message


Ant learning Summary
Ximazi original (Participation score: 1521, expert score: 100) published: 1.0 read:477Times

Ant. ant is a Java-based build tool. Theoretically, it is similar to make in (UNIX) C, but it does not have the defect of make. Ant is also a cross-platform tool thanks to Java's cross-platform nature.
1. Ant For more information see http://ant.apache.org/
2. Ant download, http://jakarta.apache.org/builds/jakarta-ant/release
3. Ant installation. for Win32, download ant's apache-ant-1.6.5-bin.zip. Decompress ant to C:/. After decompression, the default folder is Apache-ant-1.6.5 and renamed ant.
4. Ant configuration, configure environment variables in the system advanced. In the user variable, add ant_home = C:/ant. in the path of the system variable, add the following sentence: C:/ANT/bin. After the configuration is complete, log out of the user and log on again, ant can use it.
5. Ant is used to compile Java files, package jar files, war files, and generate javadoc.
Now I will introduce how to use ant. First, ant is used to make compilation of your Java program no longer so slow, and eclipse is not required, saving a little memory. First, let's test it with a very simple test project. To make ant run, you must compile build. xml. Build. xml should be placed in the root directory of your project. Since ant execution is such a process, you run cmd to enter the command line interface, enter ant in the root directory of your project, and then ant will search for build in the root directory. XML, find build. and then execute the task according to the configuration. The most important task is to compile ant build. xml.
Build. xml sets a series of tasks, that is, target.
Example: <target name = "init">
</Target>
Target is generally divided into several types according to habits:
1 usage print the help information of this script (default)
2 init initialization information, variables, etc.
3 build
4. javadoc generates javaapi documentation
5. jar package the generated file
6. Clean the files and directories in the process.
7. All tasks are executed.
The simplest task is to execute only one target, that is, build, which is my most desired task.
Let's start with an example:
<? XML version = "1.0"?>
<Project default = "build" basedir = ".">
<Property name = "appname" value = "jspsmartupload"/>
<Property name = "src" value = "src"/>
<Property name = "build" value = "WEB-INF/classes"/>
<Property name = "myclasspath" value = "$ {base}/WEB-INF/lib"/>
<Target name = "build">
<Mkdir dir = "$ {build}"/>
<Javac srcdir = "$ {SRC}" destdir = "$ {build}">
<Include name = "**/*. Java"/>
</Javac>
</Target>
</Project>
This build. XML is the ant script of a simple project that I wrote at the beginning, with many errors in the middle. In this build. XML, <project default = "build" basedir = "."> indicates that the default task is build and the root directory is the current directory. <Property name = "src" value = "src"/> defines some PATH variables for later use.
<Target name = "build">
<Mkdir dir = "$ {build}"/>
<Javac srcdir = "$ {SRC}" destdir = "$ {build}">
<Include name = "**/*. Java"/>
</Javac>
</Target>
This task segment is a build task. First, mkdir is used to create a folder and compile it.
However, an error occurred while running ant.
The error occurs because classpath is not introduced. Modify ant as follows:
<? XML version = "1.0"?>
<Project default = "build" basedir = ".">
<Property name = "appname" value = "jspsmartupload"/>
<Property name = "src" value = "src"/>
<Property name = "build" value = "WEB-INF/classes"/>
<Property name = "myclasspath" value = "$ {base}/WEB-INF/lib"/>
<Target name = "build">
<Mkdir dir = "$ {build}"/>
<Javac srcdir = "$ {SRC}" destdir = "$ {build}">
<Include name = "**/*. Java"/>
<Classpath>
<Pathelement Path = "$ {myclasspath}"/>
<Fileset dir = "./WEB-INF/lib"> <include name = "**/*. Jar"/> </fileset>
<Pathelement location = "classes"/>
</Classpath>
</Javac>
</Target>
</Project>
Run ant again.
Now we have to make more complicated attempts.
Now, we use more targets in an ant project to compile, package, and generate javadoc documents for more complex projects.
The following is an example:
<Project default = "all" basedir = ".">
<! -- ===================================================== ========================================= -->
<! -- Initialization target -->
<! -- ===================================================== ========================================= -->
<Target name = "init">
<Tstamp/>
<Property file = "$ {basedir}/build. properties"/>
<Property name = "name" value = "ant_all_test"/>
<Property name = "name" value = "project_name"/>
<Property name = "version" value = "0.1"/>
<Property name = "year" value = "2005"/>
<Echo message = "----------- $ {name }$ {version} [$ {year}] ------------"/>
<Property name = "debug" value = "off"/>
<Property name = "optimize" value = "on"/>
<Property name = "deprecation" value = "on"/>
<Property name = "src" value = "./src"/>
<Property name = "lib" value = "./lib"/>
<Property name = "build. SRC" value = "./web/WEB-INF/src"/>
<Property name = "build" value = "./web/WEB-INF/classes"/>
<Property name = "javadocs" value = "./Doc"/>
<Property name = "packages" value = "com. XXXXXXXX. *"/>
<Path id = "classpath">
<Pathelement Path = "$ {jsdk_jar}"/>
<Fileset dir = "$ {lib}">
<Include name = "**/*. Jar"/>
</Fileset>
</Path>
<Filter token = "year" value = "$ {year}"/>
<Filter token = "version" value = "$ {version}"/>
<Filter token = "date" value = "$ {today}"/>
<Filter token = "log" value = "true"/>
<Filter token = "verbose" value = "true"/>
</Target>
<! -- ===================================================== ========================================= -->
<! -- Help on usage -->
<! -- ===================================================== ========================================= -->
<Target name = "usage" depends = "init">
<Echo message = "$ {name} Build File"/>
<Echo message = "-------------------------------------------------------------"/>
<Echo message = ""/>
<Echo message = "available targets are:"/>
<Echo message = ""/>
<Echo message = "jar --> generates the $ {name}. jar file"/>
<Echo message = "Build --> compiles the source code"/>
<Echo message = "javadoc --> generates the API documentation"/>
<Echo message = "clean --> cleans up the directory"/>
<Echo message = ""/>
<Echo message = "Please rename build. properties. Default to build. properties"/>
<Echo message = "and edit build. properties to specify jsdks 2.3 classpath."/>
<Echo message = ""/>
<Echo message = "See the comments inside the build. xml file for more details."/>
<Echo message = "-------------------------------------------------------------"/>
<Echo message = ""/>
<Echo message = ""/>
</Target>
<! -- ===================================================== ========================================= -->
<! -- Prepares the source code -->
<! -- ===================================================== ========================================= -->
<Target name = "prepare-Src" depends = "init">
<! -- Create directories -->
<Mkdir dir = "$ {build. SRC}"/>
<Mkdir dir = "$ {build}"/>

<! -- Copy SRC files -->
<Copy todir = "$ {build. SRC}">
<Fileset dir = "$ {SRC}"/>
</Copy>
</Target>

<! -- ===================================================== ========================================= -->
<! -- Compiles the source directory -->
<! -- ===================================================== ========================================= -->
<Target name = "build" depends = "prepare-Src">
<Javac srcdir = "$ {build. SRC }"
Destdir = "$ {build }"
DEBUG = "$ {debug }"
Optimize = "$ {optimize}">
<Classpath refID = "classpath"/>
</Javac>
</Target>
<! -- ===================================================== ========================================= -->
<! -- Creates the class package -->
<! -- ===================================================== ========================================= -->
<! -- <Target name = "jar" depends = "build">
<Jar jarfile = "$ {lib}/$ {name}. Jar"
Basedir = "$ {build }"
Includes = "**"/>
</Target> -->
<! -- ===================================================== ========================================= -->
<! -- Creates the API documentation -->
<! -- ===================================================== ========================================= -->
<Target name = "javadoc" depends = "build">
<Mkdir dir = "$ {javadocs}"/>
<Javadoc packagenames = "$ {packages }"
Sourcepath = "$ {build. SRC }"
Destdir = "$ {javadocs }"
Author = "true"
Version = "true"
Use = "true"
Splitindex = "true"
Windowtitle = "$ {name} API"
Doctitle = "$ {name}">
<Classpath refID = "classpath"/>
</Javadoc>
</Target>
<Target name = "all"
Depends = "usage, init, prepare-Src, build, javadoc"
Description = "make all task."/>

</Project>
<! -- End of file -->
<Project default = "all" basedir = "."> indicates that the default target of the Project is all. Find <target name = "all" depends = "usage, init, prepare-Src, build, javadoc" Description = "make all task. "/>; this target depends =" usage, init, prepare-Src, build, javadoc "calls other target tasks. Other tasks are performed in sequence. After ant is executed, all operations are successfully executed, and the compilation and javadoc generation are successfully completed. I feel very good after my own attempts and learning. I hope everyone can do more, so that new knowledge will naturally be integrated.

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.