P03 | Gradle 构建配置:理解 build.gradle.kts

💰 付费文章 | 第一阶段:环境与架构

为什么用 Gradle 而不是 Maven?

Gradle (Kotlin DSL) Maven
配置语言 Kotlin(类型安全) XML(冗长)
构建速度 快(增量编译、缓存) 较慢
灵活性 高(可编程)
AI 支持 更好(训练数据更多)
学习曲线

本项目选择 Gradle Kotlin DSL:类型安全的配置 + 更好的 IDE 支持。


build.gradle.kts 完整解析

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

// 项目基本信息
group = "com.lohas.pikachu"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_11

// 构建脚本依赖(plugins 块用的工具)
buildscript {
    repositories {
        maven { url = uri("https://plugins.gradle.org/m2/") }
    }
}

// 应用插件
plugins {
    id("org.springframework.boot") version "2.3.0.RELEASE"
    id("io.spring.dependency-management") version "1.0.9.RELEASE"
    kotlin("jvm") version "1.6.10"
    kotlin("plugin.spring") version "1.6.10"   // @Component 等注解开放
    kotlin("plugin.jpa") version "1.6.10"      // JPA 无参构造函数
}

// 依赖仓库
repositories {
    // 国内镜像,速度更快
    maven { url = uri("https://maven.aliyun.com/repository/public") }
    maven { url = uri("https://maven.aliyun.com/repository/spring") }
    mavenCentral()
}

// 项目依赖
dependencies {
    // Spring Boot Web
    implementation("org.springframework.boot:spring-boot-starter-web")
    
    // Kotlin 标准库
    implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    
    // 数据库
    implementation("org.springframework.boot:spring-boot-starter-data-jpa")
    runtimeOnly("mysql:mysql-connector-java")
    
    // MyBatis-Plus
    implementation("com.baomidou:mybatis-plus-boot-starter:3.3.2")
    implementation("com.baomidou:mybatis-plus-generator:3.3.2")
    
    // Redis
    implementation("org.springframework.boot:spring-boot-starter-data-redis")
    
    // Druid 连接池
    implementation("com.alibaba:druid-spring-boot-starter:1.1.23")
    
    // 工具库
    implementation("cn.hutool:hutool-all:5.7.22")  // Swiss Army Knife
    implementation("commons-codec:commons-codec:1.15")  // 编解码工具
    
    // 模板引擎(代码生成器用)
    implementation("org.freemarker:freemarker:2.3.31")
    
    // 测试
    testImplementation("org.springframework.boot:spring-boot-starter-test")
}

// Kotlin 编译器配置
tasks.withType<KotlinCompile> {
    kotlinOptions {
        freeCompilerArgs = listOf("-Xjsr305=strict")  // 严格 null 检查
        jvmTarget = "11"
    }
}

// 测试任务
tasks.withType<Test> {
    useJUnitPlatform()
}

application.properties 关键配置

# 服务器
server.port=8080
server.servlet.context-path=/pikachu

# 数据库
spring.datasource.url=jdbc:mysql://localhost:3306/slowtime?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
spring.datasource.username=pikachu
spring.datasource.password=your_password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

# Druid 监控
spring.datasource.druid.stat-view-servlet.enabled=true
spring.datasource.druid.stat-view-servlet.url-pattern=/druid/*

# MyBatis-Plus
mybatis-plus.mapper-locations=classpath:mapper/**/*.xml
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
mybatis-plus.global-config.db-config.table-prefix=
mybatis-plus.global-config.db-config.id-type=input  # 手动指定ID,不自增

# JPA(仅用于复杂查询,不用于建表)
spring.jpa.hibernate.ddl-auto=none
spring.jpa.show-sql=false

# Redis
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.database=0

# 自定义配置
pikachu.rsa.private-key=MIIEowIBAAK...  # RSA 私钥(实际部署时用环境变量)
pikachu.rsa.public-key=MIIBIjANBgkq...  # RSA 公钥

环境分离:dev / prod

开发环境和生产环境用不同配置文件

resources/
├── application.properties          ← 公共配置
├── application-dev.properties      ← 开发环境(本地数据库)
└── application-prod.properties     ← 生产环境(服务器数据库)
# application.properties
spring.profiles.active=dev  # 开发时用 dev,部署时改为 prod

# application-dev.properties
spring.datasource.url=jdbc:mysql://localhost:3306/slowtime...
logging.level.root=DEBUG

# application-prod.properties
spring.datasource.url=jdbc:mysql://prod-server:3306/slowtime...
logging.level.root=WARN

常用 Gradle 命令

# 编译
./gradlew build

# 运行(开发模式)
./gradlew bootRun

# 运行(指定环境)
./gradlew bootRun --args='--spring.profiles.active=dev'

# 打包
./gradlew bootJar

# 只运行测试
./gradlew test

# 清理编译产物
./gradlew clean

# 查看依赖树
./gradlew dependencies

AI 辅助:如何让 AI 帮你添加依赖

"我需要在项目中集成阿里云 OSS SDK,
请在 back/pikachu_plat/build.gradle.kts 中添加正确的依赖,
并创建一个 OSSConfig.kt 配置类,
从 application.properties 读取 accessKeyId、accessKeySecret、bucketName、endpoint"

AI 会找到正确的 Maven 坐标,添加到 dependencies {} 块,并生成配置类。


下一篇

P04 → 全局请求加密:RequestBodyAdvice 实现原理

Logo

汇聚全球AI编程工具,助力开发者即刻编程。

更多推荐