0

I am new to Gradle and am trying to create a configurable variable via the gradle.properties file.

To do this, I have created a gradle.properties file at the root of my project, and defined a build directory like this:

buildDir="~/my/custom/build/directory"

In my build.gradle file I have referenced the variable like this:

libsDirName = buildDir

This does not work. If I swap buildDir for the string in gradle.properties it builds to the correct location. Why is this happening?

Here is the complete build.gradle file:

plugins {
    id 'java-library'
}

// This fails
libsDirName = buildDir

// This builds correctly
libsDirName = "~/my/custom/build/directory"

repositories {
    jcenter()
}

dependencies {
    api 'org.apache.commons:commons-math3:3.6.1'
    implementation 'com.google.guava:guava:23.0'
    testImplementation 'junit:junit:4.12'
}
jskidd3
  • 4,609
  • 15
  • 63
  • 127

2 Answers2

1

Edit: To get only jar file to a specific directory.

I hope you can't restrict creating tmp, resources during gradle build. So The idea is to copy a jar file to a specific directory once the gradle build success.

I suggest referring this link to copy jar.

gradle - copy file after its generation

You change

buildDir="~/my/custom/build/directory"

to

buildDir=~/my/custom/build/directory

and try..

Also, can you add println buildDir in build.gradle file and check what it prints.

ThisaruG
  • 3,222
  • 7
  • 38
  • 60
Shivaraja HN
  • 168
  • 1
  • 10
0

That is not how you declare and access a variable in Gradle file. refer below :

Below is how you declare a variable

def buildDir = "~/my/custom/build/directory"

Below is how you use its value

libsDirName = "${buildDir}"

Let me know if you face any issue. Happy Coding :)

ThisaruG
  • 3,222
  • 7
  • 38
  • 60
Apurv
  • 391
  • 2
  • 9
  • The quotations marks around the variable are totally unnecessary as are the braces and dollar sign `libsDirName = buildDir` is fine. You only need to do it your way if you are doing string interpolation. You would only need the braces if you are evaluating an expression. – Michael Mar 12 '20 at 17:22