Spring Boot:使用 Gradle 构建多模块项目

此文章发布于 47 个月前,部分信息可能已经过时,请自行斟酌确认。

Gradle 项目最重要的配置文件是 build.gralde,使用 Gradle 构建多模块项目相比 Maven 资料会比较少一点,建议直接摘取下面示例源码使用。

示例源码

🌍https://github.com/ifu25/spring-boot-demo

开发环境

  • 操作系统:Win10
  • 开发工具:IDEA 2020.3
  • 开发语言:Kotlin + Java
  • JDK:11
  • Gradle:6.8

配置文件

根 build.gradle

buildscript {

    ext {
        springbootVersion = '2.4.4'
        kotlin_version = '1.4.31'
    }

    repositories {
        mavenLocal()
        maven { url "http://maven.aliyun.com/nexus/content/groups/public/" }
        mavenCentral()
    }

    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:$springbootVersion")
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

allprojects {

    apply plugin: 'java'
    apply plugin: 'kotlin'
    apply plugin: 'org.springframework.boot'
    apply plugin: 'io.spring.dependency-management'

    group 'cn.lttc'
    version '1.0.0'

    sourceCompatibility = 11

    repositories {
        //本地仓库
        mavenLocal()
        //阿里云
        maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
        //中央仓库
        mavenCentral()
    }

    //项目中所使用到的依赖,这里添加的依赖会被所有项目,包括root项目继承
    dependencies {
        implementation 'org.springframework.boot:spring-boot-starter-web'
        implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
        implementation "com.fasterxml.jackson.module:jackson-module-kotlin"
        testImplementation 'org.springframework.boot:spring-boot-starter-test'
    }

    compileKotlin {
        kotlinOptions {
            jvmTarget = "11"
        }
    }
    compileTestKotlin {
        kotlinOptions {
            jvmTarget = "11"
        }
    }
}

根 settings.gradle

rootProject.name = 'spring-boot-demo'

include 'spring-boot-demo-hello'
include 'spring-boot-demo-logback'

子项目 build.gradle

子项目的配置继承自 root 项目,所以如果没有特别的依赖或其它配置,基本是空白的。

plugins {
    
}

dependencies {

}

项目结构

20210320_132820.png

最后修改:2021 年 03 月 20 日 01 : 59 PM
如果觉得我的文章对你有用,请随意赞赏

发表评论