如何向kotlinCompilerClasspath添加依赖项?

时间:2019-01-23 17:05:02

标签: gradle kotlin

我正在使用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版本

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()
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我建议首先不要为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'
}
相关问题