【发布时间】:2018-07-11 04:02:46
【问题描述】:
我正在使用 Kotlin/JS 开发一个 Web 应用程序,并且我正在尝试使用 Kotlin 库“com.beust:klaxon:3.0.1”(我已经明确编译了 lib 的依赖项并将它们从lib 以减少版本冲突的可能性,在没有显式编译的情况下不成功):
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-js:$kotlin_version"
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
implementation('com.beust:klaxon:3.0.1') {
exclude group: 'org.jetbrains.kotlin'
}
}
Gradle 输出:
19:40:45: Executing task 'build'...
:compileJava NO-SOURCE
e: .../School-Web-Project/frontend/src/requests/Abstraction.kt: (3, 8): Unresolved reference: com
e: .../School-Web-Project/frontend/src/requests/Abstraction.kt: (20, 57): Unresolved reference: Klaxon
:compileKotlin2Js FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':compileKotlin2Js'.
> 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
BUILD FAILED in 2s
1 actionable task: 1 executed
Compilation error. See log for more details
19:40:48: Task execution finished 'build'.
我不知道错误是什么。我已经运行 gradle clean 和 invlidated 缓存并重新启动。
感谢您的宝贵时间。
完整文件:
build.gradle
version '1.0'
buildscript {
ext.kotlin_version = '1.2.50'
ext.web_dir = "${projectDir}/web/"
repositories {
jcenter()
mavenCentral()
maven { url "https://plugins.gradle.org/m2/" }
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "com.gradle:build-scan-plugin:1.14"
}
}
apply plugin: 'kotlin2js'
apply plugin: 'com.gradle.build-scan'
sourceSets {
main.kotlin.srcDirs += "src/"
}
repositories {
mavenCentral()
jcenter()
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-js:$kotlin_version"
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
implementation('com.beust:klaxon:3.0.1') {
exclude group: 'org.jetbrains.kotlin'
}
}
clean.doFirst() {
delete("${web_dir}")
}
build.doLast() {
// Copy kotlin.js and kotlin-meta.js from jar into web directory
configurations.compile.each { File file ->
copy {
includeEmptyDirs = false
from zipTree(file.absolutePath)
into "${web_dir}/lib"
include { fileTreeElement ->
def path = fileTreeElement.path
path.endsWith(".js") && (path.startsWith("META-INF/resources/") || !path.startsWith("META-INF/"))
}
}
}
抽象.kt
package requests
import com.beust.klaxon.Klaxon
import org.w3c.xhr.XMLHttpRequest
object Requestables {
fun requestWeapons(id: Int, amount: Int)
= "backend/request_weapons.php?id=$id&amount=$amount"
}
object RawRequestData {
fun getWeaponsRaw(id: Int, amount: Int, callback: (String) -> Unit, error: (XMLHttpRequest) -> Unit = {}) {
CommonDataRequest.makeAsyncRequest("GET", Requestables.requestWeapons(id, amount), {
callback(responseText)
}, error)
}
}
object WeaponRawProcessing {
fun getWeaponDTOs(rawData: String): WeaponRowDTO = Klaxon().parse(rawData)?: throw NullPointerException("What a Terrible Failure!")
}
object Requests {
fun getWeapons(id: Int, amount: Int, callback: (WeaponRowDTO) -> Unit, error: (XMLHttpRequest) -> Unit = {}) {
RawRequestData.getWeaponsRaw(id, amount, {
callback(WeaponRawProcessing.getWeaponDTOs(it))
}, error)
}
}
// Copy scripts to web directory
copy {
into "${web_dir}"
from ("build/classes/kotlin/main") {
include "*.js"
}
}
}
buildScan {
termsOfServiceUrl = 'https://gradle.com/terms-of-service'
termsOfServiceAgree = 'yes'
}
【问题讨论】:
-
你怎么在
implementation配置下指定依赖?那不应该是compile吗?我没有看到你在哪里应用java-library插件,我认为这是api和implementation配置存在所必需的。 -
@user31601
compileis now deprecated。虽然api会让我使用 klaxon 的库,但我想做的是“强制”更新它们,这样我就可以防止版本控制问题。 -
我相信你提到的关于
compile被弃用的链接答案过于简单化了。如果您通读实际的 Gradle 文档,您会发现这些较新的配置仅在使用java-library插件时适用,而不是单独使用java插件。您应该在此处使用哪一组依赖项将取决于可传递应用的插件kotlin2js- 我不确定它是哪个。 -
> 如果您阅读了实际的 Gradle 文档,您会发现这些较新的配置仅在使用 java-library 插件时适用,而不是单独使用 java 插件。
可以给我一个源链接吗? -
当然,here 是。我之前只是没有提供链接,因为我不知道如何在 cmets 中使用(并且错过了小的“帮助”链接)。