build.gradle 2.7 KB

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