If else condition in Gradle

not-a-bug

(Ganesh) #1

I am new to Gradle and Groovy and I have a question on the if else construct used in build scripts

I have this really strange behavior in Gradle and I cannot find a way out of it. In my gradle.properties file, I am using this checking condition:

//gradle.properties

if ( "${System.Property['DATABASE_DIR']}".compareTo('swdb') == 0 ) {
    PROJECT_DATABASE_PATH=../database/swdb/include
    }   
else {
    PROJECT_DATABASE_PATH=../database/include/
}

I created a new task called printProperties and it looks like this.

//build.gradle
task printProperties {
    println "${System.properties['DATABASE_DIR']}".compareTo('swdb') == 0
    println PROJECT_DATABASE_PATH
}

I get the following output when I run the printProperties task.

$gradle printProperties -DDATABASE_DIR=swdb
true
../database/include/
:printProperties UP-TO-DATE
BUILD SUCCESSFUL
Total time: 1.07 secs

It is really strange that the task prints true but the gradle.properties file does not evaluate the same condition correctly. Could anybody help me with this?

However, if I am not allowed to code small constructs in the gradle.properties file, how do I read a command line defined system property based on which I choose the location of my headers in the prebuilt Libraries part of my native C Gradle model?


(James Justinic) #2

It is the case that you are not allowed to write logic in the gradle.properties file. A properties file is just for properties themselves. In your example, the path will always be ‘../database/include/’ because the last value set for PROJECT_DATABASE_PATH wins.

In your example, PROJECT_DATABASE_PATH ends up as project property, so I am expecting that’s what you want to end up with to then use in your native model configuration. One option you have is to define both paths with different keys in your properties file. Then set ext.PROJECT_DATABASE_PATH to be the one you want based on your condition. You could also build the path based on DATABASE_DIR, especially if swdb could be one if many possibilities.