gpt4 book ai didi

gradle - Gradle如何找到一个setings.gradle文件

转载 作者:行者123 更新时间:2023-12-03 04:04:29 24 4
gpt4 key购买 nike

在一个普通的项目中,我可以有这样的结构:

myProject/
build.gradle
gradle.properties
**settings.gradle**

但如果我有另一个项目

myProject2/
build.gradle
gradle.properties

它工作正常。

当 myProject2 是 myProject 的子文件夹(但不是子项目)时,事情变得很奇怪。

myProject/
myProject2/
build.gradle
gradle.properties
build.gradle
gradle.properties
settings.gradle

gradle 如何找到要使用的settings 文件?

最佳答案

settings.gradle 文件有以下两个主要用途:

  1. 将库添加到您的构建脚本类路径
  2. 定义哪些项目参与多项目构建

因此它对于单个项目构建是可选的,但由于 #2 它对于多项目构建是强制性的。

默认情况下假定设置文件的位置也是根项目的位置,但您可以在设置文件中重新定义根项目的位置。

现在,gradle 逻辑用于定位 settings.gradle 文件,如 gradle 用户指南 here 中所述:

If you trigger a multiproject build from a directory with a settings file, things are easy [Amnon - you just found it]...

If you execute Gradle from within a project with no settings.gradle file, Gradle looks for a settings.gradle file in the following way:

  • It looks in a directory called master which has the same nesting level as the current dir.

  • If not found yet, it searches parent directories.

  • If not found yet, the build is executed as a single project build. If a settings.gradle file is found, Gradle checks if the current project is part of the multiproject hierarchy defined in the found settings.gradle file.

  • If not, the build is executed as a single project build. Otherwise a multiproject build is executed. Otherwise (you executed Gradle from

(Gradle 用户指南还提供了此行为的目的 here )。

现在,回到您的案例,您提供的前两个项目布局用于单个项目构建,因此 settings.gradle 是可选的。对于您的第三个项目布局,如果您将从根项目 (myProject) 运行 gradle,那么它会在其旁边找到 settings.gradle 但如果您将从 myProject2 文件夹,因为此项目父路径包含一个 settings.gradle 文件,gradle 将找到并使用它。

关于gradle - Gradle如何找到一个setings.gradle文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30235390/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com