Sunday, December 8, 2013

Global properties in gradle

When we have multiple projects and we may want to refer global properties such as dependencies by all sub-projects.

For example we have a project structure like this

So we have a root project called ProjectX and two sub projects called ProjectA and ProjectB. Each of them has their own build.gradle file for gradle build. By default, they all should be named as build.gradle. To distinguish them, we denote them as

  • build.gradleX
  • build.gradleA
  • build.gradleB

Say we want to use the same version of JUnit in all projects, so we need to use the same JUnit dependency and reference the same dependency across all projects.

In build.gradleX, we define a property
ext {
    lib = [
        junit: 'junit:junit:4.11'

In build.gradleA and build.gradleB we refer the dependency by
dependencies {
     compile lib.junit  //This references the property defined in build.gradleX

The mechanism of the above code is, the build.gradleX defines a property in root project(ProjectX) so in its subjects, lib.junit will be read inherited from the root project. But if we define ext.lib in PojectA as well like below then the lib.junit will use its local value. This is good if we want to override the value but if we don't know what happens behind the scene, mistake occurs.
in build.gradleA
ext {
    lib = [
        junit: 'junit:junit:3.0.0'

So, to refer the lib.ext we use
rootProject.lib.junit    //referencing the absolute root project 
parent.lib.junit         //referencing relative parent project which is the root here


  1. When I initially commented, I clicked the “Notify me when new comments are added” checkbox and now each time a comment is added I get several emails with the same comment. Is there any way you can remove people from that service? Thanks.
    Amazon Web Services Training in OMR , Chennai | Best AWS Training in OMR,Chennai
    Amazon Web Services Training in Tambaram, Chennai|Best AWS Training in Tambaram, Chennai

  2. This comment has been removed by the author.

  3. Superb. I really enjoyed very much with this article here. Really it is an amazing article I had ever read. I hope it will help a lot for all. Thank you so much for this amazing posts and please keep update like this excellent article. thank you for sharing such a great blog with us.

    angularjs Training in online

    angularjs Training in bangalore

    angularjs Training in bangalore

    angularjs Training in btm

  4. This is my 1st visit to your web... But I'm so impressed with your content. Good Job!
    Python training in marathahalli | Python training institute in pune

  5. This comment has been removed by the author.

  6. Wow it is really wonderful and awesome thus it is very much useful for me to understand many concepts and helped me a lot. it is really explainable very well and i got more information from your blog.

    blueprism training in chennai | blueprism training in bangalore | blueprism training in pune | blueprism online training

  7. I have been meaning to write something like this on my website and you have given me an idea. Cheers.
    Java training in Indira nagar | Java training in Rajaji nagar

    Java training in Marathahalli | Java training in Btm layout