build.gradle 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. plugins {
  2. id 'java'
  3. id 'application'
  4. id "com.github.johnrengelman.shadow" version "7.1.2"
  5. }
  6. group = 'com.incubator.center'
  7. version = '1.0-SNAPSHOT'
  8. //repositories {
  9. // mavenCentral() // 使用中央仓库
  10. //}
  11. application {
  12. // 使用新的方法设置主类,适应 Gradle 7.0 及以上版本
  13. mainClass.set('com.incubator.center.CenterServerStart')
  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-center')
  24. // 使用项目的版本号作为 JAR 的版本
  25. archiveVersion.set(project.version)
  26. // 移除分类器,生成的 JAR 没有后缀
  27. archiveClassifier.set('')
  28. // 添加自定义清单
  29. manifest {
  30. attributes 'Main-Class': application.mainClass.get() // 使用动态设置的主类
  31. }
  32. // 可选:排除不必要的文件(例如签名文件)
  33. exclude 'META-INF/*.SF', 'META-INF/*.DSA', 'META-INF/*.RSA'
  34. }
  35. // 版本号集中管理
  36. ext {
  37. junitVersion = '5.9.0' // 集中管理 JUnit 版本
  38. }
  39. // 项目依赖
  40. dependencies {
  41. // 子模块依赖
  42. implementation project(path: ':incubator-core')
  43. // 测试依赖
  44. testImplementation "org.junit.jupiter:junit-jupiter-api:${junitVersion}"
  45. testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:${junitVersion}"
  46. }
  47. // 配置测试任务
  48. tasks.test {
  49. useJUnitPlatform()
  50. testLogging {
  51. events 'PASSED', 'FAILED', 'SKIPPED'
  52. }
  53. }
  54. // 将 Shadow JAR 绑定到 assemble 阶段
  55. tasks.assemble.dependsOn(tasks.shadowJar)
  56. // 增加构建信息任务
  57. tasks.register("buildInfo") {
  58. doLast {
  59. println "Building project '${project.name}' version '${project.version}'"
  60. println "Main class: ${application.mainClass.get()}"
  61. println "Shadow JAR: ${tasks.shadowJar.get().archiveFile.get()}"
  62. }
  63. }
  64. // 清理任务优化
  65. tasks.clean {
  66. delete "$buildDir/libs" // 确保清理构建目录
  67. }