【发布时间】:2021-11-16 13:36:20
【问题描述】:
目前,我正在制作一个库并尝试将其导入我们的项目。 该库包含Exposed等库。
下面的代码是库的build.gradle.kts。
plugins {
kotlin("jvm")
}
val exposedVersion: String by project
dependencies {
implementation(kotlin("stdlib"))
implementation(kotlin("stdlib-jdk8"))
implementation(kotlin("test"))
implementation(kotlin("test-junit"))
implementation(kotlin("reflect"))
implementation(kotlin("script-runtime"))
implementation(kotlin("script-util"))
implementation(kotlin("scripting-jsr223"))
implementation("org.slf4j", "slf4j-api", "1.7.30")
implementation("org.jetbrains.exposed:exposed-core:$exposedVersion")
implementation("org.jetbrains.exposed:exposed-dao:$exposedVersion")
implementation("org.jetbrains.exposed:exposed-jdbc:$exposedVersion")
implementation("org.jetbrains.exposed:exposed-java-time:$exposedVersion")
}
但是,当 JAR 被构建并添加到项目中时,它会抛出一个错误(NoClassDefFoundError),它找不到 JAR 引用的类,例如 Exposed。
由于上面的错误,就像下面的代码,我必须将我从库中添加的所有库添加回项目中。
val exposedVersion: String by project
dependencies {
// spring
implementation("org.springframework.boot:spring-boot-starter")
/** other dependencies */
// Dependencies for `my library jar`
implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar"))))
implementation(kotlin("script-runtime"))
implementation(kotlin("scripting-jsr223"))
implementation("org.jetbrains.exposed:exposed-core:$exposedVersion")
implementation("org.jetbrains.exposed:exposed-dao:$exposedVersion")
implementation("org.jetbrains.exposed:exposed-jdbc:$exposedVersion")
implementation("org.jetbrains.exposed:exposed-java-time:$exposedVersion")
}
我想要的build.gradle.kts如下。
val exposedVersion: String by project
dependencies {
// spring
implementation("org.springframework.boot:spring-boot-starter")
/** other dependencies */
// Dependencies for `my library jar`
implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar"))))
}
我怎样才能做我想做的事?我必须做肥罐吗?
【问题讨论】:
-
您如何使用
fileTree“将 jar 添加到项目中”?你为什么不像其他你知道如何使用的那样让它成为一个正确的依赖项? -
看到
Exposed做了什么后,我尝试将库中build.krs的implements全部改成api,但是问题没有解决。有没有其他方法来构建jar?添加为fileTree有问题吗?如果我使用maven存储库是否解决了问题? -
添加为
fileTree是个问题,因为Gradle 不明白你的库需要那些其他的jar。相反,您应该说implementation("my:library:1.2.3")。您可能需要将此库发布到与 Maven 兼容的存储库(OSSRH 或私有存储库)。