gradle - 读取文件的Gradle Multi项目构建

标签 gradle build.gradle

嗨,我有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/

相关文章:

kotlin - 如何在Kotlin DSL上安装JUnit5?

java - 通过命令行运行spring boot应用的非主类

Android Studio 构建 2 个包含相同包的依赖项

build.gradle.kts 中的 androidTest Groovy 模拟

gradle - 在gradle文件中指定多个依赖项

android - 条目名称 'AndroidManifest.xml' 冲突(将 android gradle 插件更新到 3.6.0 后构建失败)

java - 'Gradle cucumber' 与 testImplementation 不起作用

gradle - 无法在 groovy 空对象上调用 multip() 方法

java - 使用 Kotlin 构建 Guava 依赖打破 Jar

kotlin - 在 Gradle 中合并多个提供程序。 Provider.map 与 project.provider 之间的区别