0% found this document useful (0 votes)
20 views82 pages

Apache ANT

This document provides an introduction to Apache Ant, a Java-based build tool that automates the software build process. It covers the advantages of using Ant, installation instructions, and the structure of build files, including targets and tasks. The document also emphasizes the importance of automating build processes to reduce manual errors and improve efficiency.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views82 pages

Apache ANT

This document provides an introduction to Apache Ant, a Java-based build tool that automates the software build process. It covers the advantages of using Ant, installation instructions, and the structure of build files, including targets and tasks. The document also emphasizes the importance of automating build processes to reduce manual errors and improve efficiency.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 82

Contents

ANT_1_Introduction to Apache ANT········································································································· 2


ANT_2_Building Sample Java Projects··································································································· 20
LC - Milestone4······································································································································· 78
Introduction to
Apache Ant

1 © 2012 WIPRO LTD | WWW.WIPRO.COM


Agenda

Introduction to Apache Ant

2 © 2012 WIPRO LTD | WWW.WIPRO.COM


Objectives
At the end of this module, you will be able to:

• Appreciate the advantages of using ANT


• Learn how to install ANT and use

3 © 2012 WIPRO LTD | WWW.WIPRO.COM


Why ANT?
• Building a software is much more than just typing and
compiling the source code
• Do you know that there are a number of steps required
to transform the source into a deployable software
solution?

4 © 2012 WIPRO LTD | WWW.WIPRO.COM


High level view of a software build process
• Let us look at a high level view of a software build process

– The team members of a project usually upload their source code


into the source code repository. Hence you may need to
download or fetch the source code from the source code
repository.

– You may need to prepare the build area. You may want to create
a set of directories. Different applications might want to have a
different directory structure.

– You may want to compile the source code.

– You may want to validate the source code. You may have a style
guide and you may want to confirm that your source code
conforms to the standards set before it is released.

5 © 2012 WIPRO LTD | WWW.WIPRO.COM


High level view of a software build process
• You may want to complete the testing of all the source code that you
have downloaded from the Source code control system.

• You may want to build the compiled code into libraries.

• Package all the components of the software - code, images,


resources, documentation etc

• You may want to produce several packages in different formats for


different target users

• Deploy the software to some standard location for use or distribution

6 © 2012 WIPRO LTD | WWW.WIPRO.COM


Why these tasks need to be automated?
• The steps mentioned earlier may vary depending upon the type of
software that you are developing.
• Each of these steps may involve many individual operations.
• Doing all these steps manually will be error prone and a tedious task.
• And all these tasks might have to be done so many number of times.
• Hence we may want to automate the build process and hence you
need the help of build tools.
• ANT is one such build tool which is platform independent.

7 © 2012 WIPRO LTD | WWW.WIPRO.COM


Introduction

• Ant stands for ‘Another Neat Tool’


• Ant is a free java based build tool from the Apache
Jakarta Group
• Originally written by James Duncan Davidson who is also
the author of Tomcat Server
• It’s like a ‘make file’ (a build file in Unix platform) but is
better than that as it is cross platform
• Uses an XML file to drive its action – build.xml
• Extremely powerful and modular
• Easily extensible

8 © 2012 WIPRO LTD | WWW.WIPRO.COM


Why would I want to use Ant?
Do you spend your day doing the following tasks manually?
•Compile code
•Package the binaries
•Deploy the binaries to the test server
•Test your changes
•Copy code from one location to another
• If you have answered yes to any of the above, then it is time to
automate the process and take away that burden from you.
• On average, a developer spends 3 hours (out of a 8 hour working
day) doing mundane tasks like build and deployment.
• ANT helps you to build your project easily. It can be used in all
platforms and hence it offers a big advantage to developers working
on different OS.
• It doesn’t need human intervention when the build process is on and
the status of the build process can be mailed to different stakeholders
at the end of the building process.

9 © 2012 WIPRO LTD | WWW.WIPRO.COM


Why would I want to use Ant?

• Ant is a java based build tool used to automate software


build process
• Can be used in different platforms
• It supports unattended builds
• It can fetch source code from Source code management
systems like CVS, SourceSafe, PVCS etc
• Ant uses an XML build file and it can be easily updated by
anyone with basic XML skills

10 © 2012 WIPRO LTD | WWW.WIPRO.COM


Why would I want to use Ant?

• Ant is a java based build tool used to automate software


build process
• Can be used in different platforms
• It supports unattended builds
• It can fetch source code from Source code management
systems like CVS, SourceSafe, PVCS etc
• Ant uses an XML build file and it can be easily updated by
anyone with basic XML skills

11 © 2012 WIPRO LTD | WWW.WIPRO.COM


Installing Ant

• Ant binary distribution can be downloaded from:


http://ant.apache.org/bindownload.cgi
• You have to set ANT_HOME variable to the
location where you installed Ant
• The $ANT_HOME\bin should be included in
PATH
• The JAVA_HOME variable should point to JDK

12 © 2012 WIPRO LTD | WWW.WIPRO.COM


Setting up variables

• Setting ANT_HOME

• Updating PATH

• Setting JAVA_HOME

13 © 2012 WIPRO LTD | WWW.WIPRO.COM


Running Ant from the command line
• The ant command syntax is as shown below
ant [options] [target [target2 [target3] ...]
• Some important options are :

-help, -h Used to display the help message.


-version Used to print the version information.
Used to print all the possible output that Ant
-verbose, -v produces.

Used to specify the path to search for JARs and


-lib <path> classes, in addition to your classpath

-buildfile <file> Specifies the name of the build file to be used for
-file <file> this build. The default one is a file called build.xml
-f <file> in the current directory.
-D<property>=<value> Sets the value of property <property> to <value>
-keep-going, -k Forces Ant to execute every target that does not
depend on a failed target. This ensures that at
least part of the build was successful.
-propertyfile <name> Loads all the properties from specified property file
14 © 2012 WIPRO LTD | WWW.WIPRO.COM
Running Ant from the command line
• Type ant command from the command prompt
Output

• The error message is because ant command looks for a build.xml file
in the current directory, and we have not created one
• Let’s learn about the build.xml and it’s various parts in the next
session

15 © 2012 WIPRO LTD | WWW.WIPRO.COM


Assignment

Installation and Setting of ANT


---------------------------------------
Step -1: Download latest stable version of Ant 1.9. It is available from
the Ant web page http://archive.apache.org/dist/ant/binaries/ or obtain it
from the Faculty.
Step -2: Extract the contents of the downloaded zip file.
Step -3: Assume Ant is installed in c:\ant\. Set up the following
environment variables:
set ANT_HOME=<path to ant installation directory e.g c:\\ant >
set JAVA_HOME=<path to JDK installation>
set PATH=%PATH%;%ANT_HOME%\bin
Step -4: In the command prompt type ant and check if it gives an error
message as shown in the previous slide

16 © 2012 WIPRO LTD | WWW.WIPRO.COM


Quiz

1. ANT is?
a. Another Nice Tool
b. A Neat Tool
c. Another Normal Tool
d. Another Neat Tool
2. Which of the following is the configuration file for ANT?
a. build.properties
b. buld.xml
c. ant.xml
d. ant.properties
3. Which of the following is ANT?
a. Testing Tool
b. Build Tool
c. Logging Framework
d. IDE

17 © 2012 WIPRO LTD | WWW.WIPRO.COM


Summary
In this session, you were able to :

• Understand the need for a build tool


• How to install ANT

18 © 2012 WIPRO LTD | WWW.WIPRO.COM


Building Simple
Java Projects

1 © 2012 WIPRO LTD | WWW.WIPRO.COM


Agenda

Structure of a build file

2 Some useful Targets

3 Some useful Tasks

2 © 2012 WIPRO LTD | WWW.WIPRO.COM


Objectives
At the end of this module, you will be able to:

• Understand the structure of build.xml


• Understand some important targets and tasks in ANT

3 © 2012 WIPRO LTD | WWW.WIPRO.COM


Structure of a Build File

4 © 2012 WIPRO LTD | WWW.WIPRO.COM


A sample build file – build.xml
<project name="My first Ant"
Output :
default="one">
D:\>ant
<target name="one">
<echo>From one</echo>
Buildfile: D:\build.xml
</target>
one:
[echo] From one
<target name="two">
<echo message="I'm from two"/>
BUILD SUCCESSFUL
</target>
Total time: 0 seconds
</project>

Type the following contents Open the command prompt and type
in a notepad and save it as ant in the location where the
build.xml build.xml file is saved

5 © 2012 WIPRO LTD | WWW.WIPRO.COM


Structure of the Build file - <project>

• The root tag of the build.xml file is project


• Every build.xml will have a single Project
• Every Project will have at least one target
• Every target will have one more task elements

<project name="My first Ant" default="one">

• The above code snippet indicates that


• The name of the project is My first Ant
• The default attribute is assigned “one”
• Here the target ‘one’ gets automatically executed as soon as we
execute the ant command
• If you call ant without any arguments it will execute one
• If you issue the ant command as ‘ant two’ it will execute target
two

6 © 2012 WIPRO LTD | WWW.WIPRO.COM


Assignment

1. Create build.xml in a notepad file


2. Save it in D:\
3. Open command prompt, go to D drive and issue the ant command
4. Note the output generated

1. In the same command prompt, type ant two


2. Note down your results

1. Now remove the default attribute in project tag


2. In the command prompt, give ant command and note your
observations

7 © 2012 WIPRO LTD | WWW.WIPRO.COM


<target>

• All targets must have a name such as “compile”,”init”, “clean”


• Can have a “description” to explain the target’s purpose
• Targets can depend on each other
– If a target depends on another target it will demand that the other
target gets executed first

<target name=“jar” depends=“compile”>


………</target>

– Multiple targets can be specified in depends; they will be


executed in the order listed

<target name=“jar” depends=“prepare,compile”>


</target>

8 © 2012 WIPRO LTD | WWW.WIPRO.COM


Structure of the Build file - <target>

• The build file can contain many number of targets


• Each target consists of a series of tasks that are specific actions for
ant to take
• A target can have the following attributes
– name (required)
– depends (comma separated list of other targets)
– if
– unless
– description

9 © 2012 WIPRO LTD | WWW.WIPRO.COM


An Example of if and unless attribute
If the
attribute is
declared
the target
gets
executed

The target gets executed if the property is not declared

10 © 2012 WIPRO LTD | WWW.WIPRO.COM


Assignment

1. Understand why the name attribute is mandatory in any target


2. Create few more targets called three, four and five. Let target one
be dependent on two, let two be dependent on three, three be
dependent on four, four be dependent on five
3. Issue ant one, and note your observations
4. Issue ant three and note down your observations
5. Declare a property called ‘name’ as shown in the previous slide
and try if and unless attribute

11 © 2012 WIPRO LTD | WWW.WIPRO.COM


Some important targets

12 © 2012 WIPRO LTD | WWW.WIPRO.COM


Target - init
• The first target you will probably specify is “init”
– Used to set up properties that you will use throughout your build
file
– Properties can be set directly in the build file or by specifying all
the properties in a property file
– The advantage of setting variable here is, it gives you one place to
make changes that ripple through the entire file
– Using a properties file is even better because the syntax is simpler

<target name=“init” >


<property file=“build.properties” />
</target>

13 © 2012 WIPRO LTD | WWW.WIPRO.COM


Target - prepare
• The “prepare” target depends on “init”
• This target is used to create the directory structure that is needed

<target name=“prepare” depends=“init”>


<mkdir dir=“${build.dir}”/>
<mkdir dir=“${build.dir}\META-INF”/>
<target>

• Notice the ${build.dir} syntax; this is dereferencing a property that


was set in the “init” target

14 © 2012 WIPRO LTD | WWW.WIPRO.COM


Tatget - clean
• “clean” is useful for enabling clean builds
• Generally clean just deletes stuff from previous runs to ensure that no
artifacts persist between builds

<target name=“clean” depends=“init”>


<delete dir=“${build.dir}”/>
</target>

• For clean build, you should execute the “clean” target before trying to
build anything

15 © 2012 WIPRO LTD | WWW.WIPRO.COM


Target - compile
• Compile some/ all of your source files in this target

<target name=“compile” depends=“prepare”>


<echo message=“Compiling now…”/>
<javac srcdir=“${src.dir}”
destdir=“${build.dir}” includes=“**/*.java” />
</target>

• “**/*.java” notation means “all files recursively” from ‘srcdir’ that ends
with .java

16 © 2012 WIPRO LTD | WWW.WIPRO.COM


Target - jar
• Once you’ve compiled everything, we should create the jar file

<target name=“jar” depends=“compile”>


<jar destfile=“${dist.dir}/${name}.jar”
basedir=“${build.dir}”/>
</target>

• This builds a jar file


– The name of the jar file is as pointed by the property ${name}
– The Jar file will be created In the directory as pointed by ${dist.dir}
– It includes everything from the ${build.dir} directory

17 © 2012 WIPRO LTD | WWW.WIPRO.COM


Built in tasks

18 © 2012 WIPRO LTD | WWW.WIPRO.COM


Built-In Tasks
• We will now look at some of the many built-in tasks that
ship with Ant
• We will show some of the most common options; the
others are left as exercise…
• Ant comes with extensive documentation on these tasks
showing all possible options
• We will begin with “property”, “javac” and “jar” and go into
the others in alphabetical order

19 © 2012 WIPRO LTD | WWW.WIPRO.COM


Working with Properties
• Hard-coding directory paths and filenames is not a good idea in any area of
programming
• Properties comes handy in this case
• The ${} notation is used by Ant to retrieve the value of the Property

Displaying default properties of Ant Output


<project name="Ant Properties Project"
basedir="." default="builtin">
<target name="builtin">
<echo message="The base directory:
${basedir}"/>
<echo message="This file: ${ant.file}"/>
<echo message="Ant version: ${ant.version}"/>
<echo message="Project name:
${ant.project.name}"/>
<echo message="Java version:
${ant.java.version}"/>
</target>
</project>

20 © 2012 WIPRO LTD | WWW.WIPRO.COM


Setting properties in a build.xml
• The <property> element is defined as a task, unlike the <project>
and <target> elements
• The<property> elements can be included inside a target depending
on which target has been selected
• Properties can also be set at the beginning of a build file so that they
apply to the entire build

<target name="properties.custom">
<property name="version" value="1.1"/>
<echo message="Version. = ${version}"/>
</target>

21 © 2012 WIPRO LTD | WWW.WIPRO.COM


Properties file
• All the property values used in a build.xml can be grouped in a
separate ‘properties’ file
• The property values are immutable
• If you set a property value in the properties file you cannot change it
in the build file

Sample.properties
web.lib.dir=${web.dir}/WEB-INF/lib
build.classes.dir=build/classes

Using that in build.xml


<property file=“Sample.properties" />

22 © 2012 WIPRO LTD | WWW.WIPRO.COM


Assignment

1. Write a build.xml to print some of the built-in ANT properties


2. Name the file as built-in.xml and execute it
3. Create an xml file with user defined properties and display all of
them
4. Store the properties in a file called build.properties and print them
using the xml file
5. Create the targets called init (which is used to initialize properties),
prepare (which is used to create directories) and clean (which is
used to delete the build directory) and check how it works

23 © 2012 WIPRO LTD | WWW.WIPRO.COM


<javac>
• The javac task compiles java source into class files, just as the javac
command-line tool does
• If you store your source code in directory structures that match the
package structure of your application, then Ant will recompile only
those files that have changed (very useful)
• It’s possible to use different compilers
• For eg:
– classic - the standard compiler of JDK 1.1/1.2
– modern - the standard compiler of JDK 1.3/1.4/1.5/1.6/1.7/1.8)
– gcj - the gcj compiler from gcc
• This can be specified by setting the global build.compiler property

24 © 2012 WIPRO LTD | WWW.WIPRO.COM


<javac> (Contd.).

<javac srcdir=“${src.dir}” destdir=“${build.dir}”


Optimize=“off” debug=“on” deprecation=“on”
Classpath=“${classpath}” includes=“**/*.java”
excludes=“**/*Test*” />

• This task will


– Compile all the java source files, except those files which have “Test” in
their name names in the ${src.dir} directory
– Place the compiled classes in the ${build.dir}
– Include debug information in the .class files
– No need to perform any optimization
– Should show deprecation warnings

25 © 2012 WIPRO LTD | WWW.WIPRO.COM


<javac> (Contd.).

• The “srcdir” attribute is optional if there are nested “src” tags

<javac ..>
<src path=“${src.dir}”/>
<src path=“${other.src.dir}”/>
<exclude name=“com/xyz/**/*.java” />
</javac>

• The task above will


– Compile source files in the directory specified by both the ${src.dir} and
${other.src.dir} properties and their children
– It exclude files in the com.xyz package and any sub package

26 © 2012 WIPRO LTD | WWW.WIPRO.COM


<javac> (Contd.).
• If “destdir” is omitted, the compiled .class files will be placed in the
source directory itself
• “debug”,”optimize” and “deprecation” are all “off” by default

<javac srcdir=“${src.dir}”/>

27 © 2012 WIPRO LTD | WWW.WIPRO.COM


<jar>

• Creates a jar file

<jar destfile=“${dist.dir}/${name}.jar” basedir=“${build.dir}” />

• The basedir attribute is the reference directory from where to jar


• This includes everything under ${build.dir} in the jar file based on
the “name” property, located in the directory specified by the
“dist.dir” property
• Includes a MANIFEST.MF file

28 © 2012 WIPRO LTD | WWW.WIPRO.COM


<jar> (Contd.).

• Also supports includes and excludes


<jar basedir=“${build.dir}”
destfile=“${dist.dir}/${name}.jar”
includes=“**/*.class”
excludes=“**/*Test*.class” />

• Supports a nested <metainf> tag to specify what should be


included in the META-INF directory (you can specify multiple
<metainf> tags to pull files from different directories)

<jar …>
<metainf dir=“${etc.dir}” includes=“**/*.xml” />
</jar>A

29 © 2012 WIPRO LTD | WWW.WIPRO.COM


Assignment

1. In a folder called src, create some java files. Using a build.xml


compile all those .java files and keep the .class files in a folder
called build. Include the init, prepare, clean and compile targets
and test
2. To the above build.xml add the jar target and test

30 © 2012 WIPRO LTD | WWW.WIPRO.COM


<ant>

• It is used to call ant from within ant on a different buildfile


• Useful for building subprojects (e.g. partitioning large projects)

<ant dir=“./test” antfile=“build.xml” target=“compile”


ouput=“test.log” >
<property name=“param1” value=“1.0” />
</ant>

• If “antfile” is omitted, “build.xml” is assumed


• If “target” is omitted, the default target is used
• If “output” is omitted, output goes to the console

31 © 2012 WIPRO LTD | WWW.WIPRO.COM


Assignment

1. Create 2 xml files called build1.xml and build2.xml file. Create a


target called target1 in build1.xml and using <ant> call invoke target
called target2 which is there in build2.xml.
2. Declare a property called ‘course’ in build1.xml file and initialize it
with a value called ‘ant’. In build2.xml file also declare a property
called ‘course’ and initialize it with a value called ‘junit’.
In target2 of buld2.xml print the value of the property ‘course’ and
check what happens
3. Now remove the property called ‘course’ from build1.xml and run
build1.xml and check your result

32 © 2012 WIPRO LTD | WWW.WIPRO.COM


<antcall>

• Calls another target within the same buildfile


• Can pass parameters to it
• Useful for reusing targets

<target name=“foo”>
<echo message=“param1=“${param1}” />
</target>

<target name=“bar”>
<antcall target=“foo”>
<param name=“param1” value=“test”/>
</antcall>
</target>

33 © 2012 WIPRO LTD | WWW.WIPRO.COM


<antcall> example

34 © 2012 WIPRO LTD | WWW.WIPRO.COM


<available>
• Sets a property if a specified resource is available on the current
system
• The resource could be a directory, a file, a class in the classpath, or a
JVM system resource.
• Useful for targets that can’t/ shouldn’t be executed under certain
circumstances

<!– sets the “has.junit” peroperty to “true” if the


TestCase class is found in the classpath 
<available property=“has.junit”
classname=“junit.framework.TestCase” >
<classpath refid=“classpath” />
</available

35 © 2012 WIPRO LTD | WWW.WIPRO.COM


<available> (Contd.).
• Looks for the file called “foo.txt” and sets the “file.there” property if
found

<available property=“file.there” file=“./test.txt” />

• Looks for the “test.properties” resource in the classpath and sets the
property if found

<available properties=“test.there”
resource=“test.properties” >
<classpath refid=“classpath” />
</available>

36 © 2012 WIPRO LTD | WWW.WIPRO.COM


<condition>

• Sets a property if a given series of requirements are met


• Several nested conditions are available
– and, or, not, os, available, uptodate, equals,istrue, isfalse,…
• Sets the property javamail.complete if both the JavaBeans Activation
Framework and JavaMail are available in the classpath.

<condition property="javamail.complete">
<and>
<available
classname="javax.activation.DataHandler"/>
<available classname="javax.mail.Transport"/>
</and>
</condition>

37 © 2012 WIPRO LTD | WWW.WIPRO.COM


<copy>
• Copy a directory, excluding some files

<copy todir=“../test”>
<fileset dir =“${src.dir}” excludes=“**/*Test*.java”
/>
</copy>

• Copy a directory, renaming the files !

<copy todir=“../test” >


<fileset dir=“${src.dir}” />
<mapper type=“glob” from=“*” to=“*.bak” />
</copy>

38 © 2012 WIPRO LTD | WWW.WIPRO.COM


<echo>
• Prints a message to the console or a file
• Can expand properties just like other tasks
• If the “file” parameter is omitted, the console is used
• The “append” parameter works with the “file” parameter when an
output file exists
• Works like the “echo” command (UNIX/DOS)
• Send a static message to the console

<echo message=“now compiling..” />


<echo> This is another way to do it </echo>

• Append a dynamic message to a file

<echo message=“myvar=${myvar}” file=“log.txt”


append=“yes” />

39 © 2012 WIPRO LTD | WWW.WIPRO.COM


<exec>
• Executes a system command
• Command line arguments should be specified as nested <arg>
elements
• The <exec> task delegates to Runtime.exec
• The command shell executable ‘cmd’ should be executed using the /c switch

<target name="help"> %PATH% a.ba


<exec executable="cmd"> t
<arg value="/c"/>
<arg value="a.bat"/>
</exec>
</target>

Output

40 © 2012 WIPRO LTD | WWW.WIPRO.COM


<javadoc>
• Generates javaDoc from your source files
• Much easier than running javadoc at the command line
• You really need to structure your source according to your package
structure for best results
• Here’s the simple case, generating the docs for the packages listed,
in ${src.dir}, placing the generated files in ${doc.dir}

<javadoc sourcepath=“${src.dir}” destdir=“${doc.dir}”


packagenames=“foo.*,bar.*,baz.*” />

41 © 2012 WIPRO LTD | WWW.WIPRO.COM


<javadoc> (Contd.).
• Some of the useful parameters are
– sourcepath – where the sources to process live
– destdir – where to store the generated HTML files
– author –includes the @author information
– version – includes the @version information
– access –which level of access should be included (e.g publid,
private); protected is default
– packagenames – those packages to include in the doc
– excludepackagenames –packages not to include

42 © 2012 WIPRO LTD | WWW.WIPRO.COM


<mail>
• Used to send email to the email address specified
• Useful for overnight builds
• The log of what happened can be sent to you in the morning
• Also useful if a job break to notify someone immediately

<mail from=“anitha.ramesh@wipro.com”
tolist=“you@yourbox.com” subject=“Nighty build
results” files=“log.txt” />

It will go as an
attachment

43 © 2012 WIPRO LTD | WWW.WIPRO.COM


<mail> (Contd.).

• Could also send email based on a message string

<mail from=“anitha.ramesh@wipro.com”
tolist=“you@yourbox.com” subject=“Nighty build
results” message=“Finished building ${name}” />

• ‘tolist’ can be list of email addresses separated by commas

44 © 2012 WIPRO LTD | WWW.WIPRO.COM


<mkdir>
• Create a directory

<mkdir dir=“${build.dir}/temp” />

• Create an entire directory structure !

<mkdir dir=“${build.dir}/foo/bar/baz” />

45 © 2012 WIPRO LTD | WWW.WIPRO.COM


<move>

• Like copy, only it moves things


• Like the Unix “mv” program, can be used to rename things

<move file=“foo.java” tofile=“foo.save” />

• Or just move a file

<move todir=“${java.dir}”>
<fileset dir=“${src.dir} includes=“**/foo*.java” />
</move>

46 © 2012 WIPRO LTD | WWW.WIPRO.COM


<parallel>
• Runs two or more Ant tasks (not targets) simultaneously in separate
threads
• Useful if you have extra horsepower on a box or have several tasks
that are independent and it lasts for a while
• Caution must be exercised to not parallelize tasks that might crash
(eg. Two javac tasks)

<parallel>
<copy todir=“${remove.drive.one}” …/>
<copy todir=“${remove.drive.two}” …/>
</parallel>

47 © 2012 WIPRO LTD | WWW.WIPRO.COM


<tstamp>
• By default, sets the DSTAMP, TSTAMP and TODAY properties in
the build file
• These properties can be referenced later in the file
• Set DSTAMP, TSTAMP and TODAY

<tstamp/>

• Specifies a property name and a format

<tstamp>
<format property=“TODAY_US”
pattern=“ dd-MMMM-YYYY” />
</tstamp>

48 © 2012 WIPRO LTD | WWW.WIPRO.COM


<tstamp> (Contd.).

• Specify the jar file name using one of the default <tstamp> properties

<tstamp/>
<property name=“jarname”
value=“${name}-${DSTAMP}.jar” />

• Specify the jar file name using a custom property

<tstamp>
<format property=“TODAY_US” pattern=“d-MMMM-yyyy” />
</tstamp>
<property name=“jar.name” value=“${name}-
${TODAY_US}.jar” />

49 © 2012 WIPRO LTD | WWW.WIPRO.COM


Assignment - Using ANT with Eclipse

Step 1: Create an Eclipse Project


Step 2: Create a Java program
Step 3: Right click project and create build.xml file
Step 4: Execute the build.xml
a. Right click build.xml and select Run→ Ant Build or
Enable ANT view by following
Window > Show View > Other > Ant > Ant

Practice all the ANT targets and tasks

50 © 2012 WIPRO LTD | WWW.WIPRO.COM


Assignment - Using ANT with Eclipse

Step 1: Create an Eclipse Project


Step 2: Create a Java program
Step 3: Right click project and create build.xml file
Step 4: Execute the build.xml
a. Right click build.xml and select Run→ Ant Build or
Enable ANT view by following
Window > Show View > Other > Ant > Ant

Practice all the ANT targets and tasks given in this module

51 © 2012 WIPRO LTD | WWW.WIPRO.COM


Quiz

1. Which of the following is a mandatory parameter for javac task?


a. destdir
b. srcdir
c. debug
d. Classpath

2. What will happen if we execute a build.xml file with the following


<project name="MyProject">
...
</project> tag as E:\>ant build.xml
a. It'll look for build.xml in E:\ and will execute it if found
b. If build.xml is not found in E:\ it'll throw an error message
c. Both a and b are correct

52 © 2012 WIPRO LTD | WWW.WIPRO.COM


Quiz
3. What will happen when we issue the following command?
E:\>ant
a. It'll look for build.xml in E:\ and will execute it if found
b. If build.xml is not found in E:\ it'll throw an error message
c. Both a and b are correct

4. What does the following indicate?


E:\> ant -f mybuild.xml compile

a. The buildfile to be used is mybuild.xml


b. The target to be executed in mybuild.xml file is ‘compile’ target
c. Both a and b are correct

53 © 2012 WIPRO LTD | WWW.WIPRO.COM


Quiz
5. What happens with the following execution
E:\> ant mybuild.xml
a. It'll execute the mybuild.xml file if it is found in E drive
b. The build will fail

6. How do we make the earlier command work?


a. By changing the file name to build.xml and executing it as ant
build.xml
b. By using a –f option and executing it as ant –f mybuild.xml file
c. Both a and b are correct

54 © 2012 WIPRO LTD | WWW.WIPRO.COM


Quiz
7. Which of the following are build tools?
a. Ant
b. Makefile
c. Both a and b are correct

8. Which of the following are true regarding Ant?


a. Ant is platform dependent
b. Ant is not a programming language
c. Ant can compile source code
d. Ant can run unit tests
e. Ant can package compiled code and resources

9. Properties in Ant should be defined for


a. Any piece of information that needs to be configured
b. Any piece of information that might change
c. Both a and b are correct

55 © 2012 WIPRO LTD | WWW.WIPRO.COM


Quiz

Match the following:

1. Author of ANT a. points to ANT installation directory


2. Build file of ANT b. ANT built-in property
3. ANT_HOME c. build file in Unix environment
4. ant.version d. Duncan Davidson
5. Makefile e. build.xml

56 © 2012 WIPRO LTD | WWW.WIPRO.COM


References
1. Apache Ant 1.8.2 Manual(2012). Overview of Apache Ant Tasks.
Retrieved on April 10, 2012, from,
http://ant.apache.org/manual/tasksoverview.html

57 © 2012 WIPRO LTD | WWW.WIPRO.COM


Hands-On Activities – Milestone 4

Activity 1:
Design an algorithm to accept a string from the user and print all duplicate character (which
occur more than once) and their count. Characters which appear only once in the string must
not be displayed. Also if there are no duplicate characters in the given string a message “No
duplicate characters” must be printed.

For example if given String is "Programming" then your output should print
g: 2

r: 2

m: 2

Activity 2:
The product of two numbers is 120 and the sum of their squares is 289. Design an algorithm to
find the sum of the two numbers.

Activity 3: Magic Squares Puzzle


Design an algorithm which finds the magic square for a given odd number and displays it.
A magic square is an arrangement of numbers from 1 to n^2 in an [n x n] matrix, with each
number occurring exactly once, and such that the sum of the entries of any row, any column, or
any main diagonal is the same.

For Example –

If the value of n is 5

The output must be as below:

17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
If the value on n is: 6

The output must be as below:

Magic square works for an odd numbered size

Activity 4:
Design an algorithm to accept a string from the user. The algorithm must then check if the
string has 2 characters which are consecutive and are vowels. If so the message “consecutive
vowels present” must be printed, else the message “no consecutive vowels” must be printed.

For Example –

If the input string is “team”

Output must be “consecutive vowels present” as characters ‘e’ and ‘a’ are consecutive in the
string and are vowels.

If the input string is “the”

Output must be “no consecutive vowels” as there are no consecutive characters which are
vowels in the given string.

Activity 5:
Design an algorithm which accepts 2 numbers in binary format as input and prints their binary
sum.

For Example –
Enter binary number1: 0110
Enter binary number2: 1011

The output must be 10001 (which is the sum of 0110 & 1011)

Activity 6:
Design an algorithm which accepts a string as input. The entered string must be alpha numeric.
The algorithm must search for the numbers present in the entered string and display squares of
those numbers as the output.
For Example –

If the input string is:


Abc23xyz45

The output must be:


4
9
16
25

Note:
If the input string does not have numbers, an error message “string entered is not alpha
numeric” must be displayed.

Activity 7:
Design an algorithm to accept 15 integer elements for an array. The algorithm must then
identify odd and even numbers from the array and display them separately.

Activity 8:
Design an algorithm which accepts values of radius R and height H of a cylinder. The algorithm
must then calculate the volume and surface area of the cylinder.

Note:
The values entered for R and H are positive and non-zero. Also both radius and height belong to
same unit (may be cm or meters)

Formulas –
Surface area = 2 * Pi * r * (r + h)
Volume = Pi * r * r * h

Value of Pi is 22/7

Activity 9:
Design an algorithm to find the sum of the series given below.
1+1/2+1/3+1/4……..+1/N
The value of N is given as input by the user.
N must be a positive non-zero integer value.

For Example –

If the value of N is 4

The output must be 2.08


(Calculation: 1+1/2+1/3+1/4)

Activity 10:
Design an algorithm which calculates the simple interest given the principal amount, rate of
interest and time. The values for principal amount (p), rate of interest (r) and time (t) must be
taken as inputs from the user. The simple interest calculated must be displayed as output.

Formula-
Simple Interest = (p * t * r) / 100

Note:
The values entered for p, t and r are positive and non-zero.

Activity 11: Happy Numbers


Let the sum of the square of the digits of a positive integer S 0 be represented by S1. In a similar
way, let the sum of the squares of the digits of S1 be represented by S2 and so on. If Si = 1 for
some i >= 1, then the original integer S0 is said to be Happy number. A number, in which Si does
not come to 1, is not happy and called ‘Unhappy number’.

For Example –

Consider a number 7.

72 = 49 -> 42 + 92 = 97 -> 92 + 72 = 130 -> 12 + 32 = 10 -> 12 = 1

Therefore 7 is a “Happy number”

Consider another number 4.

4 is an “Unhappy number” since:


42 = 16 -> 12 + 62 = 37 -> 32 + 72 = 58 -> 52 + 82 = 89 -> 82 + 92 = 145 -> 12 + 42 + 52 = 42 -> 42 + 22 =
20 -> 22 + 02 = 4

Design an algorithm which accepts a number (which is non-zero and positive) from the user and
then checks if it is a happy number or not.
If the number entered by the user is a happy number then the message “Happy number” must
be printed otherwise the message “Unhappy number” must be printed.

Activity 12: Find Good Numbers


A number is termed as a “GOOD NUMBER” if meets the 2 criteria’s mentioned below;

a. All the digits in the number must be only 1 or 2 or 3


b. No two adjacent substrings of the number is same

Example for good numbers: 1, 121, 1213, 1213121

Example for bad numbers: 11, 1212, 1221, 1231231

 11 is a bad number because 1 follows 1


 Similarly 1212 is bad because the substring “12” is present adjacent to the substring
“12”
 1221 is a bad number because the substring “2” follows “2”
 1231231 is a bad number because the substring “123” is adjacent to substring “123”

Design an algorithm to accept an input value N (which is a positive non-zero number). The
algorithm must then find out the smallest good number which can be formed and has only N
digits in it. The number must be displayed as output.

For Example –

If the value on N entered by the user is 4

The smallest possible good number which can be formed having 4 digits is 1213

Activity 13:
Design an algorithm to accept 2 numbers as input from the user. The algorithm must compute
the Lowest Common Multiple (LCM) for the 2 numbers and print the number as output.
For Example –

If the numbers entered by the user are: 456 and 12


The output must be 456 which is the LCM of 456 and 12

Activity 14:
Design an algorithm to accept 2 numbers as input from the user. The algorithm must compute
the Greatest Common Divisor (GCD) for the 2 numbers and print the number as output.

For Example –

If the numbers entered by the user are: 100 and 50

The output must be 10 which is the GCD of 100 and 50

Activity 15:
Design an algorithm to accept 2 numbers as input from the user. The algorithm must compute
the Highest Common Factor (HCF) for the 2 numbers and print the number as output.

For Example –

If the numbers entered by the user are: 24 and 36

The output must be 12 which is the HCF of 24 and 36

You might also like