Gradle series 13-multi-project build

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:
Insert picture description here
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:
Insert picture description here
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:

  1. All projects need to use java plug-ins, web projects also need to rely on the Java environment
  2. The web subproject needs to be war package
  3. Unified configuration of common attributes, such as: group, version
  4. Unified management of resource library
  5. Common dependency configuration, such as the introduction of logback log function

Sample demonstration steps

Create root project and all submodule projects
  1. Create a Gradle Java project, ArtifactId: comment
    Insert picture description here
    Insert picture description here

  2. Create a core module under the comment project, ArtifactId: core
    Insert picture description here
    Insert picture description here
    Insert picture description here
    Insert picture description here

  3. Create a model module under the comment project. The
    specific creation steps of ArtifactId: model are the same as those of core

  4. 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
    Insert picture description here

  5. Create a web module under the comment project, ArtifactId: web
    and admin are the same

    After creating all the modules, the project structure is shown in the following figure:
    Insert picture description here
    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:
    Insert picture description here
    Let's look at setting.gradle File: The
    Insert picture description here
    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'
}

Insert picture description here
Remove java plugin introduction and version specification in all submodules
Insert picture description here

build verification
Insert picture description here
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'
    }
}

Published 159 original articles · 22 praises · 90,000+ views

Guess you like

Origin blog.csdn.net/ytuglt/article/details/105026287