SCM/Build & Release Management: Chiranjeevi Patel
SCM/Build & Release Management: Chiranjeevi Patel
SCM/Build & Release Management: Chiranjeevi Patel
Release
Management
Chiranjeevi Patel
SCM/Build & Release Management 2012
Contents
Build Automation
Build automation is the act of scripting or automating a wide variety of tasks that software
developers do in their day-to-day activities including things like:
We have different types of scripting available in the market to make the software builds as an
automated task.
Ant scripting:
Ant – originally an acronym for “Another Neat Tool”
Installation:
Download the ant from below URL and unzip it.
http://apache.techartifact.com/mirror//ant/binaries/apache-ant-1.8.4-bin.zip
After downloading add the Ant bin directory to your path (for Windows)
Running Ant
Ant by default always looks for a file named build.xml. If a build file other than build.xml needs to be
used then ant should be run with options
ant –f anotherbuild.xml
<project>
<target>
task 1 ….
task 2 ….
</target>
</project>
Attributes:
basedir – The base directory for all file and path references. The default value is the current
directory.
A target can depend on other targets. You might have a target for compiling, for example, and a target
for creating a distributable. You can only build a distributable when you have compiled first, so the
distribute target depends on the compile target. Ant resolves these dependencies.
Attributes
E.g.
When you set dependencies like this, before the target “ProgA” is built, Ant will check for targets,
“LibB” and “LibC”. If they have not already been built using the latest versions of their source codes,
they will be built before “ProgA”.
Tasks – tag that represents the actions that has to be done during the build process
A task is a piece of code that can be executed. A task can have multiple attributes (or arguments, if you
prefer). The value of an attribute might contain references to a property. These references will be
resolved before the task is executed.
<?xml version="1.0"?>
<target name="prepare">
</target>
<delete dir="${build.dir}"/>
</target>
</target>
</target>
<target name = "all" depends = "clean,jar" description"Clean,compiles, then bulds the JAR file"/>
</project>
Echo task: Echoes a message to the current loggers and listeners which means System.out unless
overridden. A level can be specified, which controls at what logging level the message is filtered at.
<echo message="Hello, world"/>
Mkdir task: Creates a directory. Also non-existent parent directories are created, when necessary.
Does nothing if the directory already exist
<mkdir dir="build"/>
Copy task: Copies a file or resource collection to a new file or directory. By default, files are only copied
if the source file is newer than the destination file, or when the destination file does not exist. However,
you can explicitly overwrite files with the overwrite attribute
<copy todir="../new/dir">
<fileset dir="src_dir"/>
</copy>
Move task: Moves a file to a new file or directory, or collections of files to a new directory. By default,
the destination file is overwritten if it already exists. When overwrite is turned off, then files are only
moved if the source file is newer than the destination file, or when the destination file does not exist
<move todir="new/dir/to/move/to">
Javac task: Compiles a Java source tree. The source and destination directory will be recursively
scanned for Java source files to compile. Only Java files that have no corresponding .class file or where
the class file is older than the .java file will be compiled
<javac srcdir="src"
destdir="build/classes"
/>
http://ant.apache.org/manual/tutorial-HelloWorldWithAnt.html
How do we interpret the manual build steps into Ant build script
java-only Ant
md build\classes <mkdir dir="build/classes"/>
javac <javac
-sourcepath src srcdir="src"
-d build\classes destdir="build/classes"/>
src\oata\HelloWorld.java <!-- automatically detected -->
echo Main-Class: <!-- obsolete; done via manifest tag -->
oata.HelloWorld>mf <mkdir dir="build/jar"/>
md build\jar <jar
jar cfm destfile="build/jar/HelloWorld.jar"
build\jar\HelloWorld.jar
mf basedir="build/classes">
-C build\classes <manifest>
. <attribute name="Main-Class"
value="oata.HelloWorld"/>
</manifest>
</jar>
java -jar <java jar="build/jar/HelloWorld.jar"
build\jar\HelloWorld.jar fork="true"/>
Properties:
These are like variables in programming languages.
Properties are immutable: whoever sets a property first freezes it for the rest of the build; they are most
definitely not variables.
Ex:
<property name="source_dir">src</property>
Properties can be placed in separate file and this file can be imported into the build script.
Ex:
<property file="build.properties"/>
There few built in properties available with ant, please find them below
Fileset Type
Fileset is a subtask used within the other tasks like copy,move ..etc
Attributes
This type is used to specify a list of files to be processed. This can be done using the nested tags given
below.
Attributes:
<includesfile> tag
Attributes:
name – name of a text file, whose each line represents a pattern of files to be included.
<exclude> tag
This is similar to include tag, but used to remove the files that have been included using the
<excludesfile> tag
This is similar to includesfile tag, but used to remove the files that have been included using the
include and includesfile tag.
Ex:
<fileset basedir="bin">
<include name="*.java"/>
<include name=“test.java"/>
</fileset>
Patterns:
Ex:.
build/** – all the files in build directory and all its sub directories
**/mydir/* - all the files in the directory named “mydir” in any location in the directory tree
Ex:
<zip destfile="${dist}/manual.zip"
basedir="htdocs/manual"
/>
PatternSets are used to select files to extract from the archive. If no patternset is used, all files are
extracted.
War
An extension of the Jar task with special treatment for files that should end up in the WEB-INF/lib, WEB-
INF/classes or WEB-INF directories of the Web Application Archive.
<fileset dir="WebContent"/>
<lib dir="WebContent/WEB-INF/lib"/>
</war>
AntCall :
Call another target within the same buildfile optionally specifying some properties (params in this
context). This task must not be used outside of a target.
Ex:
target name="default">
<antcall target="doSomethingElse">
<param name="param1" value="value"/>
</antcall>
</target>
<target name="doSomethingElse">
<echo message="param1=${param1}"/>
</target>
Ant inputs:
These are command line inputs passed to ant at run time.
${input} can be accessible from the build script and it contains the value as “value”
Ex: <path>
<pathelement location=“${libdir}/servlet.jar”/>
<pathelement path=“${builddir}”/>
</path>
<classpath>
Author: Chiranjeevi P @ Chiranjeevi.aec@gmail.com 10
SCM/Build & Release Management 2012
<pathelement path=“${builddir}”/>
</classpath>
Advanced tasks:
Exec task:
Executes a system command. When the os attribute is specified, then the command is only executed
when Apache Ant is run on one of the specified operating systems.
If :
Perform some tasks based on whether a given condition holds true or not.This task is heavily based on
the Condition framework that can be found in Ant 1.4 and later, therefore it cannot be used in
conjunction with versions of Ant prior to 1.4. Due to numeruos bugs in Ant 1.4(.1) that affect this task,
we recommend to use Ant 1.5 or later.
Download ant-contrib.jar to run the below task and define it in your build.xml
Ex:
<if>
<equals arg1="${foo}" arg2="bar" />
<then>
<echo message="The value of property foo is bar" />
</then>
<else>
<echo message="The value of property foo is not bar" />
</else>
</if>
scp : Copies a file or FileSet to or from a (remote) machine running an SSH daemon. FileSet only works
for copying files from the local machine to a remote machine.
External libraries:
Ant by default provides a list of tasks, apart from them if we need to use any external tasks then we
need to download the respective library and copy it to lib dir of ant installation.
Ant Options:
ant -help
Ant
Search for a file with the extension “.build”. If one is found, use it to build its default target.
ant –buildfile:project1.build
ant clean
ant -debug
Displays messages that Ant and task developers have flagged as debugging messages.
ant –version
Review yourself:
1) What is ant?
2) How do I get started to use ant? Can you give me a “Hello World” ant script?
11) How to use ant to run command line command? How to get perl script running result?