build.gradle 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. plugins {
  2. id 'java'
  3. id 'application'
  4. id "com.github.johnrengelman.shadow" version "8.1.1"
  5. }
  6. group = 'com.incubator.app'
  7. version = '1.0-SNAPSHOT'
  8. // 使用 Gradle 的现代 API 定义源集
  9. sourceSets {
  10. main {
  11. resources {
  12. srcDirs = ['conf'] // 移除 += 以明确覆盖默认值
  13. }
  14. }
  15. }
  16. application {
  17. // 类型安全的主类配置
  18. mainClass = 'App'
  19. // 配置 JVM 参数,支持高并发和性能优化
  20. applicationDefaultJvmArgs = [
  21. "-XX:+UseZGC", // 使用 ZGC 垃圾回收器,适合高并发
  22. "-Xmx2G", // 限制最大堆大小
  23. "-XX:+UnlockExperimentalVMOptions", // 启用实验性选项
  24. "-XX:ZUncommitDelay=300", // 减少内存未使用时的延迟
  25. "-XX:+HeapDumpOnOutOfMemoryError", // OOM 时生成堆转储
  26. "-XX:ZCollectionInterval=3",
  27. "-Dio.netty.allocator.numDirectArenas=0", // 禁用堆外内存
  28. "-Dio.netty.leakDetection.level=PARANOID"
  29. ]
  30. }
  31. tasks.withType(JavaCompile).configureEach {
  32. options.compilerArgs += "--enable-preview"
  33. }
  34. // 禁用默认的 JAR 任务,因为我们使用 Shadow JAR
  35. tasks.named('jar') {
  36. enabled = false
  37. }
  38. // 配置 Shadow JAR 任务
  39. tasks.shadowJar {
  40. zip64 = true
  41. // 设置 JAR 包的基本名称
  42. archiveBaseName = 'incubator-app'
  43. // 使用项目的版本号作为 JAR 的版本
  44. archiveVersion = project.version
  45. // 移除分类器,生成的 JAR 没有后缀
  46. archiveClassifier = ''
  47. // 添加自定义清单
  48. manifest {
  49. // 使用动态设置的主类
  50. attributes(
  51. 'Main-Class': application.mainClass.get()
  52. )
  53. }
  54. // 将 conf 目录包含到最终的 JAR 文件中
  55. from('conf') { into('conf') }
  56. // 可选:排除不必要的依赖
  57. exclude 'META-INF/*.SF', 'META-INF/*.DSA', 'META-INF/*.RSA'
  58. // 避免重复打包无关的文件
  59. duplicatesStrategy = DuplicatesStrategy.EXCLUDE
  60. }
  61. // 项目依赖
  62. dependencies {
  63. // 子模块依赖
  64. implementation project(':incubator-game')
  65. // 测试依赖
  66. testImplementation platform("org.junit:junit-bom:5.10.0")
  67. testImplementation "org.junit.jupiter:junit-jupiter-api"
  68. testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine"
  69. }
  70. // 配置测试任务
  71. tasks.test {
  72. useJUnitPlatform()
  73. testLogging {
  74. events 'PASSED', 'FAILED', 'SKIPPED'
  75. }
  76. maxParallelForks = Runtime.runtime.availableProcessors() // 并行测试
  77. }
  78. // 将 Shadow JAR 绑定到 assemble 阶段
  79. tasks.named('assemble') {
  80. dependsOn tasks.named('shadowJar')
  81. }
  82. // 增加构建信息任务
  83. tasks.register("buildInfo") {
  84. doLast {
  85. def mainClass = application.mainClass.get()
  86. def shadowJarPath = tasks.shadowJar.flatMap { it.archiveFile }.get().asFile.absolutePath
  87. println """
  88. |Project: ${project.name}
  89. |Version: ${project.version}
  90. |Main Class: $mainClass
  91. |Shadow JAR Path: $shadowJarPath
  92. """.stripMargin()
  93. }
  94. }
  95. // 清理任务优化
  96. tasks.named('clean') {
  97. delete(layout.buildDirectory)
  98. }