I am currently trying to migrate a Maven
project to Gradle
In my Maven
version I have the dependency versions all listed out l
Adding to Vikas Sachdeva's answer:
It's not worked for me in that way on my subproject. The Gradle throws an error 'Could not find org.springframework:spring-context:$springVersion'. Finally, I solved my problem by adding the plus sign.
for example:
root gradle.properties:
springVersion = '5.0.3.RELEASE'
root build.gradle
dependencies {
compile "org.springframework:spring-context:${springVersion}"
}
subproject build.gradle
dependencies {
compile "org.springframework:spring-context:" + springVersion
}
Note: my gradle version is 5.4.1
or you can using the plugin manage your dependency version.
io.spring.dependency-management
Refs: https://docs.spring.io/dependency-management-plugin/docs/current/reference/html/
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}" }
the version-value-variable works only in double quotes.
Below implementation doesn't work with single quotes.
ext {
springBootVersion = '2.2.7.RELEASE'
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web:$springBootVersion'
}
Below one works with double quotes:->
ext {
springBootVersion = '2.2.7.RELEASE'
}
dependencies {
implementation "org.springframework.boot:spring-boot-starter-web:$springBootVersion"
}
So, now I have to convert all single quotes to double quotes. Can someone help me to make version-value-variable work with single quotes?
I have a an alternate syntax sugar. I guess this is the "long" way of writing the dependency.
master "gradle.build"
plugins {
id 'java'
}
ext {
slf4jApiVersion = '1.7.28'
junitVersion = '4.12'
}
sub module / project gradle.build
dependencies {
testCompile group: 'junit', name: 'junit', version: "${junitVersion}"
compile group: 'org.slf4j', name: 'slf4j-api', version: "${slf4jApiVersion}"
}
note the seperation of group, name and version... SINQLE quotes around group and name values, and DOUBLE quotes around the version-value-variable.
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"
}
The way you've provided dependency version is correct and should work. IntelliJ Idea has some troubles with variables within gradle build scripts, so you don't have to rely on it, just try to run your build and check out, whether is dependency downloaded and your project is build correctly.
If you wish to store your project dependencies in the separate file, then the most common approach is to define them in the root build.script, via subprojects
closure for all subprojects or via allprojects
if you wish to specify dependency for all your projects include the root. This will look like this:
subprojects{
dependencies{
compile ...
}
}
This way you can provide any common configuration for subprojects within the root build script. Read about it the user guide
Or you can declare a variable in the root build script, initialize it as an map with all the dependency specifications you need or as an array with single dependencies list and use this variable within your subprojects, if you need them. Somethin like this in the root:
ext.commomLibs = [
'testlibs' : 'junit:junit:4.8'
]
Note, you can define a value of the map as an array of the lib specification to make it pointing to all this dependencies. And then you can use it in your subprojects as:
dependencies{
testCompile commomLibs.testlibs
}
One more alternative is to use separate build script with dependencies and apply it to the buils script you need, like so
apply from: 'dependencies.script'
Then you can place your dependencies into dependencies script and just apply it in any other build script, where you need this dependenies and/or may be some common logic.