Categories
Ant Build Script

Documentation

1 . ant -help

This command executed in command prompt gives helpful information about what are all parameters present to execute the ant command.

2. Ant official Documentation : https://ant.apache.org/manual/index.html

Above link provides detailed developers documentation about all capabilities of ant build. It can give detailed documentation about syntax, procedures, what are all in built tasks available, how to create customized tasks, how to configure Ant with various development IDEs etc.

Categories
Ant Build Script Uncategorized

Setting Properties

In previous tutorial, we have set several paths hardcoded in buildConfig configuration file. However Ant provides capability to either declare those values as properties in configuration file, load values from resource file or pass resource key-value pairs from command line. We will use same buildConfig file to modify it.

1.Declare Values as Properties in configuration file:

We have cleanLogs target in buildConfig file, which is receiving directory path to delete files within. Lets declare that path as property value in beginning of path, like below

<property name="log.dir" value="d:/logs"/>

and target definition would become

    <target name="deleteLogs" depends="stopOracle" 
		description="Clean Logs directory">
      <delete>
         <fileset dir="${log.dir}">
            <include name="*"/>
         </fileset>
      </delete>
	</target>
  
     
     

Lets run the ant target

2. Read values from build.properties

Lets prepare a file named build.properties, which would host key value pairs. We have compileSource target, which receives several paths as hardcoded

<target name="compileSource">
	<javac
    srcdir="D:/Learning/11JAVA XML/1. javax.xml.bind/workspace/01HelloWorld/src"
    destdir="D:/Learning/11JAVA XML/build/classes"/>
	
	<jar destfile="D:/deployModule/lib/util.jar"
			basedir="D:/Learning/11JAVA XML/build/classes"
			includes="*"
	>
	</jar>
	</target>

Lets move all paths in build.properties in form of key-value pairs.

src.dir=D:/Learning/11JAVA XML/1. javax.xml.bind/workspace/01HelloWorld/src
build.class=D:/Learning/11JAVA XML/build/classes
jar.file=D:/deployModule/lib/util.jar

build.properties has to be declared at beginning as property file like below

<property file="build.properties"/>

Target definition “compileSource” would become like below

<target name="compileSource">
 <javac
    srcdir="${src.dir}"
    destdir="${build.class}"/>
	
 <jar destfile="${jar.file}"
			basedir="${build.class}"
			includes="*"
	>
	</jar>
</target>

3.Property passed from command line :

We have target definition to start the oracle database service “startOracle”, wherein database instance service name is hardcoded.

  <target name="startOracle">
		<exec executable="cmd">
			<arg value="/c"/>
			<arg value="net start OracleServiceORCLDB"/>
		</exec>
   </target>

Here we can configure such that we receive service name from command line. Hence target definition would become like this

<target name="startOracle">
		<exec executable="cmd">
			<arg value="/c"/>
			<arg value="net start ${service.name}"/>
		</exec>
    </target>

Now we can pass service.name value as command line parameter like below

cmd>ant -buildfile buildConfig.xml -Dservice.name=OracleServiceORCLDB startOracle

Hence we will receive output as

Now we have completely modified file buildConfig.xml as below

<?xml version="1.0"?>
   <project name="AntDemo" default="hello">
   
   <property name="log.dir" value="d:/logs"/>
   <property file="build.properties"/>
   
   <target name="hello">
      <echo>Hello World - Welcome to Apache Ant!</echo>
   </target>
   
  <target name="stopOracle">
		<exec executable="cmd">
			<arg value="/c"/>
			<arg value="net stop ${service.name}"/>
		</exec>
   </target>
   
	<target name="startOracle">
		<exec executable="cmd">
			<arg value="/c"/>
			<arg value="net start ${service.name}"/>
		</exec>
    </target>
   
   
    <target name="deleteLogs" depends="stopOracle" 
		description="Clean Logs directory">
      <delete>
         <fileset dir="${log.dir}">
            <include name="*"/>
         </fileset>
      </delete>
	</target>

	<target name="compileSource">
	<javac
    srcdir="${src.dir}"
    destdir="${build.class}"/>
	
	<jar destfile="${jar.file}"
			basedir="${build.class}"
			includes="*"
	>
	</jar>
	</target>
</project>

we have additional build.properties file below

src.dir=D:/Learning/11JAVA XML/1. javax.xml.bind/workspace/01HelloWorld/src
build.class=D:/Learning/11JAVA XML/build/classes
jar.file=D:/deployModule/lib/util.jar

Categories
Ant Build Script Uncategorized

Ant Target

Target can be seen as module of tasks in ant configuration file. We can request ant to execute specific target, by passing command line input. Each target may have one or more tasks. Following is sample buildConfig.xml

<?xml version="1.0"?>
   <project name="AntDemo" default="hello">
   <target name="hello">
      <echo>Hello World - Welcome to Apache Ant!</echo>
   </target>
   
  <target name="stopOracle">
		<exec executable="cmd">
			<arg value="/c"/>
			<arg value="net stop OracleServiceORCLDB"/>
		</exec>
   </target>
   
	<target name="startOracle">
		<exec executable="cmd">
			<arg value="/c"/>
			<arg value="net start OracleServiceORCLDB"/>
		</exec>
    </target>
   
   
    <target name="deleteLogs" depends="stopOracle" 
		description="Clean output directories">
      <delete>
         <fileset dir="d:/logs">
            <include name="**/*.class"/>
         </fileset>
      </delete>
	</target>

	<target name="compileSource">
	<javac
    srcdir="D:/Learning/11JAVA XML/1. javax.xml.bind/workspace/01HelloWorld/src"
    destdir="D:/Learning/11JAVA XML/build/classes"/>
	
	<jar destfile="D:/deployModule/lib/util.jar"
			basedir="D:/Learning/11JAVA XML/build/classes"
			includes="*"
	>
	</jar>
	</target>
</project>

Exec as Target : please refer target named “stopOracle” having exec as task tag inside. It contains executable command to stop oracle service. if we pass this as target it will stop oracle databse service named “OracleServiceORCLDB”. Similarly we have “startOracle” to start the oracle database instance service.

Dependent Target : Target “deleteLogs” is having dependency on “stopOracle” target. Hence if we execute “deleteLogs” target, it will first invoke “stopOracle”, then its own tasks are executed.

Multiple Tasks : If we want two or more tasks to be executed together as single module, multiple tasks can be mentioned in single target. Target “compileSource” contains the two tasks compile .java files and also create the jar of compiled classes.

Categories
Ant Build Script Uncategorized

Execute Ant

It is xml based configuration file.

Following is HelloWorld configuration file “buildConfig.xml”. File can have any name. Open command prompt and copy file in current directory.

<?xml version="1.0"?>
   <project name="AntDemo" default="hello">
   <target name="hello">
      <echo>Hello World - Welcome to Apache Ant!</echo>
   </target>
</project>

Execute following Ant command

>ant -buildfile buildConfig.xml hello

As seen in command, we are invoking ant.bat having two command line parameters -buildFile and target to execute in the build file.

In the buildConfig.xml target “hello” is mentioned and invoked. Target “hello” consists of single in-built task echo to print the input message in config file.

It prints output on console by default. Each target is executed & printed on console. Here “hello:” target is executed & printed on console its output that is echo message. In the end it says “BUILD SUCCESSFUL” with return code 0.

Ant has several optional input parameters, that can be seen using following command

cmd>ant -help

Categories
Ant Build Script Uncategorized

Install Ant

  • Download Ant distribution from https://ant.apache.org/bindownload.cgi
  • Check that compatible java version is already installed on local machine & environment variable JAVA_HOME is already set. Following commands can help to check that.
  • Unzip the Ant downloaded distribution package
  • Set environment variable ANT_HOME pointing to base directory of unzipped ant distribution.
  • Add new entry in PATH environment variable, to add path of ant bin directory.
  • To check if ant is successfully installed, open command prompt and type command “ant -version”, it should show installed version of ant.
Categories
Ant Build Script

What is Ant

Ant is open source build framework provided by Apache Foundation, based on Java. At can help us to automate day to day tasks mainly for software build procedure. It can also serve as developer tool to automate day to day mundane tasks like start-stop services, delete logs, update database, codebase etc.

Ant takes XML configuration file as input build File. Build file contains the tags based instructions on what & how to perform certain tasks.

Build file may mention more than one target definitions. Each target may have one or more tasks to perform. Target can be passed as another input to Ant script, to instruct which target to perform. Target may have dependency on some other targets. In such case, dependency targets are executed first, followed by tasks in current target.

Each target may have one or more tasks to perform like copy files, delete files, compile, execute etc. Tasks are in built tag definitions of Ant. Ant provides rich set of inbuilt tasks viz copy, delete, echo, exec etc. Those in built tasks can be used passing attributes as their inputs. Also customized ant tasks can also be developed.

XML based build file may be viewed as static configuration. For dynamic configurations, input values can be passed through command line arguments or resource files.

Design a site like this with WordPress.com
Get started