91

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?

2
  • 1
    Don't forget, you can break it out as well: doStuff group: 'org.springframework', name: 'spring-web', version: ${springVersion}, ext: 'war' Commented Feb 7, 2017 at 21:41
  • 1
    Don't forget, you can remove the parenthesis {} from ${spingVersion} as well: doStuff group: 'org.springframework', name: 'spring-web', version: $springVersion, ext: 'war' Commented Jun 8, 2018 at 3:37

5 Answers 5

109

If you're developing an Android application 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}"
}
Sign up to request clarification or add additional context in comments.

Comments

57

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"
}

1 Comment

True. Changing from single to double quotes fixed my problem
20

On android there are actually 2 possibilities how to achieve this. It really depends which suits your needs. Those two possibilities have their pros and cons. You can use def variable or ext{} block. Variable def is awesome because it lets you click on the variable and points exactly where it is defined in the file compared to ext{} block which does NOT points to that exact variable. On the other hand ext{} has one good advantage and that is you can refer variables from project_name/build.gradle to project_name/app/build.gradle which in some cases is very useful BUT as I said if you click on that variable lets say only inside only one file it wont points out to the definition of that variable which is very bad because it takes you more search time if your dependency list grows.

1) def option which is propably best and saves you search time.

def lifecycle = '2.0.0'

dependencies {
    implementation 'androidx.lifecycle:lifecycle-extensions:$lifecycle'
}

2) second ext{} block. Its kinda ok if dependency list is not huge.

ext {
    lifecycle = '1.1.1'
}

dependencies {
    implementation 'androidx.lifecycle:lifecycle-extensions:$lifecycle'
}

3) In some cases if you want to share variables between project_name/build.gradle and project_name/app/build.gradle use ext{}

in project_name/build.gradle you define kotlin_shared_variable:

buildscript {
    ext.kotlin_shared_variable = '1.3.41'

    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_shared_variable"
    }
}

which you can use in project_name/app/build.gradle

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_shared_variable"
}

and of course you can combine them.

1 Comment

So, the ext{} option works, but I cannot override that property using the -Plifecycle=2.0.0 command-line flag. The only way I've seen to do this is by providing the property in gradle.properties. Is that the recommended way of doing that?
1

You can also define variables in the gradle.properties file at the root of your project. You don't have to use double quotes in that file. You would need to add the following line:

lifecycle=2.0.0

1 Comment

the ext { } approach did not work for me however this one did. so 1 vote to Doctor
0

see here.

Double-quoted strings are plain java.lang.String if there’s no interpolated expression, but are groovy.lang.GString instances if interpolation is present.

Gradle uses Groovy as a DSL. Here "${springVersion}" is a placeholder, what you want is to interpolate, so you should use the double quote, only the double quote in GString has the capability to interpolate.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.