plugins { id 'java' id 'application' id "com.github.johnrengelman.shadow" version "7.1.2" } group = 'com.incubator.game' version = '1.0-SNAPSHOT' sourceSets { main { resources { srcDirs += 'conf' } } } application { // 使用新的方法设置主类,适应 Gradle 7.0 及以上版本 mainClass.set('com.incubator.game.GameServerStart') // 添加 JVM 参数,指定 conf 目录下的配置文件路径 applicationDefaultJvmArgs = [ "-XX:+UseG1GC", // 使用 G1 GC "-XX:MaxGCPauseMillis=200", // 控制 GC 停顿时间 "-XX:+HeapDumpOnOutOfMemoryError", // 发生 OOM 时生成堆转储 "-Dlog4j.configurationFile=conf/log4j2.xml" ] } // 禁用默认的 JAR 任务,因为我们使用 Shadow JAR tasks.jar { enabled = false } // 配置 Shadow JAR 任务 tasks.shadowJar { zip64 = true // 设置 JAR 包的基本名称 archiveBaseName.set('incubator-game') // 使用项目的版本号作为 JAR 的版本 archiveVersion.set(project.version) // 移除分类器,生成的 JAR 没有后缀 archiveClassifier.set('') // 添加自定义清单 manifest { // 使用动态设置的主类 attributes 'Main-Class': application.mainClass.get() } // 将 conf 目录包含到最终的 JAR 文件中 from('conf') { into('conf') // 指定 JAR 内的路径 } // 可选:排除不必要的依赖 exclude 'META-INF/*.SF', 'META-INF/*.DSA', 'META-INF/*.RSA' // 避免重复打包无关的文件 duplicatesStrategy = DuplicatesStrategy.EXCLUDE } // 依赖版本集中管理 ext { junitVersion = '5.9.0' shadowVersion = '7.1.2' } // 项目依赖 dependencies { // 子模块依赖 implementation project(':incubator-core') // 测试依赖 testImplementation "org.junit.jupiter:junit-jupiter-api:${junitVersion}" testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:${junitVersion}" } // 配置测试任务 tasks.test { useJUnitPlatform() testLogging { events 'PASSED', 'FAILED', 'SKIPPED' } } // 将 Shadow JAR 绑定到 assemble 阶段 tasks.assemble.dependsOn(tasks.shadowJar) // 增加构建信息任务 tasks.register("buildInfo") { doLast { println "Building project '${project.name}' version '${project.version}'" println "Main class: ${application.mainClass.get()}" println "Shadow JAR: ${tasks.shadowJar.get().archiveFile.get()}" } } // 清理任务优化 tasks.clean { delete "$buildDir/libs" // 确保清理构建目录 }