build.gradle 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. "-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-center')
  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' // 集中管理 JUnit 版本
  57. }
  58. // 项目依赖
  59. dependencies {
  60. // 子模块依赖
  61. implementation project(path: ':incubator-core')
  62. // 测试依赖
  63. testImplementation "org.junit.jupiter:junit-jupiter-api:${junitVersion}"
  64. testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:${junitVersion}"
  65. }
  66. // 配置测试任务
  67. tasks.test {
  68. useJUnitPlatform()
  69. testLogging {
  70. events 'PASSED', 'FAILED', 'SKIPPED'
  71. }
  72. }
  73. // 将 Shadow JAR 绑定到 assemble 阶段
  74. tasks.assemble.dependsOn(tasks.shadowJar)
  75. // 增加构建信息任务
  76. tasks.register("buildInfo") {
  77. doLast {
  78. println "Building project '${project.name}' version '${project.version}'"
  79. println "Main class: ${application.mainClass.get()}"
  80. println "Shadow JAR: ${tasks.shadowJar.get().archiveFile.get()}"
  81. }
  82. }
  83. // 清理任务优化
  84. tasks.clean {
  85. delete "$buildDir/libs" // 确保清理构建目录
  86. }