build.gradle 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. plugins {
  2. id 'java'
  3. id 'application'
  4. id "com.github.johnrengelman.shadow" version "7.1.2"
  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 参数,指定 conf 目录下的配置文件路径
  19. applicationDefaultJvmArgs = [
  20. "-XX:+UseG1GC", // 使用 G1 GC
  21. "-XX:MaxGCPauseMillis=200", // 控制 GC 停顿时间
  22. "-XX:+HeapDumpOnOutOfMemoryError", // 发生 OOM 时生成堆转储
  23. "-Dlog4j.configurationFile=conf/log4j2.xml"
  24. ]
  25. }
  26. // 禁用默认的 JAR 任务,因为我们使用 Shadow JAR
  27. tasks.jar {
  28. enabled = false
  29. }
  30. // 配置 Shadow JAR 任务
  31. tasks.shadowJar {
  32. zip64 = true
  33. // 设置 JAR 包的基本名称
  34. archiveBaseName.set('incubator-game')
  35. // 使用项目的版本号作为 JAR 的版本
  36. archiveVersion.set(project.version)
  37. // 移除分类器,生成的 JAR 没有后缀
  38. archiveClassifier.set('')
  39. // 添加自定义清单
  40. manifest {
  41. // 使用动态设置的主类
  42. attributes 'Main-Class': application.mainClass.get()
  43. }
  44. // 将 conf 目录包含到最终的 JAR 文件中
  45. from('conf') {
  46. into('conf') // 指定 JAR 内的路径
  47. }
  48. // 可选:排除不必要的依赖
  49. exclude 'META-INF/*.SF', 'META-INF/*.DSA', 'META-INF/*.RSA'
  50. // 避免重复打包无关的文件
  51. duplicatesStrategy = DuplicatesStrategy.EXCLUDE
  52. }
  53. // 依赖版本集中管理
  54. ext {
  55. junitVersion = '5.9.0'
  56. shadowVersion = '7.1.2'
  57. }
  58. // 项目依赖
  59. dependencies {
  60. // 子模块依赖
  61. implementation project(':incubator-core')
  62. // 测试依赖
  63. testImplementation "org.junit.jupiter:junit-jupiter-api:${junitVersion}"
  64. testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:${junitVersion}"
  65. }
  66. // 配置测试任务
  67. tasks.test {
  68. useJUnitPlatform()
  69. testLogging {
  70. events 'PASSED', 'FAILED', 'SKIPPED'
  71. }
  72. }
  73. // 将 Shadow JAR 绑定到 assemble 阶段
  74. tasks.assemble.dependsOn(tasks.shadowJar)
  75. // 增加构建信息任务
  76. tasks.register("buildInfo") {
  77. doLast {
  78. println "Building project '${project.name}' version '${project.version}'"
  79. println "Main class: ${application.mainClass.get()}"
  80. println "Shadow JAR: ${tasks.shadowJar.get().archiveFile.get()}"
  81. }
  82. }
  83. // 清理任务优化
  84. tasks.clean {
  85. delete "$buildDir/libs" // 确保清理构建目录
  86. }