Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I define a variable for the dependency version in Gradle

I am currently trying to migrate a Maven project to Gradle

In my Maven version I have the dependency versions all listed out like this in my parent pom:

<properties>     <spring.version>4.2.3.RELEASE</spring.version>     <spring.boot.version>1.3.3.RELEASE</spring.boot.version>     ... </properties> 

And then I can define a dependency like this in any of my sub-modules:

<dependency>     <groupId>org.springframework</groupId>     <artifactId>spring-jdbc</artifactId>     <version>${spring.version}</version> </dependency> 

I am attempting to do the same thing on Gradle, because some of my modules share dependency versions like this, and I wouldn't like to modify more then one place if I want to upgrade Spring or do a similar operation.

The closest I have gotten to it is this:

dependencies {     ext.springVersion = '4.2.3.RELEASE'     compile "org.springframework:spring-jdbc:$springVersion" } 

Yet that still doesn't work. What is the recommended way to achieve this in Gradle? Or does Gradle treat this differently? Perhaps my mind is still too much on Maven to see another solution.

Please keep in mind that that attempt on Gradle isn't exactly what I want yet. I would like to be able to define the dependencies in a separate file, not directly on the file that will use it.

like image 649
Rodrigo Sasaki Avatar asked Jun 29 '16 17:06

Rodrigo Sasaki


People also ask

How do you define Gradle dependencies?

Gradle build script defines a process to build projects; each project contains some dependencies and some publications. Dependencies refer to the things that supports in building your project, such as required JAR file from other projects and external JARs like JDBC JAR or Eh-cache JAR in the class path.

How do you write dependency in Gradle?

To add a dependency to your project, specify a dependency configuration such as implementation in the dependencies block of your module's build. gradle file. This declares a dependency on an Android library module named "mylibrary" (this name must match the library name defined with an include: in your settings.


2 Answers

Below configuration in build.gradle file worked for me with gradle version 4.5, posting it here for future reference -

ext {     springVersion = '5.0.3.RELEASE' }  dependencies {     compile  "org.springframework:spring-context:$springVersion" } 
like image 130
Vikas Sachdeva Avatar answered Oct 02 '22 16:10

Vikas Sachdeva


Use Double Quotes it worked for me.

buildscript {     ext {         springBootVersion = '2.0.4.RELEASE'         hazelCastVersion = '3.10.4'     }      dependencies {         classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")      } }  dependencies {     compile('org.springframework.cloud:spring-cloud-starter-config')     compile "com.hazelcast:hazelcast-spring:${hazelCastVersion}"     compile group: 'com.hazelcast', name: 'hazelcast', version: "${hazelCastVersion}" } 
like image 24
Kumar Abhishek Avatar answered Oct 02 '22 15:10

Kumar Abhishek