Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle includeBuild vs implementation project

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:

  1. https://docs.gradle.org/current/userguide/declaring_dependencies.html#sub:project_dependencies
  2. https://docs.gradle.org/current/userguide/composite_builds.html#separate_composite

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?

like image 678
Jan Veselý Avatar asked Feb 29 '20 11:02

Jan Veselý


People also ask

What is Includebuild in Gradle?

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.

What is the difference between compileOnly and implementation in Gradle?

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.

What is the difference between API and implementation scopes?

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.

What is compile and implementation in Gradle?

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.

Should I use buildSrc?

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.


1 Answers

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.

Project dependency case

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")

Composite build case

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")
}
like image 73
Renaud Avatar answered Sep 23 '22 03:09

Renaud