Why is there a difference between ./gradlew clean build and ./gradlew clean :build
Asked Answered
C

1

16

I have the following situation:

I have a project with several subprojects. Today I tried to build the projects with gradle via command line.

The build was successful when I executed ./gradlew clean :build, but not with ./gradlew clean build. It causes different errors, depending on which subprojects are activated. Why is that? Shouldn't it be the same?

Both commands are executed directly after each other, without changes in the code, and from the same directory (the base-directory, where settings.gradle is located.

The gradle-refresh of Intellij works, the build is successful (but fails on our build server, if that is relevant).

According to the documentation https://docs.gradle.org/current/userguide/command_line_interface.html#executing_tasks_in_multi_project_builds I assumed that it would do the same, since no subproject is specified, and the build task is executed for all submodules. There is no folder called build in the root project, so this should not cause confusion. Am I interpreting it wrong?

I searched online, however, I could not find the result, since : is is not recognized by most search engines, and colon leads to not relevant results like What is the colon operator in Gradle?.

The gradle version is 4.10.2

If you need more information, please let me know.

Cassicassia answered 20/11, 2018 at 13:57 Comment(4)
when you execute ./gradlew clean :build : you execute task clean on all projects, but task build only on the root project, because :build references a task named "build" in the ":" project (root project). this is different than executing ./gradlew clean build: here you execute clean on all projects, then build on all projects. So maybe you have one subproject that has error in build execution: in the first case you won't see this error as you only build the rootProject.Shorn
@Shorn thanks for the reply. But I thought that build in the root project (called with :build) executes all build of subprojects too? If not, what does it do?Sharpwitted
the root project is a project itself: you will use :build if you want to only execute build task on this specific project and not subprojectsShorn
I posted an answer with an additional Gradle doc link which I think explains better the behavior : docs.gradle.org/current/userguide/…Shorn
S
32

There is a difference between ./gradlew clean :build and ./gradlew clean build, that's why you have a different behavior: in the first case you are using qualified task name, in the other case you are using simple task name. These documentations here and here explain these two approaches for executing tasks:

  • Using simple task name ( ./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.

=> so executing ./gradlew build in the root project directory will trigger execution of build task for root project and all subprojects

  • Using qualified task name ( ./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.

=> executing ./gradlew :build , you will execute "only" the build task for rootProject

As I said in comment, you migth have some issues in one or more of your subproject, but you will not see these errors if you execute only Root project build ( ./gradlew :build )

Shorn answered 22/11, 2018 at 8:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.