Gradle Kotlin DSL: get sourceSet of another project
当前,我们正在尝试将现有的build.gradle脚本迁移到新的Kotlin DSL。现在,我们在jar任务配置中苦苦挣扎。
我们的项目是一个简单的多项目。假设我们Core和Plugin和Plugin使用Core中的类。现在,在构建Plugin时,目标jar应包含Core中使用的所有类。
这是以前的样子:
1 2 3 4
| jar {
from sourceSets.main.output
from project(':Core').sourceSets.main.output
} |
这是我们使用Kotlin DSL的当前解决方案:
1 2 3 4 5
| val jar: Jar by tasks
jar.apply {
from(java.sourceSets["main"].allSource)
from(project(":Core").the<SourceSetContainer>()["main"].allSource)
} |
但是,以上示例仅给我一个Extension of type 'SourceSetContainer' does not exist. Currently registered extension types: [ExtraPropertiesExtension]错误。我还尝试了我发现的其他代码段,但是到目前为止,它们都没有起作用。
我也尝试过此方法(如第一个答案中建议的那样):
1 2 3 4 5
| val jar: Jar by tasks
jar.apply {
from(java.sourceSets["main"].allSource)
from(project(":Core").sourceSets.getByName("main").allSource)
} |
但是随后IDE(还有jar任务)认为sourceSets不可用:Unresolved reference. None of the following candidates is applicable because of receiver type mismatch: public val KotlinJvmProjectExtension.sourceSets: NamedDomainObjectContainer defined in org.gradle.kotlin.dsl。
我希望有人能为我们提供帮助,因为花费大量时间进行配置而不是编写任何有用的代码非常令人沮丧。
提前非常感谢您。
您可以通过以下方式访问SourceSetContainer
1
| project(":Core").extensions.getByType(SourceSetContainer::class) |
似乎 Project.the(extensionType: KClass< T >): T在项目的convention中查找,而val Project.sourceSets: SourceSetContainer get()在extensions ExtensionContaier中查找。 这有点奇怪,因为the的文档说"返回指定类型的插件约定或扩展名"。
请注意,您可能需要在gradle.projectsEvaluated中进行sourceSet操作,因为否则,如果尚未评估相应的项目,则可能尚未配置有问题的sourceSet。
如果您可以访问该项目,则所有内容都应类似于您的实际groovy gradle脚本:
1
| project(":Core").sourceSets.getByName("main").allSource |
因此,关于您的实际代码:
1 2 3 4 5
| val jar: Jar by tasks
jar.apply {
from(java.sourceSets["main"].allSource)
from(project(":Core").sourceSets.getByName("main").allSource)
} |
-
谢谢。 香港专业教育学院也尝试过,但是这也不起作用:Unresolved reference. None of the following candidates is applicable because of receiver type mismatch: public val KotlinJvmProjectExtension.sourceSets: NamedDomainObjectContainer defined in org.gradle.kotlin.dsl