From dc4ca245330193423d90bf6af2d74e623aacb912 Mon Sep 17 00:00:00 2001 From: tzdwindows 7 <3076584115@qq.com> Date: Mon, 10 Feb 2025 21:20:56 +0800 Subject: [PATCH] =?UTF-8?q?build(gradle):=20=E9=87=8D=E6=9E=84=E6=9E=84?= =?UTF-8?q?=E5=BB=BA=E9=85=8D=E7=BD=AE=E5=B9=B6=E6=B7=BB=E5=8A=A0=E6=96=B0?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 更新 Shadow 插件版本 - 添加 JDK 版本检查 - 分离依赖项到 libs 目录- 修改 jar 任务以包含 Class-Path 属性 - 更新测试配置 - 添加开源文档处理和打包功能 - 创建可运行分发包 - 设置默认构建任务 --- build.gradle | 73 +++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 67 insertions(+), 6 deletions(-) diff --git a/build.gradle b/build.gradle index 38d38f3..186a3ec 100644 --- a/build.gradle +++ b/build.gradle @@ -1,10 +1,10 @@ plugins { id 'java' id 'application' - id 'com.github.johnrengelman.shadow' version '7.1.2' id 'edu.sc.seis.launch4j' version '2.5.4' } +// JDK 版本检查 def requiredJavaVersion = 20 def currentJavaVersion = JavaVersion.current().majorVersion.toInteger() if (currentJavaVersion != requiredJavaVersion) { @@ -39,19 +39,80 @@ dependencies { implementation 'org.ow2.asm:asm-tree:7.1' } +// 分离依赖项到 libs 目录 +task copyDependencies(type: Copy) { + from configurations.runtimeClasspath + into "$buildDir/libs/libs" +} + +// 执行我生成jar jar { manifest { - attributes 'Main-Class': 'com.axis.innovators.box.Main' + attributes 'Main-Class': 'com.axis.innovators.box.Main', + 'Class-Path': configurations.runtimeClasspath.files.collect { "libs/$it.name" }.join(' ') + } + dependsOn copyDependencies +} + +// 测试配置 +test { + useJUnitPlatform() + systemProperty "java.system.class.loader", "com.axis.innovators.box.plugins.BoxClassLoader" + + // 确保测试能看到依赖 + classpath = files(sourceSets.test.output) + + files("$buildDir/libs/libs") + + configurations.testRuntimeClasspath +} + +// 处理开源文档文件 +sourceSets { + main { + resources { + exclude '**/*.md' + } + } + openSourceDocs { + resources { + srcDir 'src/main/resources' + include '**/*.md' + } } } +// 单独打包文档 +task packageOpenSourceDocs(type: Jar) { + archiveClassifier = 'docs' + from sourceSets.openSourceDocs.resources + destinationDirectory = file("$buildDir/libs/docs") +} + +// 完整的 application 配置 application { mainClass = 'com.axis.innovators.box.Main' + + // 确保运行时参数生效 applicationDefaultJvmArgs = [ - "-Djava.system.class.loader=com.axis.innovators.box.plugins.BoxClassLoader" + "-Djava.system.class.loader=com.axis.innovators.box.plugins.BoxClassLoader", + "-Dloader.library.path=$buildDir/libs/libs" ] } -test { - useJUnitPlatform() -} \ No newline at end of file +// 创建可运行分发 +tasks.register('release') { + dependsOn build, packageOpenSourceDocs + + doLast { + copy { + from "$buildDir/libs" + into "$buildDir/dist" + include '*.jar' + include 'docs/**' + include 'libs/**' + } + println "Release package ready at: $buildDir/dist" + } +} + +// 默认构建任务 +build.dependsOn release \ No newline at end of file