build.gradle - Gradle 获得依赖但不在构建中使用它

标签 build.gradle kotlin-js

我正在 Kotlin/JS 中开发一个 Web 应用程序,我正在尝试使用 Kotlin 库“com.beust:klaxon:3.0.1”(我已经明确编译了库的依赖项并将它们从库中删除以减少版本冲突的概率,在没有显式编译的情况下不成功):

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'
    }
}

但我不能使用它:
failure to build in IntelliJ

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 缓存并重新启动。

感谢您的时间。

完整文件:

构建.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'
}

最佳答案

我相信 com.beust:klaxon旨在与 kotlin-jvm 一起使用.对于 kotlin-js你应该使用专门为 js 构建的库,比如 kotlin-stdlib-js .

关于build.gradle - Gradle 获得依赖但不在构建中使用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51127527/

相关文章:

缺少 Android Studio 0.5.3 android.compileSdkVersion

Gradle依赖 json-simple 错误

javascript - 在 Kotlin JS 中实例化期望 "new"关键字的 Javascript 类

java - 如何使用 Spring Boot Gradle Plugin 2.1.* 构建不可执行的 jar(库)

java - 如何在Gradle useTestNG中使用testng参数

android - Gradle 和本地 AAR 依赖项之间的 APK 大小是否存在差异?

kotlin - 在 Kotlin React 应用程序中导入外部 css

gradle - 在类型为org.gradle.api.internal.tasks.DefaultSourceSetContainer的SourceSet容器上找不到参数的方法srcDirs()

kotlin - 在 kotlin js 中加载资源文件(json)