我正在尝试使用 kotlin DSL 测试 gradle 5。
我创建了一个库,并将其构建如下:
Hasans-Air:blogiclib h_ajsf$ gradle init --type=kotlin-library
Starting a Gradle Daemon, 1 busy Daemon could not be reused, use --status for details
Select build script DSL:
1: groovy
2: kotlin
Enter selection (default: kotlin) [1..2] 2
Project name (default: blogiclib):
Source package (default: blogiclib):
**BUILD SUCCESSFUL** in 16s
2 actionable tasks: 2 executed
Hasans-Air:blogiclib h_ajsf$ ls
build.gradle.kts gradlew settings.gradle.kts
gradle gradlew.bat src
Hasans-Air:blogiclib h_ajsf$ code .
Hasans-Air:blogiclib h_ajsf$ gradle build
然后我得到了生成的输出文件:
build\libs\blogiclib.jar
Library.kt
生成的文件是:package blogiclib
class Library {
fun someLibraryMethod(): Boolean {
return true
}
}
和
build.gradle.kts
生成的是:plugins {
id("org.jetbrains.kotlin.jvm").version("1.3.10")
}
repositories {
jcenter()
}
dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
testImplementation("org.jetbrains.kotlin:kotlin-test")
testImplementation("org.jetbrains.kotlin:kotlin-test-junit")
}
然后我生成了一个 kotlin 应用程序并进行了如下测试:
Hasans-Air:gradle h_ajsf$ gradle init --type=kotlin-application
Starting a Gradle Daemon (subsequent builds will be faster)
Select build script DSL:
1: groovy
2: kotlin
Enter selection (default: kotlin) [1..2] 2
Project name (default: gradle): blogic
Source package (default: blogic):
**BUILD SUCCESSFUL** in 25s
2 actionable tasks: 2 executed
Hasans-Air:gradle h_ajsf$ ls
build.gradle.kts gradlew settings.gradle.kts
gradle gradlew.bat src
Hasans-Air:gradle h_ajsf$ code .
Hasans-Air:gradle h_ajsf$ gradle run
**BUILD SUCCESSFUL** in 6m 4s
3 actionable tasks: 3 executed
Hasans-Air:gradle h_ajsf$ gradle run
**> Task :run**
Hello world.
然后我添加了之前生成的lib
blogiclib.jar
到文件夹:main\resources
并制作了我的
App.kt
文件为:package blogic
import blogiclib.LibraryKt
class App {
val greeting: String
get() {
return "Hello world."
}
}
fun main(args: Array<String>) {
println("${App().greeting} = someLibraryMethod()")
}
和它的
build.gradle.kts
根据我对 here 的理解作为:plugins {
id("org.jetbrains.kotlin.jvm").version("1.3.10")
application
}
repositories {
jcenter()
}
dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
testImplementation("org.jetbrains.kotlin:kotlin-test")
testImplementation("org.jetbrains.kotlin:kotlin-test-junit")
}
application {
mainClassName = "blogic.AppKt"
}
task<JavaCompile>("compile") {
source = fileTree(file("src/main/resources/blogiclib.jar"))
}
但是在编译时出现以下错误:
Hasans-Air:gradle h_ajsf$ gradle run e: /Users/h_ajsf/Documents/gradle/src/main/kotlin/blogic/App.kt: (6, 8): Unresolved reference: blogiclib **> Task :compileKotlin** FAILED FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':compileKotlin'. > Compilation error. See log for more details * Try: Run with **--stacktrace** option to get the stack trace. Run with **--info** or **--debug** option to get more log output. Run with **--scan** to get full insights. * Get more help at **https://help.gradle.org** Deprecated Gradle features were used in this build, making it incompatible with Gradle 6.0. Use '--warning-mode all' to show the individual deprecation warnings. See https://docs.gradle.org/5.0/userguide/command_line_interface.html#sec:command_line_warnings **BUILD FAILED** in 1s 2 actionable tasks: 2 executed
更新
根据收到的评论,我做了以下工作:
src/main/libs
:build.gradle.kts
:配置{
创建(“外部库”)
}
依赖{
“externalLibs”(文件(“src/main/libs/blogiclib.jar”))
}
代替:
task<JavaCompile>("compile") {
source = fileTree(file("src/main/resources/blogiclib.jar"))
}
但仍然得到同样的错误:(
最佳答案
您可以将所有 jar 文件包含在某些文件夹中,如下所示:
dependencies {
implementation(fileTree("libs/compile"))
compileOnly(fileTree("libs/provided"))
}
或者,选择特定文件:
repositories {
flatDir {
dirs("libs/compile")
dirs("libs/provided")
}
}
dependencies {
implementation(":gson-2.8.5")
compileOnly(":javaee-api-8.0")
}
关于gradle - 如何使用 Gradle Kotlin-DSL 添加本地资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53508161/