Gradle performs multi-project builds
The average small project has a build.gradle file and a sourse tree, but as the project gets bigger, the single module can’t meet the needs of engineers. If a large project is broken up into smaller, interdependent submodules, the architecture is usually clearer and easier to read, a process known as “modularization” in a colloquial sense. The word “interdependence” is important, which is why we often want to link sub-modules together through a single build.
Gradle supports this scenario with multi-project builds. Note that multi-project builds are actually Gradle project support (single Software Component).
An alternative approach is to divide a large-scale software project into multiple components, each of which is represented by a single Gradle build. This chapter focuses on Gradle multi-project builds
Create Gradle Multi-project builds
A multi-project Gradle build consists of a root project and sub-proejCT projects.
The most basic multi-project build consists of a root project and a subproject. Here is a multi-project build structure with a subproject named APP:
//Project Layout. ├─ app │... │ ├ ─ └─ buildCopy the code
The above structure is the official recommended Gradle project structure. The Build Init Plugin generates projects that follow this architectural framework
Note: Root project has no build.gradle file, only setttings. Gradle file, which configures which subprojects are involved in the build
// settings.gradle.kts
rootProject.name = "basic-multiproject"
include("app")
Copy the code
In this example, at build time, Gradle looks for the build.gradle file in the app directory to execute.
We can see the structure of a multi-project build by running the Gradle projects command.
> gradle -q projects
------------------------------------------------------------
Root project 'basic-multiproject'
------------------------------------------------------------
Root project 'basic-multiproject'
\--- Project ':app'
To see a list of the tasks of a project, run gradle <project-path>:tasks
For example, try running gradle :app:tasks
Copy the code
Suppose the APP subproject is a Java application, using the Application plug-in and configuring the main class:
// app/build.gradle.kts
plugins {
id("application")
}
application {
mainClass.set("com.example.Hello")
}
Copy the code
// app/src/main/java/com/example/Hello.java
package com.example;
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}
Copy the code
We can run the main function:
> gradle -q run
Hello, world!
Copy the code
Add subprojects
Now we want to add a subproject named ‘lib’ to the project. We need to add a line include to the setting.gradle file
// settings.gradle.kts
rootProject.name = "basic-multiproject"
include("app")
include("lib")
Copy the code
At build time gradle looks for a build file in the lib folder to execute
// Project Layout. ├─ app │... │ └ ─ ─ build. Gradle. KTS ├ ─ ─ lib │... │ ├ ─ ├ ─ sci-impCopy the code