In the previous post, we have learned what is Apache Ant, its features and an example to create simple build file. This post will guide you other basic ant tasks that are frequently used to create and deploy packages.

Property

 Ant allows us to declare properties and assign values that can be used as variables

<property name="property_name" value="property_value"/>

And that can be used using

${property_name}

Ant predefined properties

Property

Description

ant.version

Version of apache ant

ant.java.version

Java version that apache ant is using

ant.home

Home directory where apache is installed

ant.library.dir

Path of ant library files

ant.file

Location of build file

basedir

Base directory of build

Echoing message on screen

<echo message="Version of Apache Ant ${ant.project.name}: ${ant.file}"/>

Define classpath

<path id="classpath">
    <fileset dir="directory_path" includes="*.jar" />
    <fileset dir="directory_path" includes="*.jar" />
</path>

Compiling Java code

<javac srcdir="source_path_of_java_files" classpathref="classpath" destdir="destination_of_compiled_files" />

Creating Jar 

<jar jarfile="jar_name.jar" basedir="compiled_files_path" includes="**/*.class" update="true">

Create directory

<mkdir dir="directory_name" />

Delete directory

<delete dir="directory_name" />

Copy directory

<copy todir="directory_path_where_to_copy" overwrite="true">
    <fileset dir="directory_path_to_copy_files">
         <include name="**/*.*" />
     </fileset>
</copy>

Copy files

<copy file="file_to_copy" todir="directory_path_where_to_copy" overwrite="true" />

Delete files

 <delete>
     <fileset dir="directory_path_to_copy_files">
         <include name="file_name_or_wild_card"/>
         <include name="file_name_or_wild_card"/>
    </fileset>
 </delete>

File loading 

<loadfile property="defined_property_name" srcFile="file_to_load_as_string" />

Replace string in file

<replace file="file_path" token="string_to_replace" value="string_value_to_replace_with"></replace>

Set property based on condition

Based on condition property value is set to true or false.

<condition property="property_name_to_set">
     <matches string="string_data" pattern="pattern_to_match_in_string" />
 </condition>

Above code will set property if particular pattern is matched in specified string. Pattern can hold regex expression. You can specified string inline or property that holds the string file data. Detailed explanation is available on official site.

If-else

<if>
   <equals arg1="value" arg2="value"/>
   <then><echo message="if block executed"/></then>
   <else>
         <echo message="else block executed"/>
   </else>
</if>

Like if-else in any other language, based on condition task is performed. Detailed explanation is available on official site.

Call target inside a target

<antcall target="valid_target_name"/>

Add external tasks

<taskdef name="xmltask" classname="com.oopsconsultancy.xmltask.ant.XmlTask" classpath="./lib/xmltask.jar" />

Xmltask : Insert data using XPath

<xmltask source="source_file_to_edit" dest="destination_to_save_file">
     <insert path="//*[local-name()='beans'][1]/*[local-name()='bean'][last()]" position="after">
           <![CDATA[
              <import xmlns="http://www.springframework.org/schema/beans" resource="MyData.xml"/>
            ]]>
     </insert>
 </xmltask>
 
 Above command will insert xml-import after the path defined using XPath.

Xmltask : Insert data based on condition using XPath

<xmltask source="source_file_to_edit" dest="destination_to_save_file">
     <copy path="//*[local-name()='beans'][1]/*[local-name()='bean'][@resource='MyData.xml']/@resource" property="ITEM_EXISTS_ALREADY" />
     <insert path="//*[local-name()='beans'][1]/*[local-name()='bean'][last()]" position="after"> position="after" unless="ITEM_EXISTS_ALREADY">
         <![CDATA[
            <import xmlns="http://www.springframework.org/schema/beans" resource="MyData.xml"/>
         ]]>
      </insert>
</xmltask>

The above code will set property ITEM_EXISTS_ALREADY if the attribute resource with value MyData.xml  exists under given XPath. Then insertion will be performed based on the ITEM_EXISTS_PROPERTY.

Xmltask : Remove xml content

<xmltask source="source_file_to_edit" dest="destination_to_save_file">
    <remove path="/beans/bean/import[@resource='MyData.xml']"/>
 </xmltask> 

The above code will remove the import with resource value MyData.xml.

Reference

https://ant.apache.org/

https://www.tutorialspoint.com/ant/index.htm

Leave Comment

Your email address will not be published. Required fields are marked *

en_USEnglish
en_USEnglish