0

How to set JAR version with variable in Gradle?

If I write

ext.nd4j_version = "1.0.0-beta2"
ext.nd4j_backend = "nd4j-native-platform"

dependencies {

    compile group: 'org.nd4j', name: '${nd4j_backend}', version: '${nd4j_version}' 

it is unable to find artifacts.

And if I write

nd4j_version = "1.0.0-beta2"
nd4j_backend = "nd4j-native-platform"

dependencies {

    compile group: 'org.nd4j', name: '${nd4j_backend}', version: '${nd4j_version}' 

then it is able to find artifact, but issues a warning

Could not set unknown property 'nd4j_version' for root project 

UPDATE

The following also doesn't work

ext.nd4j_version = "1.0.0-beta2"
ext.nd4j_backend = "nd4j-native-platform"


dependencies {

    compile group: 'org.nd4j', name: '${project.ext.nd4j_backend}', version: '${project.ext.nd4j_version}' // works

and the following

ext.nd4j_version = "1.0.0-beta2"
ext.nd4j_backend = "nd4j-native-platform"


dependencies {

    compile group: 'org.nd4j', name: '${rootProject.ext.nd4j_backend}', version: '${rootProject.ext.nd4j_version}' 

and the following

ext {
    nd4j_version = "1.0.0-beta2"
    nd4j_backend = "nd4j-native-platform"
}


dependencies {

    compile group: 'org.nd4j', name: '${project.nd4j_backend}', version: '${project.ext.nd4j_version}'

Please give an answer with an explanation, not just random working syntax.

Dims
  • 47,675
  • 117
  • 331
  • 600
  • Possible duplicate of [How to set a version of dependencies on Gradle?](https://stackoverflow.com/questions/53000509/how-to-set-a-version-of-dependencies-on-gradle) – Martin Zeitler Oct 27 '18 at 20:21
  • 1
    `"${project.ext.nd4j_version}"` should work (the location of the variable definition in the `build.gradle` matters, because it either has refer to `project` or to `rootProject`). – Martin Zeitler Oct 27 '18 at 20:24
  • @MartinZeitler how would I know this from Gradle doc without reading it in it's entirety? – Dims Oct 27 '18 at 20:28
  • much of that only gets obvious while working with it. would suggest to add `ext {nd4j_version = "1.0.0-beta2"}` to the root project's `build.gradle` and then reference it within a module as `"${rootProject.ext.nd4j_version}"` ...which makes sense, when occasionally having to update several modules. – Martin Zeitler Oct 27 '18 at 20:34
  • eg. `compile "org.nd4j:nd4j-native-platform:${rootProject.ext.nd4j_version}"` ...but this only makes sense with several modules; for a single module, it's sufficient to define it at the module level. the `"` is important there, else there won't be any string substitution. – Martin Zeitler Oct 27 '18 at 20:40

1 Answers1

2

Remove the '{$}' and use the variable as it is.

ext.nd4j_version = "1.0.0-beta2"
ext.nd4j_backend = "nd4j-native-platform"

dependencies {
   compile group: 'org.nd4j', name: nd4j_backend, version: nd4j_version 

EDIT: You can also use double-quotes with ${}:

dependencies {
   compile group: 'org.nd4j', name: "${nd4j_backend}", version: "${nd4j_version}"
Kushagra Goyal
  • 252
  • 1
  • 8