Ant installation Configuration notes and best practices

Source: Internet
Author: User



Ant Installation/Configuration notes
Author: Che Dong posted on: 2003-03-06 17:03 Last updated: 2007-04-12 11:04
Copyright NOTICE: You can reprint, reprint, please be sure to hyperlink form to indicate the original source and author information and this statement.
Http://www.chedong.com/tech/ant.html
--------------------------------------------------------------------------------
Content Summary:
Ant is a Java-based automated scripting engine that is formatted as XML. In addition to doing Java compilation-related tasks, ant can also implement many application calls through Plug-ins.


Ant's Basic concept:
Ant installation: Unpack, set path
Ant usage: The best learning is just a simple and practical example of getting started ...
Ant's basic concept: Java's makefile
When a code project is large, each recompile, package, test, etc. will become very complex and repetitive, so the C language has make script to help the batch completion of these tasks. Application in Java is platform independent, of course, do not use platform-related make script to complete these batch tasks, ant itself is such a process scripting engine, used to automate the calling program to complete the project's compilation, packaging, testing and so on. In addition to being platform independent based on Java, the format of the script is xml-based and is better maintained than the make script.


Each ant script (default called build.xml) sets a set of tasks (target): For example, the following tasks may be required for a generic project.

Task 1:usage Print Help information for this script (default)
Task 2:clean <--init empty initialization environment
Task 3:javadoc <--build <--init generate Javadoc
Task 4:jar <--build <--init generate jar
Task 5:all <--jar + Javadoc <--build <--Init completes all of the above tasks: Jar Javadoc
Multiple tasks often contain dependencies: for example, the entire Application Package task (JAR) relies on a compilation task (build), and the compilation task relies on the entire environment initialization task (INIT), and so on.

Note: I see that the names in the ant scripts for many projects are basically consistent, such as: compiling is generally called build or compile; Packaging is usually called a jar or a war; The resulting document is generally named Javadoc or Javadocs, and all tasks are performed. In each task, ant invokes some external applications based on the configuration and is executed with the appropriate parameters. Although Ant can invoke a wide variety of external applications, the most commonly used are 2, 3: Javac Javadoc jars, for example.
Ant Installation
After you unpack, add the path to the ant bin in the system executable path, such as adding the following configuration to the/etc/profile on the Gnu/linux:
Export Ant_home =/home/ant
Export Java_home =/usr/java/j2sdk1.4.1
Export PATH = $PATH: $JAVA _home/bin: $ANT _home/bin

After executing ant, if you do not specify profile ant to find build.xml this profile by default and perform tasks based on the configuration file, the default task settings can point to the most common tasks, such as build, or point to print help: usage, Tell the user that there are those scripting options available to use.


Use of ant

The best learning process is to understand the open The Build.xml script in the source project is then simplified to a simpler one, based on its own needs, and many very engineered projects on Ant and Apache: Simple to use, and very adaptable, since these projects are often built from the most immediate needs of the developer.
The following is an example of a Weblucene application: Modify the Build.xml from Jdom:

<project default = "Usage" Basedir = "." >

<!--===================================================================-->
<!--initialization target-->
<!--===================================================================-->
< target name = "Init" >
< Tstamp/>
< property File = "${basedir}/build.properties"/>
< property name = ' name ' value = ' projectfullname '/>
< property name = ' name ' value = ' project_name '/>
< property name = "Version" VALUE = "0.2"/>
< property name = ' Year ' value = ' 2003 '/>

< echo message = "-----------${name} ${version} [${year}]------------"/>

< property name = ' Debug ' value = ' off '/>
< property name = ' optimize ' value = ' on '/>
< property name = ' deprecation ' value = ' on '/>

< property name = "Src.dir" value = "./src/web-inf/src"/>
< property name = "Lib.dir" value = "./src/web-inf/lib"/>
< property name = "Packages" value = "com.chedong.*,org.apache.lucene.*"/>

< property name = "Build.src" value = "./src/web-inf/build"/>
< property name = "Build.dest" value = "./src/web-inf/classes"/>
< property name = "Build.javadocs" value = "./src/doc"/>

< path ID = "Classpath" >
< Pathelement Path = "${jsdk_jar}"/>
< Fileset dir = "${lib.dir}" >
< 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 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 JSDK 2.3 classpath. '/>
< echo message = ""/>
< echo message = "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.dest}"/>

<!--copy src files-->
< copy Todir = "${build.src}" >
< Fileset dir = "${src.dir}"/>
</copy >
</Target >

<!--===================================================================-->
<!--compiles the source directory-->
<!--===================================================================-->
< target name = "Build" depends = "PREPARE-SRC" >
< Javac Srcdir = "${build.src}"
Destdir = "${build.dest}"
debug = "${debug}"
optimize = "${optimize}" >
< Classpath refID = "Classpath"/>
</Javac >
</Target >

<!--===================================================================-->
<!--creates the class package-->
<!--===================================================================-->
< target name = "Jar" depends = "Build" >
< jar Jarfile = "${lib.dir}/${name}.jar"
Basedir

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.