123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- plugins {
- id 'java'
- id 'application'
- id "com.github.johnrengelman.shadow" version "8.1.1"
- }
- group = 'com.incubator.center'
- version = '1.0-SNAPSHOT'
- sourceSets {
- main {
- resources {
- srcDirs += 'conf'
- }
- }
- }
- application {
- // 使用新的方法设置主类,适应 Gradle 7.0 及以上版本
- mainClass.set('com.incubator.center.CenterServerStart')
- // 添加 JVM 参数,指定 conf 目录下的配置文件路径
- applicationDefaultJvmArgs = [
- "-XX:+UseZGC", // 使用 ZGC 垃圾回收器,适合高并发
- "-XX:+UnlockExperimentalVMOptions", // 启用实验性选项
- "-XX:ZUncommitDelay=300", // 减少内存未使用时的延迟
- "-XX:MaxHeapSize=2G", // 限制最大堆大小
- "-XX:+HeapDumpOnOutOfMemoryError", // OOM 时生成堆转储
- "--enable-preview", // 启用 Java 23 的预览功能
- "-Dlog4j.configurationFile=conf/log4j2.xml"
- ]
- }
- // 禁用默认的 JAR 任务,因为我们使用 Shadow JAR
- tasks.jar {
- enabled = false
- }
- // 配置 Shadow JAR 任务
- tasks.shadowJar {
- zip64 = true
- // 设置 JAR 包的基本名称
- archiveBaseName.set('incubator-center')
- // 使用项目的版本号作为 JAR 的版本
- archiveVersion.set(project.version)
- // 移除分类器,生成的 JAR 没有后缀
- archiveClassifier.set('')
- // 添加自定义清单
- manifest {
- // 使用动态设置的主类
- attributes 'Main-Class': application.mainClass.get()
- }
- // 将 conf 目录包含到最终的 JAR 文件中
- from('conf') { into('conf') }
- // 可选:排除不必要的文件(例如签名文件)
- exclude 'META-INF/*.SF', 'META-INF/*.DSA', 'META-INF/*.RSA'
- // 避免重复打包无关的文件
- duplicatesStrategy = DuplicatesStrategy.EXCLUDE
- }
- // 项目依赖
- dependencies {
- // 子模块依赖
- implementation project(path: ':incubator-core')
- // 测试依赖
- testImplementation "org.junit.jupiter:junit-jupiter-api:5.10.0"
- testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:5.10.0"
- }
- // 配置测试任务
- tasks.test {
- useJUnitPlatform()
- testLogging {
- events 'PASSED', 'FAILED', 'SKIPPED'
- }
- maxParallelForks = Runtime.runtime.availableProcessors() // 并行测试
- }
- // 将 Shadow JAR 绑定到 assemble 阶段
- tasks.assemble.dependsOn(tasks.shadowJar)
- // 增加构建信息任务
- tasks.register("buildInfo") {
- doLast {
- println "Building project '${project.name}'"
- println "Version '${project.version}'"
- println "Main class: ${application.mainClass.get()}"
- println "Shadow JAR: ${tasks.shadowJar.get().archiveFile.get().asFile.absolutePath}"
- }
- }
- // 清理任务优化
- tasks.clean {
- delete layout.buildDirectory.get().asFile // 使用现代 API 清理构建目录
- }
|