我有一个具有以下结构的 Compose 桌面应用程序:
project
/src
/main
/kotlin
*.kt files; lot's of them
/resources
/sql
query1.sql
query2.sql
在项目内部,我使用 random 配置来读取资源:
val query = MainConfig::class.java.getResource("/sql/create_schema.sql").readText()
这在开发系统上运行良好,并且应用程序能够读取查询。但是,当我运行任何 Gradle 任务(如
createReleaseDistributable
或 packageDistributionForCurrentOS
)并将其复制到目标系统或安装时,应用程序会崩溃,因为查询丢失。
我的
build.gradle.kts
看起来像这样:
import org.jetbrains.compose.desktop.application.dsl.TargetFormat
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
plugins {
kotlin("jvm") version "1.9.22"
id("org.jetbrains.compose") version "1.5.12"
}
repositories {
mavenCentral()
maven("https://maven.pkg.jetbrains.space/public/p/compose/dev")
google()
}
dependencies {
// ...
}
compose.desktop {
application {
mainClass = "MainKt"
javaHome = System.getenv("JDK_17")
jvmArgs += listOf("-Xmx2G")
nativeDistributions {
targetFormats(TargetFormat.Exe)
packageName = "project"
packageVersion = "1.0.1"
includeAllModules = true
windows {
perUserInstall = true
}
}
buildTypes.release.proguard {
isEnabled = false
}
}
}
tasks.wrapper {
// https://gradle.org/releases/
gradleVersion = "8.6"
// You can either download the binary-only version of Gradle (BIN) or
// the full version (with sources and documentation) of Gradle (ALL)
distributionType = Wrapper.DistributionType.ALL
}
kotlin {
target {
compilations.all {
compilerOptions.configure {
jvmTarget.set(JvmTarget.JVM_17)
}
}
}
}
是否需要添加一些东西,以便应用程序在部署时可以使用资源?它显然知道通过 IntelliJ 运行时要做什么,但在部署时却不知道。为什么会这样?
另外,我是否应该在某个文件夹中找到这些文件,或者将它们编译成另一个文件,例如
*.jar
或 java 特定的文件?如果是,哪个 *.jar
应该包含它们?
我还做了什么,打印默认资源路径:
sourceSets {
main{
resources {
srcDirs.forEach {
println(it)
}
}
}
}
这正确显示了
resources
文件夹的位置,但发布版本不起作用。