Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using variables in Gradle build script

Tags:

gradle

groovy

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?

like image 803
huzeyfe Avatar asked Jan 08 '16 11:01

huzeyfe


People also ask

What is the use of Buildscript in Gradle?

Gradle builds a script file for handling two things; one is projects and other is tasks. Every Gradle build represents one or more projects. A project represents a library JAR or a web application or it might represent a ZIP that is assembled from the JARs produced by other projects.

How do you pass JVM arguments in Gradle?

Try to use ./gradlew -Dorg. gradle. jvmargs=-Xmx16g wrapper , pay attention on -D , this marks the property to be passed to gradle and jvm. Using -P a property is passed as gradle project property.


2 Answers

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!

like image 194
hzitoun Avatar answered Oct 05 '22 22:10

hzitoun


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" } 
like image 23
Giuseppe Ricupero Avatar answered Oct 06 '22 00:10

Giuseppe Ricupero