Gradle Kotlin DSL:获取另一个项目的 sourceSet

K. *_* D. 5 gradle kotlin gradle-kotlin-dsl

目前,我们正在尝试将现有build.gradle脚本迁移到新的 Kotlin DSL。现在,我们正在为jar任务配置而苦苦挣扎。

我们的项目是一个简单的多项目。假设我们已经CorePluginPlugin使用了来自Core. 现在,在构建 时Plugin,目标 jar 应包含来自Core.

这是以前的样子:

jar {
    from sourceSets.main.output
    from project(':Core').sourceSets.main.output
}
Run Code Online (Sandbox Code Playgroud)

这是我们使用 Kotlin DSL 的当前解决方案:

val jar: Jar by tasks
jar.apply {
    from(java.sourceSets["main"].allSource)
    from(project(":Core").the<SourceSetContainer>()["main"].allSource)
}
Run Code Online (Sandbox Code Playgroud)

然而,上面的例子只是给了我一个Extension of type 'SourceSetContainer' does not exist. Currently registered extension types: [ExtraPropertiesExtension]错误。我还尝试了我发现的其他代码片段,但到目前为止它们都没有工作。

我也试过这个(就像在第一个答案中建议的那样):

val jar: Jar by tasks
jar.apply {
    from(java.sourceSets["main"].allSource)
    from(project(":Core").sourceSets.getByName("main").allSource)
}
Run Code Online (Sandbox Code Playgroud)

但是随后 IDE(以及jar任务)认为这sourceSets是不可用的:Unresolved reference. None of the following candidates is applicable because of receiver type mismatch: public val KotlinJvmProjectExtension.sourceSets: NamedDomainObjectContainer<DefaultKotlinSourceSet> defined in org.gradle.kotlin.dsl.

我希望有人可以帮助我们,因为花费数小时进行配置而不是编写任何有用的代码是非常令人沮丧的。

非常感谢您提前。

dwu*_*sen 0

如果您可以访问该项目,那么一切都应该看起来像您实际的 groovy gradle 脚本:

 project(":Core").sourceSets.getByName("main").allSource
Run Code Online (Sandbox Code Playgroud)

所以关于你的实际代码:

 val jar: Jar by tasks
 jar.apply {
     from(java.sourceSets["main"].allSource)
     from(project(":Core").sourceSets.getByName("main").allSource)
 }
Run Code Online (Sandbox Code Playgroud)