build.gradle 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. application {
  12. // 使用新的方法设置主类,适应 Gradle 7.0 及以上版本
  13. mainClass.set('com.incubator.game.GameServerStart')
  14. }
  15. // 禁用默认的 JAR 任务,因为我们使用 Shadow JAR
  16. tasks.jar {
  17. enabled = false
  18. }
  19. // 配置 Shadow JAR 任务
  20. tasks.shadowJar {
  21. zip64 = true
  22. // 设置 JAR 包的基本名称
  23. archiveBaseName.set('incubator-game')
  24. // 使用项目的版本号作为 JAR 的版本
  25. archiveVersion.set(project.version)
  26. // 移除分类器,生成的 JAR 没有后缀
  27. archiveClassifier.set('')
  28. // 添加自定义清单
  29. manifest {
  30. attributes(
  31. 'Main-Class': application.mainClass.get() // 使用动态设置的主类
  32. )
  33. }
  34. // 可选:排除不必要的依赖
  35. exclude 'META-INF/*.SF', 'META-INF/*.DSA', 'META-INF/*.RSA'
  36. }
  37. // 依赖版本集中管理
  38. ext {
  39. junitVersion = '5.9.0'
  40. shadowVersion = '7.1.2'
  41. }
  42. // 项目依赖
  43. dependencies {
  44. // 子模块依赖
  45. implementation project(':incubator-core')
  46. // 测试依赖
  47. testImplementation "org.junit.jupiter:junit-jupiter-api:${junitVersion}"
  48. testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:${junitVersion}"
  49. }
  50. // 配置测试任务
  51. tasks.test {
  52. useJUnitPlatform()
  53. testLogging {
  54. events 'PASSED', 'FAILED', 'SKIPPED'
  55. }
  56. }
  57. // 将 Shadow JAR 绑定到 assemble 阶段
  58. tasks.assemble.dependsOn(tasks.shadowJar)
  59. // 增加构建信息任务
  60. tasks.register("buildInfo") {
  61. doLast {
  62. println "Building project '${project.name}' version '${project.version}'"
  63. println "Main class: ${application.mainClass.get()}"
  64. println "Shadow JAR: ${tasks.shadowJar.get().archiveFile.get()}"
  65. }
  66. }
  67. // 清理任务优化
  68. tasks.clean {
  69. delete "$buildDir/libs" // 确保清理构建目录
  70. }