From:
anon-391004
Views: 219
Comments: 0
Portlets and Apache Portals ,hacking dumbs intitle index of ebooks, chlidren's libraries, livingston county ny public library, cameron village library readings
Slide 1: ANT
Another Neat Tool
Slide 2: Objective
Build Tool
– –
What is a build tool Why do we need a build tool What is ANT Why ANT When to use ANT How ANT works
ANT
– – – –
Structure of Build File Installing and Running ANT More with ANT
Slide 3: What is a build tool
A build tool is a programming utility that is used when building a new version of a program. It ensures that source files that have been updated and files that are dependent on them will be compiled into a newer version of the program.
Slide 4: Why do we need build tools
Creating a product from source may take several steps: compile, link, copy files to various directories, remove intermediate files, generate documentation. It becomes problematic to do all these steps manually, first of all because it’s boring, second because it is error-prone. The objective should be an automated tool that does all the work for you. Type or click one command and create a final product. There are a couple of ways this can be done:
– –
Write a batch file or script
The scripts tend to be hard to maintain Make Ant
Use a tool designed for the task
Slide 5: What is ANT
Java based free build tool from Apache Build IN Java, USING Java, and FOR Java ANT uses XML based configuration “Build” file to drive its actions. It is easy to learn and to use, to create powerful Ant build files to compile and bundle applications in .jar, .ear, or .war files, and to deploy J2EE software applications
Slide 6: Why ANT
Written in Java so it’s a Cross-Platform build tool, unlike MAKE which is UNIX based. Cross platform build files support developers working on different operating systems It is extended using Java Classes , unlike other models where it is extended using shell commands. This makes it easily extensible. Instead of writing shell commands, the configuration files are XML based which are easy to read and modify.
Slide 7: Why ANT Contd…
Faster since each command is executed from within JVM.
–
Each command is a thread unlike shell based buildtools where each command is a process.
One-time setup hassle provides ea sy building of a project Ant's Debug Options are very helpful
Slide 8: When should one use ANT
On any project with more than a few source files Every day for every build…
Slide 9: ANT Directory Structure (Recommended)
Slide 10: How ANT works
Each build file has exactly one project. Each Build File is made up of at least one target.
–
Examples are: 'compile', ‘build', 'clean', etc. which are executed in a sequence
Each Target is made up of Tasks
–
Targets can have Dependencies
– – –
Examples: 'install' depends on 'compile' Can handle cascading dependencies Each Dependency is handled only once
Slide 11: Structure of BUILD File
<?xml version="1.0"?> <project name="Test" default="jar" basedir="."> <target name="init"> <property file=”build.properties”/> <path id="classpath"> <pathelement path="${weblogic.jar}"/> </path> </target> <target name="prepare" depends="init"> <mkdir dir="${build.dir}/META-INF"/> </target>
Slide 12: Structure of BUILD File Contd...
<target name="clean" depends="init"> <delete dir="${build.dir}"/> </target> <target name="compile" depends="prepare"> <javac srcdir="${src.dir}" destdir="${build.dir} classpathref="classpath"/> </target> <target name="jar" depends="compile"> <jar destfile="${dest.dir}/${name}.jar" basedir="${build.dir}"/> </target> </project>
Slide 13: Project tag
First statement should be – <?xml version="1.0"?> Top level XML tag is ‘project’; there should only be one project tag in a build file. Project tags specify the basic project attributes and have 3 properties: - name : the name of the project. - default target : the default target to use when no target is supplied. - basedir : the base directory from which all path calculations are done. This attribute might be overridden by setting the "basedir" property beforehand. When this is done, it must be omitted in the project tag. If neither the attribute nor the property have been set, the parent directory of the build file will be used. - All three are optional <project name="Test" default="build" basedir=".">
Slide 14: Targets
A project is made up of a number of targets that tell Ant what it should do like create initial directories, compile source, create javadoc, create jar files, etc. Targets consist of a series of tasks that are specific actions for ant to take When starting Ant, you can select which target(s) you want to have executed. When no target is given, the project's default is used. Targets have these attributes
– –
name (required) : All targets must have a name such as “compile”, “init”, “clean” depends (comma separated list of other targets) : Targets can depend on each other. A target that depends on another target will demand that the other target get executed first
Slide 15: Targets Contd…
– – –
description : Can have a “description” to explain the target's purpose If : “if” will execute a target only if a given property is defined Unless : “unless” will only execute a target if a given property is not defined
Each target is only executed once regardless of the number of other targets that depend on it Overall structure of targets: <target name="A"/> <target name="B" depends="A"/> <target name="C" depends="B"/> <target name="D" depends="C,B,A"/> <target name="build-module-A" if="module-A-present"/> <target name="build-own-fake-module-A" unless="module-A-present"/>
Note : If no if and no unless attribute is present, the target will always be executed.
Slide 16: Useful Target Definitions
Init : Sets up properties that will be used throughout the build file. Properties can be set directly or by specifying a properties file. Prepare : Create any directory structure which is needed. Clean : clean is useful for enabling clean builds. Generally just deletes stuff from previous runs. (ant clean build) Compile : Compile some/all of your source files in this target Jar : Creates a jar file from the stuff you’ve built
Slide 17: Tasks
Each target comprises one or more tasks Task is a piece of executable Java code (e.g. javac, jar, etc) Tasks do the actual “build” work in Ant Ant has core (built in) tasks, optional tasks and the ability to create own tasks
Slide 18: Tasks Contd…
Example:
<name attribute1="value1" attribute2="value2" ... />
<target name="prepare" depends="init“ >
<mkdir dir="${build}" />
</target> <target name="build" depends="copy" >
<javac srcdir="src" destdir="${build}"> </javac>
</target>
Slide 19: Tasks Contd…
Archive Tasks Compile Tasks Deployment Tasks Documentation Tasks File Tasks Property Tasks
Slide 20: Tasks Contd…
javac - The javac task compiles Java source into class files,just as the javac command-line tool does. Ant will recompile only those files that have changed. java - Execute a Java class javadoc - Generates JavaDoc from your source files jar (and war) - Create JAR files mkdir - Makes a directory copy - Copies files to specified location exec - allows different commands to be executed based on the OS it is executing on.
Slide 21: Tasks Contd…
delete - Deletes specified files parallel - Runs two or more Ant tasks (not targets) simultaneously in separate threads Import - Includes another build file into the current file echo - Prints a message to the console (default) or a file antcall - Calls another target within the same build file ant - Calls another target on a different build file FTP - lists, gets, puts and deletes files on an FTP server
Note : You can also write your own tasks.
Slide 22: Properties
Property has a name and a value; the name is casesensitive. Properties may be used in the value of task attributes. This is done by placing the property name between "${" and "}" in the attribute value. Example: <property name=“src” value=“/home/src”/> <property file=“build.properties”/> Can use ${src} anywhere in build file to denote /home/src Ant provides access to all system properties as if defined by the <property> task
Slide 23: Path Structures
Ant provides means to set various environment variables like PATH and CLASSPATH. Example of setting CLASSPATH:
<classpath> <pathelement path="${classpath}"/> <pathelement location="lib/helper.jar"/> </classpath
Slide 24: Command Line Arguments
-buildfile/ -file / -f buildfile – specify build file to use targetname – specify target to run (instead of running default) -debug – prints debug information -logfile filename – log the output in the given file -D<property>=<value> - use value for given property -help, -h -version : print the version information
Slide 25: Installing Ant
Download Ant binary distribution from: http://ant.apache.org/bindownload.cgi Set ANT_HOME to where you installed Ant Include $ANT_HOME/bin in PATH Make sure JAVA_HOME is set to point to JDK Do not ever set CLASSPATH. Ant does not need it, it only causes confusion and breaks things. Assume Ant is installed in c:\ant\. The following sets up the environment: set ANT_HOME=c:\ant set JAVA_HOME=c:\j2sdk1.6.0_24 set PATH=%PATH%;%ANT_HOME%\bin Note: To build and use Ant, you must have a JAXP-compliant XML parser installed and available on your classpath, such as Xerces.
Slide 26: Running Ant
Type “ant” at the command line Automatically looks for build.xml file in current directory to run Type “ant –buildfile buildfile.xml” to specify another build file to run ant –help
–
lists other command-line options
Slide 27: More with ANT
<mail> : using this task a mail carrying the build result can be sent to the specified recipients. Recording the output : <record name="${ant.file}.log“ loglevel="verbose" />
Slide 28: References
http://ant.apache.org/manual http://ant.apache.org/faq.html http://en.wikipedia.org/wiki/Apache_Ant