123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- plugins {
- id 'java'
- id 'application'
- id "com.github.johnrengelman.shadow" version "8.1.1"
- }
- group = 'com.incubator.app'
- version = '1.0-SNAPSHOT'
- // 使用 Gradle 的现代 API 定义源集
- sourceSets {
- main {
- resources {
- srcDirs = ['conf'] // 移除 += 以明确覆盖默认值
- }
- }
- }
- application {
- // 类型安全的主类配置
- mainClass = 'App'
- // 配置 JVM 参数,支持高并发和性能优化
- applicationDefaultJvmArgs = [
- "-XX:+UseZGC", // 使用 ZGC 垃圾回收器,适合高并发
- "-Xmx2G", // 限制最大堆大小
- "-XX:+UnlockExperimentalVMOptions", // 启用实验性选项
- "-XX:ZUncommitDelay=300", // 减少内存未使用时的延迟
- "-XX:+HeapDumpOnOutOfMemoryError", // OOM 时生成堆转储
- "-XX:ZCollectionInterval=3",
- "-Dio.netty.allocator.numDirectArenas=0", // 禁用堆外内存
- "-Dio.netty.leakDetection.level=PARANOID"
- ]
- }
- tasks.withType(JavaCompile).configureEach {
- options.compilerArgs += "--enable-preview"
- }
- // 禁用默认的 JAR 任务,因为我们使用 Shadow JAR
- tasks.named('jar') {
- enabled = false
- }
- // 配置 Shadow JAR 任务
- tasks.shadowJar {
- zip64 = true
- // 设置 JAR 包的基本名称
- archiveBaseName = 'incubator-app'
- // 使用项目的版本号作为 JAR 的版本
- archiveVersion = project.version
- // 移除分类器,生成的 JAR 没有后缀
- archiveClassifier = ''
- // 添加自定义清单
- manifest {
- // 使用动态设置的主类
- attributes(
- 'Main-Class': application.mainClass.get()
- )
- }
- // 将 conf 目录包含到最终的 JAR 文件中
- from('conf') { into('conf') }
- // 可选:排除不必要的依赖
- exclude 'META-INF/*.SF', 'META-INF/*.DSA', 'META-INF/*.RSA'
- // 避免重复打包无关的文件
- duplicatesStrategy = DuplicatesStrategy.EXCLUDE
- }
- // 项目依赖
- dependencies {
- // 子模块依赖
- implementation project(':incubator-game')
- // 测试依赖
- testImplementation platform("org.junit:junit-bom:5.10.0")
- testImplementation "org.junit.jupiter:junit-jupiter-api"
- testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine"
- }
- // 配置测试任务
- tasks.test {
- useJUnitPlatform()
- testLogging {
- events 'PASSED', 'FAILED', 'SKIPPED'
- }
- maxParallelForks = Runtime.runtime.availableProcessors() // 并行测试
- }
- // 将 Shadow JAR 绑定到 assemble 阶段
- tasks.named('assemble') {
- dependsOn tasks.named('shadowJar')
- }
- // 增加构建信息任务
- tasks.register("buildInfo") {
- doLast {
- def mainClass = application.mainClass.get()
- def shadowJarPath = tasks.shadowJar.flatMap { it.archiveFile }.get().asFile.absolutePath
- println """
- |Project: ${project.name}
- |Version: ${project.version}
- |Main Class: $mainClass
- |Shadow JAR Path: $shadowJarPath
- """.stripMargin()
- }
- }
- // 清理任务优化
- tasks.named('clean') {
- delete(layout.buildDirectory)
- }
|