为什么 ./gradlew clean build 和 ./gradlew clean :build 有区别
Why is there a difference between ./gradlew clean build and ./gradlew clean :build
我有以下情况:
我有一个包含多个子项目的项目。今天我尝试通过命令行使用 gradle 构建项目。
当我执行 ./gradlew clean :build
时构建成功,但执行 ./gradlew clean build
时没有成功。它会导致不同的错误,具体取决于激活的子项目。 这是为什么?不应该一样吗?
这两个命令直接在彼此之后执行,没有更改代码,并且来自同一目录(基础目录,settings.gradle
所在的位置。
Intellij 的gradle-刷新有效,构建成功(但在我们的构建服务器上失败,如果相关的话)。
根据文档 https://docs.gradle.org/current/userguide/command_line_interface.html#executing_tasks_in_multi_project_builds 我假设它会做同样的事情,因为没有指定子项目,并且对所有子模块执行构建任务。根项目中没有名为 build
的文件夹,因此这应该不会造成混淆。我是不是理解错了?
我在网上搜索过,但是找不到结果,因为大多数搜索引擎都无法识别 :
,而 colon
会导致不相关的结果,例如 .
gradle版本是4.10.2
如果您需要更多信息,请告诉我。
./gradlew clean :build
和 ./gradlew clean build
之间存在差异,这就是为什么你有不同的行为:在第一种情况下你使用合格的任务名称,在另一种情况下你使用简单的任务姓名。这些文档 here and here 解释了执行任务的这两种方法:
- 使用简单的任务名称(
./gradlew test
):
The first approach is similar to the single-project use case, but Gradle works slightly differently in the case of a multi-project build. The command gradle test will execute the test task in any subprojects, relative to the current working directory, that have that task. So if you run the command from the root project directory, you’ll run test in api, shared, services:shared and services:webservice. If you run the command from the services project directory, you’ll only execute the task in services:shared and services:webservice.
=> 因此在根项目目录中执行 ./gradlew build
将触发执行根项目和所有子项目的 build
任务
- 使用合格的任务名称(
./gradlew :build
)
For more control over what gets executed, use qualified names (the second approach mentioned). These are paths just like directory paths, but use ‘:’ instead of ‘/’ or ‘\’. If the path begins with a ‘:’, then the path is resolved relative to the root project. In other words, the leading ‘:’ represents the root project itself. All other colons are path separators.
=> 执行 ./gradlew :build
,您将为 rootProject
执行 "only" build
任务
正如我在评论中所说,您的一个或多个子项目可能存在一些问题,但如果您仅执行根项目构建 (./gradlew :build
)
,您将不会看到这些错误
我有以下情况:
我有一个包含多个子项目的项目。今天我尝试通过命令行使用 gradle 构建项目。
当我执行 ./gradlew clean :build
时构建成功,但执行 ./gradlew clean build
时没有成功。它会导致不同的错误,具体取决于激活的子项目。 这是为什么?不应该一样吗?
这两个命令直接在彼此之后执行,没有更改代码,并且来自同一目录(基础目录,settings.gradle
所在的位置。
Intellij 的gradle-刷新有效,构建成功(但在我们的构建服务器上失败,如果相关的话)。
根据文档 https://docs.gradle.org/current/userguide/command_line_interface.html#executing_tasks_in_multi_project_builds 我假设它会做同样的事情,因为没有指定子项目,并且对所有子模块执行构建任务。根项目中没有名为 build
的文件夹,因此这应该不会造成混淆。我是不是理解错了?
我在网上搜索过,但是找不到结果,因为大多数搜索引擎都无法识别 :
,而 colon
会导致不相关的结果,例如
gradle版本是4.10.2
如果您需要更多信息,请告诉我。
./gradlew clean :build
和 ./gradlew clean build
之间存在差异,这就是为什么你有不同的行为:在第一种情况下你使用合格的任务名称,在另一种情况下你使用简单的任务姓名。这些文档 here and here 解释了执行任务的这两种方法:
- 使用简单的任务名称(
./gradlew test
):
The first approach is similar to the single-project use case, but Gradle works slightly differently in the case of a multi-project build. The command gradle test will execute the test task in any subprojects, relative to the current working directory, that have that task. So if you run the command from the root project directory, you’ll run test in api, shared, services:shared and services:webservice. If you run the command from the services project directory, you’ll only execute the task in services:shared and services:webservice.
=> 因此在根项目目录中执行 ./gradlew build
将触发执行根项目和所有子项目的 build
任务
- 使用合格的任务名称(
./gradlew :build
)
For more control over what gets executed, use qualified names (the second approach mentioned). These are paths just like directory paths, but use ‘:’ instead of ‘/’ or ‘\’. If the path begins with a ‘:’, then the path is resolved relative to the root project. In other words, the leading ‘:’ represents the root project itself. All other colons are path separators.
=> 执行 ./gradlew :build
,您将为 rootProject
build
任务
正如我在评论中所说,您的一个或多个子项目可能存在一些问题,但如果您仅执行根项目构建 (./gradlew :build
)