gradle project properties

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Latest posts by Prasad Kharkar (see all)

sometimes we need to defines certain properties or variable which we can use repetitively in the build script of project or sometimes we need to provide the build script, properties at runtime while running gradle build. We can define gradle project properties to solve this problem and we will see how it can be done in this article.

Gradle Project Properties

Gradle project properties are simply the properties which we provide for the current project build. They can be defined in following ways

  • build script properties
  • command line properties
  • from external files

Create a project called ProjectProperties and create a file build.gradle under it

Build Script Properties

These are the gradle project properties which can simply be provided by defining and using them in build scripts. For this, we need to use ext{} block. Consider the build.gradle file as below

Run the task in  build file using gradle showProperties, and you will see the output

definedProperty

Command Line Properties

We can gradle projecct properties on command prompt for a gradle build. Advantage is that value of property can be given at runtime. This use particularly useful when you want to use a project for a specific build environment. Update your build.gradle to below

Now run gradle as below and you’ll see gradle project properties printed

command line properties

 

Properties File

There is another way you can define gradle project properties, define them in a separate file called gradle.properties which exist at the same location as build.gradle. Contents of gradle.properties as below

and build.gradle file as below

Run it as below and you’ll see output

properties file

 

 

 

I hope the article helped understand how external gradle project properties can be used in a build

Share Button

Prasad Kharkar

Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Leave a Reply

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