Global variable in multi-project build

Hi, I need to figure out how can I create a global variable that will be use for in mutli-build project.

This variable will be create based on argument passed to the build.

For example suppose that we have the following project layout:

 +sub-projectN ..

My build look like this:

subprojects {
def hasMyArgument=hasProperty('myArgumentValue')

Now when I try to check that the argument is passed from one of the sub-project, I’ve got a error of unknown variable. For example:

if (hasMyArgument){

Is this related to the moment when we evaluate the value of the parameter or what? Because I have tried also afterEvaluate{} and it doesn’t work for me.

Thanks !!

A regular Groovy variable will be scoped to the local block, in this case the subproject { } block and not available from other build scripts. You should use extra properties for this.

subprojects {
  ext {
    hasMyArgument = 'foo'

Thanks for the tip Mark!!
This can be considered as an half answer for my question. Let me make my question more clear:
My main purpose was to provide a conditional build ( or scope) which will be activated when I pass an argument to a build (something like a flag):

# case 1: Flag non activated, we use the default configuration
# flag=value 
# case 2: Flag activated, custom configuration enabled, we remove the commented value

The behavior of the build depends on the value of flag( case 1 or case 2), it’s like a profile right. So based on your answer I successfully manage to provide the flag for all sub-project. But this won’t work for the case 1 because hasProperty can’t be invoked and evaluated inside the ext{} tag.

Perhaps you wondering why I do it like this, well I need to ensure the availability of my build even in worst case scenarios, I Hope that the situation is more clear now.

Finally the best answer for this situation would be like this:

subprojects {
  ext {
       hasMyArgument= has('flag')&&(flag!=null)

Thanks again,

In this case then there is no need to define a new extra property or variable anywhere. When you set a property via the CLI (ex -Pmyprop) then it will be accessible from every project in your multi-project build. You can simply check for the property directly in your subproject build scripts.

if(hasProperty('myprop') {
    // do some stuff

Hi, I’m still a newbie in gradle. I’d appreciate if you could show me the snippet of code to create this global variable in the Project build.gradle file and how to reference and use it in each subproject using Java code. For example, say I want to create a global version for the whole project and reference this global version from any of my subprojects. Thanks in advance!

FYI, there is already a version property on the Project object you can use for this. Just add version = '1.0' to you build.gradle. Assuming you want to add any arbitrary property, take a look at the example below.

Root build.gradle file:

ext.globalVersion = '2.1.0'

Subproject build.gradle file:

task foo {
    doLast {