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

张开发
2026/4/13 21:19:44 15 分钟阅读

分享文章

P03 | Gradle 构建配置:理解 build.gradle.kts
P03 | Gradle 构建配置理解 build.gradle.kts付费文章| 第一阶段环境与架构为什么用 Gradle 而不是 MavenGradle (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.withTypeKotlinCompile { kotlinOptions { freeCompilerArgs listOf(-Xjsr305strict) // 严格 null 检查 jvmTarget 11 } } // 测试任务 tasks.withTypeTest { useJUnitPlatform() }application.properties 关键配置# 服务器 server.port8080 server.servlet.context-path/pikachu # 数据库 spring.datasource.urljdbc:mysql://localhost:3306/slowtime?useUnicodetruecharacterEncodingUTF-8serverTimezoneAsia/Shanghai spring.datasource.usernamepikachu spring.datasource.passwordyour_password spring.datasource.driver-class-namecom.mysql.cj.jdbc.Driver # Druid 监控 spring.datasource.druid.stat-view-servlet.enabledtrue spring.datasource.druid.stat-view-servlet.url-pattern/druid/* # MyBatis-Plus mybatis-plus.mapper-locationsclasspath:mapper/**/*.xml mybatis-plus.configuration.log-implorg.apache.ibatis.logging.stdout.StdOutImpl mybatis-plus.global-config.db-config.table-prefix mybatis-plus.global-config.db-config.id-typeinput # 手动指定ID不自增 # JPA仅用于复杂查询不用于建表 spring.jpa.hibernate.ddl-autonone spring.jpa.show-sqlfalse # Redis spring.redis.hostlocalhost spring.redis.port6379 spring.redis.database0 # 自定义配置 pikachu.rsa.private-keyMIIEowIBAAK... # RSA 私钥实际部署时用环境变量 pikachu.rsa.public-keyMIIBIjANBgkq... # RSA 公钥环境分离dev / prod开发环境和生产环境用不同配置文件resources/ ├── application.properties ← 公共配置 ├── application-dev.properties ← 开发环境本地数据库 └── application-prod.properties ← 生产环境服务器数据库# application.properties spring.profiles.activedev # 开发时用 dev部署时改为 prod # application-dev.properties spring.datasource.urljdbc:mysql://localhost:3306/slowtime... logging.level.rootDEBUG # application-prod.properties spring.datasource.urljdbc:mysql://prod-server:3306/slowtime... logging.level.rootWARN常用 Gradle 命令# 编译 ./gradlew build # 运行开发模式 ./gradlew bootRun # 运行指定环境 ./gradlew bootRun --args--spring.profiles.activedev # 打包 ./gradlew bootJar # 只运行测试 ./gradlew test # 清理编译产物 ./gradlew clean # 查看依赖树 ./gradlew dependenciesAI 辅助如何让 AI 帮你添加依赖我需要在项目中集成阿里云 OSS SDK 请在 back/pikachu_plat/build.gradle.kts 中添加正确的依赖 并创建一个 OSSConfig.kt 配置类 从 application.properties 读取 accessKeyId、accessKeySecret、bucketName、endpointAI 会找到正确的 Maven 坐标添加到dependencies {}块并生成配置类。下一篇P04 → 全局请求加密RequestBodyAdvice 实现原理

更多文章