Benjamin Berman
Benjamin Berman

Reputation: 591

How to deal with relative paths in Gradle composite builds?

I have a Gradle composite build with the following directory structure:

compositebuildroot/
  build.gradle
  settings.gradle
  componentbuild/
    build.gradle
    settings.gradle
    testfile.txt

with compositebuildroot/build.gradle containing

task runComponentBuildSampleTask {
  dependsOn gradle.includedBuild('componentbuild').task(':sampleTask')
}

, compositebuildroot/settings.gradle containing

rootProject.name = 'compositebuildroot'
includeBuild 'componentbuild'

, compositebuildroot/componentbuild/build.gradle containing

task sampleTask {
  doLast {
    println "System.getProperty(\"user.dir\"): " + System.getProperty("user.dir")
    println new File("testfile.txt").text
  }
}

, and compositebuildroot/componentbuild/settings.gradle containing

rootProject.name = 'componentbuild'

.

When I execute gradle sampleTask from the componentbuild directory, I get the absolute path of the componentbuild directory printed out, followed by the contents of testfile.txt (which is what I want/expect). However, when I execute gradle runComponentBuildSampleTask from the compositebuildroot directory, I get the absolute path of the compositebuildroot directory followed by a java.io.FileNotFoundException for the testfile.txt file.

How can I make gradle runComponentBuildSampleTask from compositebuildroot produce whatever output gradle sampleTask from componentbuild does? Is there some way to tell gradle to run component build tasks as if the current directory is that of the component build's settings.gradle file?

Upvotes: 1

Views: 454

Answers (1)

lance-java
lance-java

Reputation: 27984

Never use new File(...)

Always use file(...)

See Project.file(Object)

Upvotes: 1

Related Questions