嗨,我有2个项目,文件如下:
project1
\- build.gradle
project2
\- build.gradle
\- build.properties
project1:build.gradle
apply from: '/home/project2/build.gradle
test/test2/build.gradle'
task test1 {
println "Running task test 1"
}
test1.dependsOn test2
project2:build.gradle
task test2 {
println "Running task test 2"
Properties props = new Properties()
props.load(new FileInputStream("build.properties"))
}
当我从project1执行test1任务时,出现以下错误:
运行任务测试2
FAILURE: Build failed with an exception.
Where: Script '/home/project2/build.gradle' line: 4
What went wrong: A problem occurred evaluating script. build.properties (No such file or directory)
Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total time: 5.779 secs
由于gradle正在使用相对路径,因此它将尝试在project1目录中实际存在的project1目录中搜索build.properties文件。
我也可以将该文件移到project1目录中,但这是我不想做的。
我也可以在build.gradle中使用绝对路径,但这只是解决方案吗?
我想从
project2:test2
任务运行project1:test1
任务,而无需在project2:build.gradle中进行太多修改,因为我要在此文件中引用多个文件。
最佳答案
您可以像现在一样尝试不应用完整的构建脚本,而是根据project2中的任务从project1中创建任务,如下所示:
test1.dependsOn ':project2:test2'
但是为此,您需要使所有该项目均为同一根项目的子项目,并在根项目中具有一个
settings.gradle
文件,以定义该子项目。您可以在official documentation中阅读它。否则,您需要将属性文件移动到project1目录中,或者使用相对路径或绝对路径,因为应用只是扩展了当前构建脚本的一些其他行为,而并非如此,因为这是单独的项目。
关于gradle - 读取文件的Gradle Multi项目构建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33819897/