Categories: Ant

Using Property files in Ant and Property Task

Ant build file is a XML file so we can declare any variable as we could declare on our any programming language but we can use property element as declaring some value to parameter.

By default, Ant provides the following pre-defined properties that can be used in the build file:

  • ant.file-> The full location of the build file.
  • ant.version->  The version of the Apache Ant installation.
  • basedir->  The basedir of the build, as specified in the basedir attribute of the project element.
  • ant.java.version->  The version of the JDK that is used by Ant.
  • ant.project.name->  The name of the project, as specified in the name attribute of the project element.
  • ant.project.default-target->  The default target of the current project.
  • ant.project.invoked-targets->  Comma separated list of the targets that were invoked in the current project.
  • ant.core.lib->  The full location of the Ant jar file.
  • ant.home->  The home directory of Ant installation.
  • ant.library.dir->  The home directory for Ant library files – typically ANT_HOME/lib folder.

Example-

Declaring some property element in the xml and using it. 

Like we declaring name of tutor.

<?xml version="1.0"?>
   <project name="Hello World" default="info" basedir=".">
   <property name="tutor" value="Dinesh Rajput"/>
   <target name="info">
      <echo>Hello World - Welcome to Apache Ant Tutorial by Tutor ${tutor}!!!</echo>
   </target>
   
</project>

Running Ant on the above build file produces the following output:


Using External Property into ANT Build-

We can also use external property file and use it into ANT build file. It allows you to reuse the same build file, with different property settings for different execution environment. For example, build properties file can be maintained separately for DEV, TEST, and PROD environments.

Here we are creating the property file is named build.properties and is placed along-side the build.xml file. You could create multiple build properties files based on the deployment environments – such as build.properties.dev and build.properties.test.

The following example shows a build.xml file and its associated build.properties file:
build.xml

<?xml version="1.0"?>
   <project name="Hello World" default="info" basedir=".">
   <property file="build.properties"/>
   <target name="info">
      <echo>Hello World - Welcome to Apache Ant Tutorial by Tutor ${tutor}!!!</echo>
   </target>
   
</project>

build.properties

tutor=Dinesh Rajput

Running Ant on the above build file produces the following output:

Output is similar as above.

 

Previous
Next
Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

2 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

2 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

2 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

2 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

2 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

3 years ago