build.gradle 2.8 KB

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