What is the key difference between includeBuild(...)
and implementation(project(...))
in the Gradle build system? I cannot really see the usecase difference after reading the documentation:
What I am trying to do: Share same codebase for two separate projects: data classes (kotlix.serialization), external database dao, interfaces. It is not a full library but just some code snippets.
How can I connect the two projects in Intellij so that type hinting works?
The --include-build command-line argument turns the executed build into a composite, substituting dependencies from the included build into the executed build. Output of gradle --include-build ../my-utils run.
implementation – required to compile the production source code and are purely internal. They aren't exposed outside the package. compileOnly – used when they need to be declared only at compile-time, such as source-only annotations or annotation processors.
The api configuration should be used to declare dependencies which are exported by the library API, whereas the implementation configuration should be used to declare dependencies which are internal to the component.
compile dependencies. Rule 1: you should always use implementation rather than compile for dependencies, as compile is now deprecated or removed in Gradle 7+. Also bear in mind: Rule two: use the implementation dependency configuration if you need the dependency to be on both the compile and runtime classpaths.
buildSrc should be preferred over script plugins as it is easier to maintain, refactor and test the code. buildSrc uses the same source code conventions applicable to Java and Groovy projects. It also provides direct access to the Gradle API.
Composite Build (by using includeBuild
) is a way to create a dependency between autonomous Gradle Projects.
Project import, is a way to create a dependency between two modules within a same Gradle Project.
Composite Build is far more powerful and is also meant to be the new way of factorizing gradle configuration between multiple projects that you traditionally do with the buildSrc
technique.
I found this "Structuring Large Projects" article to be easier to read than the "Composite Builds" doc.
An excellent sample project that demonstrates the power of Composite Builds can be found in Gradle sample_structuring_software_projects.
The tree would look like this :
settings.gradle.kts
module1/build.gradle.kts
module2/build.gradle.kts
And your are declaring a dependency in module1/build.gradle.kts
like this :
dependencies{
implementation(project("com.domain:module2))
}
The dependency will be resolved only if both projects are declared as sub-modules of a common root project.
It means you have a root settings.gradle.kts
like this :
rootProject.name = "rootProject"
include(":module1")
include(":module2")
The projects do not need to have common "umbrella" root project. Each project is a totally independent project.
One project can simply declare a dependency on the other project (without even the target project knowing about it).
Tree :
project1/build.gradle.kts
project1/settings.gradle.kts
project2/build.gradle.kts
project2/settings.gradle.kts
In project1/settings.gradle.kts
:
rootProject.name = "project1"
includeBuild("../project2") //No more ':' as it is not a module
In project2/settings.gradle.kts
:
rootProject.name = "project2"
In project1/build.gradle.kts
like this :
dependencies{
implementation("com.domain:project2")
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With