Using variables in Gradle build script

I am using Gradle in my project. I have a task for doing some extra configuration with my war. I need to build a string to use in my task like, lets say I have:

task extraStuff{
    doStuff 'org.springframework:spring-web:3.0.6.RELEASE@war'
}

This works fine. What I need to do is define version (actually already defined in properties file) and use this in the task like:

springVersion=3.0.6.RELEASE

task extraStuff{
    doStuff 'org.springframework:spring-web:${springVersion}@war'
}

My problem is spring version is not recognised as variable. So how can I pass it inside the string?


If you're developing an Android APP using Gradle, you can declare a variable (i.e holding a dependency version) thanks to the keyword def like below:

def version = '1.2'
    
dependencies {
  compile "groupId:artifactId:${version}"
}

Hope that helped!


I think the problem may lay on string literal delimiters:

  1. The string literals are defined exactly as in groovy so enclose it in single or double quotes (e.g. "3.0.6.RELEASE");
  2. Gstrings are not parsed in single quotes strings (both single '...' or triple '''...''' ones) if i recall correctly;

So the code will be:

springVersion = '3.0.6.RELEASE' //or with double quotes "..."

task extraStuff{
    doStuff "org.springframework:spring-web:${springVersion}@war"
}