build.gradle 2.6 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.center'
  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.center.CenterServerStart')
  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-center')
  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' // 集中管理 JUnit 版本
  56. }
  57. // 项目依赖
  58. dependencies {
  59. // 子模块依赖
  60. implementation project(path: ':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. }