build.gradle 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. repositories {
  9. mavenCentral()
  10. }
  11. sourceSets {
  12. main {
  13. resources {
  14. srcDirs += 'conf'
  15. }
  16. }
  17. }
  18. application {
  19. // 使用新的方法设置主类,适应 Gradle 7.0 及以上版本
  20. mainClass.set('com.incubator.game.GameServerStart')
  21. // 添加 JVM 参数,指定 conf 目录下的配置文件路径
  22. applicationDefaultJvmArgs = [
  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. attributes(
  42. 'Main-Class': application.mainClass.get() // 使用动态设置的主类
  43. )
  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. // 依赖版本集中管理
  53. ext {
  54. junitVersion = '5.9.0'
  55. shadowVersion = '7.1.2'
  56. }
  57. // 项目依赖
  58. dependencies {
  59. // 子模块依赖
  60. implementation project(':incubator-core')
  61. // 测试依赖
  62. testImplementation "org.junit.jupiter:junit-jupiter-api:${junitVersion}"
  63. testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:${junitVersion}"
  64. }
  65. // 配置测试任务
  66. tasks.test {
  67. useJUnitPlatform()
  68. testLogging {
  69. events 'PASSED', 'FAILED', 'SKIPPED'
  70. }
  71. }
  72. // 将 Shadow JAR 绑定到 assemble 阶段
  73. tasks.assemble.dependsOn(tasks.shadowJar)
  74. // 增加构建信息任务
  75. tasks.register("buildInfo") {
  76. doLast {
  77. println "Building project '${project.name}' version '${project.version}'"
  78. println "Main class: ${application.mainClass.get()}"
  79. println "Shadow JAR: ${tasks.shadowJar.get().archiveFile.get()}"
  80. }
  81. }
  82. // 清理任务优化
  83. tasks.clean {
  84. delete "$buildDir/libs" // 确保清理构建目录
  85. }