【问题标题】:Use Kotlin Gradle plugin for tests only仅将 Kotlin Gradle 插件用于测试
【发布时间】:2023-01-31 00:57:33
【问题描述】:

我有一个项目和库,其中包含用 Java 编写的源代码和用 Kotlin 编写的测试。

所有项目均由 Gradle 使用 Kotlin 插件构建。

但是我注意到 Kotlin 被作为传递依赖添加到我的库中,尽管我只在测试中使用 Kotlin。

我怎样才能避免将 Kotlin 添加为传递依赖项?

settings.gradle:

pluginManagement {
    plugins {
        id 'org.jetbrains.kotlin.jvm' version '1.6.10'
    }
}

常见的build.gradle

plugins {
    id 'org.jetbrains.kotlin.jvm'
}

dependencies {
    testImplementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
    testImplementation 'org.jetbrains.kotlin:kotlin-reflect'
    testImplementation 'org.jetbrains.kotlin:kotlin-test'
    testImplementation 'org.jetbrains.kotlin:kotlin-test-junit5'
}

当我构建依赖树时:

------------------------------------------------------------
Project ':commons-logging'
------------------------------------------------------------

testCompileClasspath - Compile classpath for compilation 'test' (target  (jvm)).
+--- org.mycompany:lib-logging:1.0.0
|    \--- org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.6.0 -> 1.3.72
|         +--- org.jetbrains.kotlin:kotlin-stdlib:1.3.72
|         |    +--- org.jetbrains.kotlin:kotlin-stdlib-common:1.3.72
|         |    \--- org.jetbrains:annotations:13.0
|         \--- org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.72
|              \--- org.jetbrains.kotlin:kotlin-stdlib:1.3.72 (*)
+--- org.mycompany:lib-core:3.0.0
|    \--- org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.5.30 -> 1.3.72 (*)
+--- org.mycompany:lib-spring:3.0.0
|    \--- org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.5.30 -> 1.3.72 (*)

因此,org.mycompany 的工件中不应出现所有这些 Kotlin 依赖项

【问题讨论】:

标签: java kotlin gradle kotlin-gradle-plugin


【解决方案1】:

您可以通过在 build.gradle 的依赖项部分添加以下代码来从您的库中排除 Kotlin 传递依赖项:

dependencies {
testImplementation ('org.jetbrains.kotlin:kotlin-stdlib-jdk8') {
exclude group: 'org.jetbrains.kotlin'
}
testImplementation ('org.jetbrains.kotlin:kotlin-reflect') {
exclude group: 'org.jetbrains.kotlin'
}
testImplementation ('org.jetbrains.kotlin:kotlin-test') {
exclude group: 'org.jetbrains.kotlin'
}
testImplementation ('org.jetbrains.kotlin:kotlin-test-junit5') {
exclude group: 'org.jetbrains.kotlin'
}
}

浅见。

【讨论】:

    猜你喜欢
    • 2019-10-24
    • 2015-07-08
    • 2014-03-19
    • 1970-01-01
    • 2019-03-30
    • 2020-06-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多