QA's approach 2 Java - Build Tools - ANT

Build Tools


ANT abbreviates as Another Neat Tool

Basically its a build management tool, which is used for Compiling the Code, Creating the Project Tree structure, etc based on the targets defined in Build.xml file

Will only allow us to execute the framework from command prompt

Ant uses an xml file for its configuration. The default file name is build.xml.

Ant builds are based on three blocks: tasks, targets and extension points.
How to declare variables in Build.xml file
===============================

property element

Ant uses the property element which allows us to specify properties.

This allows the properties to be changed from one build to another, or from one environment to another.


<?xml version="1.0"?>

<project name="Test Project" default="notify">

<property name="Blogname" value="novice-2-professional.blogspot.com"/>

<target name="notify">

<echo>Version of Ant used is ${ant.version} - at ${Blogname} </echo>

</target>

</project>


But for large project, it makes sense to store the properties in a separate property file.

<?xml version="1.0"?>

<project name="Test Project" default="notify">

<property file="build.properties"/>

<target name="notify">

<echo>Version of Ant used is ${ant.version} - at ${Blogname} </echo>

</target>
</project>


DataTypes in ANT

Ant provides a number of predefined data types->set of services that are built into the product

FileSet DataType:

Represents a collection of files.

It is used as a filter to include or exclude files that match a particular pattern.


<fileset dir="${src}" casesensitive="yes">

<include name="**/*.java"/>

<exclude name="**/*Stub*"/>

</fileset>


Path

The path data type is commonly used to represent a classpath.

<path id="build.classpath.jar">

<pathelement path="${env.J2EE_HOME}/${j2ee.jar}"/>

<fileset dir="lib">

<include name="**/*.jar"/>

</fileset>

</path>


Example Build.xml file:

<?xml version="1.0"?>
<project name="Ant-Test" default="main" basedir=".">

    <!-- Sets variables which can later be used. -->
    <!-- The value of a property is accessed via ${} -->

    <property name="src.dir" location="src" />
    <property name="build.dir" location="bin" />
    <property name="dist.dir" location="dist" />
    <property name="docs.dir" location="docs" />

    <!-- Deletes the existing build, docs and dist directory-->

    <target name="clean">
        <delete dir="${build.dir}" />
        <delete dir="${dist.dir}" />
    </target>

    <!-- Creates the  build, docs and dist directory-->

    <target name="makedir">
        <mkdir dir="${build.dir}" />
        <mkdir dir="${dist.dir}" />
    </target>

    <!-- Compiles the java code  -->

    <target name="compile" depends="clean, makedir">
        <javac srcdir="${src.dir}" destdir="${build.dir}">
        </javac>
    </target>

</project>

How to generate Build.xml file using eclipse

 Open eclipse -> File->Export->


Click Next ->Then select the Project folder based on which to create Build.xml file, click on Finish.
Under the selected Project folder ->Build.xml file will be created.

Integrating Ant with TestNG:

========================================================================

<project basedir="." default="runTest" name="Ant file for TestNG">

<!--project home path & default target is specified in the above project tag>  

<property name="src" location="src" />
<property name="bin" location="bin" />
<property name="libs" location="lib" />

<!-- source, binaries, library files directory specified using property tag>  


<path id="class.path">
    <pathelement location="${libs}/testng-6.8.5.jar" />
    <pathelement location="${libs}/selenium-java-2.53.0.jar" />
    <pathelement location="${libs}/selenium-server-standalone-2.33.0.jar" />
    <pathelement location="${libs}/jxl-2.6.12.jar" />
<pathelement location="${libs}/jt400.jar" />
<pathelement location="${libs}/jta.jar" />
    <pathelement location="${bin}" />
</path>

<!--setting the class path for the jar files using path tag>


  <taskdef name="testng" classname="org.testng.TestNGAntTask">
    <classpath>
      <pathelement location="lib/testng-6.8.5.jar"/>
    </classpath>
  </taskdef>

   <target name="runTest">

    <mkdir dir="testng_output"/><!-- Create the output directory. -->

    <testng outputdir="testng_output" classpathref="class.path">

<!-- execute on OrgFirefox.xml file, i.e TestNg.xml file to execute tests on Firefox browser> 
    <xmlfileset dir="./testNGConfig/" includes="OrgFirefox.xml"/>        
   
    </testng>

  </target>
</project>

========================================================================

Comments

Popular posts from this blog

QA's approach 2 Java - Understanding Static context

QA's approach 2 Java - Collections Framework & Generics Introduction

Technologies - Log4J - Create Log4j Configuration File - Where ? How ? What ?