In enterprise projects, a complex project is often divided into several small projects to complete collaboratively, which requires the construction of multiple projects. The construction of multiple projects requires the modularization of a large project and the completion of the entire function through the cooperation of modules.
Multi-project building block division and dependencies
Use a comment website (comment) to divide the modules and build the relationship. The modules are divided as follows:
core: Common core code, including business layer, data access layer code
model: model, used to encapsulate data
admin: background management Modules, including views and Controller
web: user operation modules, including views and Controller
In the previous multi-project build using Maven, a root project was generally needed to manage all modules in a unified manner. Gradle is also the same. It also requires a root project to manage all modules in a unified manner. The relationship diagram is as follows:
the common configuration of all projects (including the root project) is configured in allprojects, and the common configuration of all submodules can be configured in subprojects, build. Gradle configuration items for projects can be configured in allprojects / subprojects.
Demo content:
- All projects need to use java plug-ins, web projects also need to rely on the Java environment
- The web subproject needs to be war package
- Unified configuration of common attributes, such as: group, version
- Unified management of resource library
- Common dependency configuration, such as the introduction of logback log function
Sample demonstration steps
Create root project and all submodule projects
-
Create a Gradle Java project, ArtifactId: comment
-
Create a core module under the comment project, ArtifactId: core
-
Create a model module under the comment project. The
specific creation steps of ArtifactId: model are the same as those of core -
Create the admin module under the comment project. The
other steps of ArtifactId: admin are the same as the above steps, except that this admin is a web project, you need to check the web
-
Create a web module under the comment project, ArtifactId: web
and admin are the sameAfter creating all the modules, the project structure is shown in the following figure:
because the src under root is not available, delete the src folder selected in the above figure, and the final project structure is shown in the following figure:
Let's look at setting.gradle File: The
root directory is comment and includes four modules, so that our project is created.
Configure gradle
Configure dependencies
Take core dependent model as an example
dependencies {
//core 依赖model子模块
compile project(":model")
testCompile group: 'junit', name: 'junit', version: '4.12'
}
Similarly, admin & web can rely on core
Introduction of configuration common java plugin
In the build.gradle under the comment project, the java plug-in and version introduction configuration are transferred to allprojects, because the root project as a management project also needs to add the java plug-in, so subprojects cannot be used here
//配置统一的信息,包括root项目
allprojects {
//统一引入 java插件和版本指定
apply plugin: "java"
sourceCompatibility = 1.8
//统一配置公共属性,例如group,version
group 'top.zhexi'
version '1.0-SNAPSHOT'
}
Remove java plugin introduction and version specification in all submodules
build verification
Configure the public resource library. After the configuration is complete, you can see that each module has logback dependencies
//配置共用资源库
subprojects {
repositories {
mavenCentral()
}
dependencies {
compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.3'
}
}