首页 > 解决方案 > 如何将依赖项添加到 kotlinCompilerClasspath?

问题描述

我正在使用 kotlin-gradle-plugin,在 build.gradle 中配置如下:

buildscript {
    ext {
        springBootVersion = '2.1.2.RELEASE'
        kotlinVersion = '1.3.11'
    }
    repositories {
        mavenLocal()
        mavenCentral()
        maven { url "http://repo.spring.io/plugins-release" }
        maven { url "https://plugins.gradle.org/m2/" }
    }
    dependencies {
        classpath "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlinVersion}"
        classpath "org.jetbrains.kotlin:kotlin-allopen:${kotlinVersion}"
}
apply plugin: 'kotlin'
apply plugin: 'kotlin-spring'

当我运行时,gradle dependencies我得到以下 kotlinCompilerClasspath 以及其他内容:

kotlinCompilerClasspath
\--- org.jetbrains.kotlin:kotlin-compiler-embeddable:1.3.11
     +--- org.jetbrains.kotlin:kotlin-stdlib:1.3.11
     |    +--- org.jetbrains.kotlin:kotlin-stdlib-common:1.3.11
     |    \--- org.jetbrains:annotations:13.0
     +--- org.jetbrains.kotlin:kotlin-script-runtime:1.3.11
     \--- org.jetbrains.kotlin:kotlin-reflect:1.3.11
          \--- org.jetbrains.kotlin:kotlin-stdlib:1.3.11 (*)

当我在 build.gradle 中禁用传递依赖项时,如下所示:

configurations {
    all {
        transitive = false
        resolutionStrategy {
            // fail eagerly on version conflict (includes transitive dependencies)
            // e.g. multiple different versions of the same dependency (group and name are equal)
            failOnVersionConflict()
        }
    }
}

现在,当我运行时,gradle dependencies我得到以下 kotlinCompilerClasspath

kotlinCompilerClasspath
\--- org.jetbrains.kotlin:kotlin-compiler-embeddable:1.3.11

如何在仍然使用 transitive = false 配置的同时向上述 kotlinCompilerClasspath 添加依赖项?

例如,我尝试将依赖项添加org.jetbrains.kotlin:kotlin-stdlib到常规依赖项块和 buildscript 块中,但上面的 kotlinCompilerClasspath 仍然没有改变。

摇篮版本

gradle --version

------------------------------------------------------------
Gradle 5.1.1
------------------------------------------------------------

Build time:   2019-01-10 23:05:02 UTC
Revision:     3c9abb645fb83932c44e8610642393ad62116807

Kotlin DSL:   1.1.1
Kotlin:       1.3.11
Groovy:       2.5.4
Ant:          Apache Ant(TM) version 1.9.13 compiled on July 10 2018
JVM:          1.8.0_191 (Oracle Corporation 25.191-b12)
OS:           Mac OS X 10.14.2 x86_64

更新

按照下面热键的建议,我从配置中过滤掉了kotlin,如下。

configurations {
    matching { !it.name.toLowerCase().contains('kotlin') }.all {
        transitive = false
        resolutionStrategy {
            // fail eagerly on version conflict (includes transitive dependencies)
            // e.g. multiple different versions of the same dependency (group and name are equal)
            failOnVersionConflict()
        }
    }
}

标签: gradlekotlin

解决方案


我建议不要首先禁用kotlinCompilerClasspath配置的传递依赖项,Kotlin Gradle 插件希望该配置也能提取 Kotlin 编译器的传递依赖项:

configurations {
    matching { it.name != 'kotlinCompilerClasspath' }.all {
        transitive = false
        /* ... */
    }
}

如果您仍想为所有配置禁用传递依赖项,您可以手动添加那些报告为kotlin-compiler-embeddable模块传递的依赖项:

dependencies {
    kotlinCompilerClasspath 'org.jetbrains.kotlin:kotlin-stdlib'
    kotlinCompilerClasspath 'org.jetbrains.kotlin:kotlin-script-runtime'
    kotlinCompilerClasspath 'org.jetbrains.kotlin:kotlin-reflect'
    kotlinCompilerClasspath 'org.jetbrains:annotations:13.0'
}

推荐阅读