如何处理Gradle复合构建中的相对路径?

如何处理Gradle复合构建中的相对路径?,gradle,Gradle,我有一个Gradle复合构建,具有以下目录结构: compositebuildroot/ build.gradle settings.gradle componentbuild/ build.gradle settings.gradle testfile.txt 使用compositebuildroot/build.gradle包含 task runComponentBuildSampleTask { dependsOn gradle.includedB

我有一个Gradle复合构建,具有以下目录结构:

compositebuildroot/
  build.gradle
  settings.gradle
  componentbuild/
    build.gradle
    settings.gradle
    testfile.txt
使用
compositebuildroot/build.gradle
包含

task runComponentBuildSampleTask {
  dependsOn gradle.includedBuild('componentbuild').task(':sampleTask')
}
rootProject.name = 'compositebuildroot'
includeBuild 'componentbuild'
task sampleTask {
  doLast {
    println "System.getProperty(\"user.dir\"): " + System.getProperty("user.dir")
    println new File("testfile.txt").text
  }
}
rootProject.name = 'componentbuild'
compositebuildroot/settings.gradle
包含

task runComponentBuildSampleTask {
  dependsOn gradle.includedBuild('componentbuild').task(':sampleTask')
}
rootProject.name = 'compositebuildroot'
includeBuild 'componentbuild'
task sampleTask {
  doLast {
    println "System.getProperty(\"user.dir\"): " + System.getProperty("user.dir")
    println new File("testfile.txt").text
  }
}
rootProject.name = 'componentbuild'
compositebuildroot/componentbuild/build.gradle
包含

task runComponentBuildSampleTask {
  dependsOn gradle.includedBuild('componentbuild').task(':sampleTask')
}
rootProject.name = 'compositebuildroot'
includeBuild 'componentbuild'
task sampleTask {
  doLast {
    println "System.getProperty(\"user.dir\"): " + System.getProperty("user.dir")
    println new File("testfile.txt").text
  }
}
rootProject.name = 'componentbuild'
,和
compositebuildroot/componentbuild/settings.gradle
包含

task runComponentBuildSampleTask {
  dependsOn gradle.includedBuild('componentbuild').task(':sampleTask')
}
rootProject.name = 'compositebuildroot'
includeBuild 'componentbuild'
task sampleTask {
  doLast {
    println "System.getProperty(\"user.dir\"): " + System.getProperty("user.dir")
    println new File("testfile.txt").text
  }
}
rootProject.name = 'componentbuild'

当我从
componentbuild
目录执行
gradle sampleTask
时,我会打印出
componentbuild
目录的绝对路径,然后是
testfile.txt
的内容(这是我想要/期望的)。但是,当我从
compositebuildroot
目录执行
gradle runComponentBuildSampleTask
时,我得到
compositebuildroot
目录的绝对路径,然后是
testfile.txt
文件的
java.io.FileNotFoundException

如何从
compositebuildroot
生成
gradle-runComponentBuildSampleTask
中的
gradle-sampleTask
生成
componentbuild
中的任何输出?是否有某种方法可以告诉gradle运行组件生成任务,就像当前目录是组件生成的settings.gradle文件一样?

永远不要使用
新文件(…)

始终使用
文件(…)


请参见

这似乎是一个好主意,部分回答了这个问题,谢谢。不幸的是,我实际的componentbuild使用的插件显然在内部使用了
新文件(…)
,接受相对路径作为参数,所以事情可能要复杂一些。我怀疑,在其他一些情况下,我们可能也无法方便地修改组件构建,更不用说插件了。还有其他想法吗?放弃插件!不要相信使用
新文件(…)
的插件,它是由ametures编写的!我一定会考虑的!