Currently, I have a Gradle project which contains multiple subprojects, when I try to build the main project, even for some sub-projects they don’t have the build.gradle
script inside them, but the build folder is still generated inside them. I also notice all of my Java modules (some of them are not declared as a subproject, and don’t have the build.gradle
file) inside project (I use IntelliJ) there is still a build folder inside each of them. And when I even try to remove implemetation project(...)
from my main project, the build folder in the sub-project is still generated as usual. I don’t understand why. Please help me clarify. Thanks a lot.
Whether you have a build script or not is not important.
If a project has no build script, it is treated like having an empty build script.
You can for example (though it is bad practice) inject the configuration from the root project using allprojects { ... }
or subprojects { ... }
, so a project does not necessarily need a build script.
The settings script defines which projects are part of the build.
If you include
a project there, you are defining it is a project, whehter it has a build script or not.
In some cases I group subprojects in a directory:
root
|_dirA
|_projA1
|_projA2
and include projA1
and projA2
in my settings file.
gradle will (sometimes?) create a build directory in dirA
. Why does gradle treat the directory like a project?
If you do include("dirA:projA1")
, you actually include project :dirA
if it is not included yet and its subproejct :dirA:projA1
, so you added two projects.
If you don’t want that, you need something like
include("projA1")
project(":projA1").projectDir = file("dirA/projA1")
until Settings DSL: Allow setting the project directory of a subproject directly as part of the `include()` statement · Issue #14561 · gradle/gradle · GitHub is implemented.
The project I am working on it has allprojects
in the root project. Is this the reason why the build
folders got generated? Or even if it doesn’t have this allprojects
, the build
folder is still generated when there is no build.script
? Thanks.
Besides that using allprojects
is a code smell in almost all cases and should be avoided, it is not a cause of any build folders being generated per-se. It only configures things. Where build folders get created depends on which tasks you execute. This can of course be influenced by something in that allprojects
block, but this cannot be said without seeing your full build. But well, ultimately as I said the build folders are there because they are defined as projects in your build using the settings script.
Hi, thanks for your answer. It seems like if I delete the include
statement in the settings.gradle
then the build
folder is not generated anymore. But I have another question, can I just build only one class or custom number of classes instead of the whole project? Thanks!
Only on a task level. So you would need separate compile tasks for the things you want to compile separately. But why do you want that? It compiles only changed and related classes anyway.