我的项目使用多个 Gradle 源集作为其生产代码库,而不仅仅是main
:
domain
dal
rest
test
dbUnitTest
事实证明,这对于限制依赖性和强制关注点分离非常有用。
然而它有一个缺点:我们无法使用以下方式访问类或方法能见度internal https://kotlinlang.org/docs/reference/visibility-modifiers.html从测试类中。原因是 Kotlin 编译器将每个源集放置在其自己的“模块”中:
$ find . -name '*.kotlin_module'
./classes/kotlin/domain/META-INF/contact-management_domain.kotlin_module
./classes/kotlin/dal/META-INF/contact-management_dal.kotlin_module
./classes/kotlin/rest/META-INF/contact-management_dal.kotlin_module
./classes/kotlin/test/META-INF/contact-management.kotlin_module
./classes/kotlin/dbUnitTest/META-INF/contact-management_dbUnitTest.kotlin_module
我希望所有源集都使用相同的模块名称“contact-management”,就像主源集默认情况下一样。
我尝试使用编译器选项覆盖该名称-module-name https://kotlinlang.org/docs/reference/compiler-reference.html:
tasks.withType<KotlinCompile> {
kotlinOptions {
// place all source sets in the same kotlin module, making objects with 'internal' visibility available to every source sets of this project
freeCompilerArgs += listOf("-module-name \"contact-management\")
}
}
运行时gradlew build
, I get
> Task :contact-management:compileDomainKotlin FAILED
e: Invalid argument: -module-name "contact-management"
原因是-module-name "contact-management_domain"
之前也由调用 Kotlin 编译器的 Gradle 代码设置,但显然此选项仅接受一次。
在 Gradle 构建中,我如何控制正在考虑的内容“一个模块” https://kotlinlang.org/docs/reference/visibility-modifiers.html#modules由 Kotlin 编译器实现?
A 相关问题 https://stackoverflow.com/q/57050889/484293哪里的test
要分割的源集目前还没有令人满意的答案。