jskidd3
jskidd3

Reputation: 4783

Can't use variable defined in gradle.properties

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'
}

Upvotes: 0

Views: 4412

Answers (2)

Shivaraja HN
Shivaraja HN

Reputation: 168

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.

Upvotes: 1

Apurv
Apurv

Reputation: 401

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 :)

Upvotes: 0

Related Questions