使用Gradle构建spring-boot多模块项目

一、目录结构

.
├── boogle-common
│   ├── build.gradle.kts
│   └── src
├── boogle-core
│   ├── build.gradle.kts
│   └── src
├── boogle-generator
│   ├── build.gradle.kts
│   └── src
├── boogle-logging
│   ├── build.gradle.kts
│   └── src
├── boogle-quartz
│   ├── build.gradle.kts
│   └── src
├── boogle-system
│   ├── build.gradle.kts
│   └── src
├── boogle-web-starter
│   ├── build.gradle.kts
│   └── src
├── buildSrc
│   ├── build
│   ├── build.gradle.kts
│   └── src
│       └── main
│           └── kotlin
│               └── org.wm.java-conventions.gradle.kts
├── gradle
│   └── wrapper
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── settings.gradle.kts

二、引入spring-boot相关依赖

buildSrc/build.gradle.kts

plugins {
    `kotlin-dsl`
  	// spring-boot 依赖管理插件
    id("org.springframework.boot") version "2.7.3"
}
// spring-boot 依赖管理插件
apply(plugin = "io.spring.dependency-management")

// 指定启动类
tasks.named<org.springframework.boot.gradle.tasks.bundling.BootJar>("bootJar") {
    mainClass.set("org.wm.WebAppApplication")
}
// 自定义slf4j版本
extra["slf4j.version"] = "1.7.20"
repositories {
    maven {
        url = uri("https://plugins.gradle.org/m2/")
    }
    gradlePluginPortal()
}
dependencies {
  // spring-boot插件依赖
    implementation("org.springframework.boot:spring-boot-gradle-plugin:2.7.3")
}

buildSrc/src/main/kotlin/org.wm.java-conventions.gradle.kts

import org.springframework.boot.gradle.tasks.bundling.BootJar

plugins {
    `java-library`
    `maven-publish`
    id("org.springframework.boot")
}
apply(plugin = "io.spring.dependency-management")

tasks.named<BootJar>("bootJar") {
    mainClass.set("org.wm.WebAppApplication")
}

repositories {
    mavenLocal()
    maven {
        url = uri("https://maven.aliyun.com/repository/public/")
    }
    maven {
        url = uri("https://repo.maven.apache.org/maven2/")
    }
}

dependencies {
    api("org.springframework.boot:spring-boot-starter-web")
    api("org.springframework.boot:spring-boot-configuration-processor")
    api("org.projectlombok:lombok:1.18.24")
    annotationProcessor("org.projectlombok:lombok:1.18.24")
    testAnnotationProcessor("org.projectlombok:lombok:1.18.24")
    testImplementation("org.springframework.boot:spring-boot-starter-test")
    testImplementation("io.projectreactor:reactor-test:3.4.17")
}

group = "org.wm"
version = "1.0.0-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_17
publishing {
    publications.create<MavenPublication>("maven") {
        from(components["java"])
    }
}
tasks.withType<JavaCompile>() {
    options.encoding = "UTF-8"
}

三、子项目依赖

boogle-common/build.gradle.kts

plugins {
    id("org.wm.java-conventions")
}
dependencies {
    api(project(":boogle-core"))
    api("org.springframework.boot:spring-boot-starter-aop")
    api("org.springframework.boot:spring-boot-starter-data-redis")
}

description = "通用工具模块"

boogle-core/build.gradle.kts

plugins {
    id("org.wm.java-conventions")
}
dependencies {
    api("org.springframework.boot:spring-boot-starter-security")
    testImplementation("org.springframework.security:spring-security-test:5.6.3")
}
description = "核心模块"

四、settings.gradle.kts配置

rootProject.name = "XunLuAdmin"
include(":boogle-core")
include(":boogle-system")
include(":boogle-web-starter")
include(":boogle-logging")
include(":boogle-generator")
include(":boogle-quartz")
include(":boogle-common")

五、打包

./gradlew bootJar

如果使用IDEA可以直接点击/Tasks/build/bootJar任务打包。

六、参考文档

spring-boot插件地址

Spring-boot依赖配置地址

Gradle官方文档

posted @ 2022-08-28 14:48  我爱这世间美貌女子  阅读(913)  评论(0编辑  收藏  举报