gradle - 如何在 gradle 中创建自定义任务以将 java 和 kotlin 代码打包到 jar 中?
问题描述
我们有一个多模块设置,我们在模块之间共享一些测试类(主要是 Fakes 实现)。我们当前的解决方案(您可以在下面找到)仅适用于用 Java 编写的类,但我们正在考虑支持共享的 kotlin 类。
if (isAndroidLibrary()) {
task compileTestCommonJar(type: JavaCompile) {
classpath = compileDebugUnitTestJavaWithJavac.classpath
source sourceSets.testShared.java.srcDirs
destinationDir = file('build/testCommon')
}
taskToDependOn = compileDebugUnitTestSources
} else {
task compileTestCommonJar(type: JavaCompile) {
classpath = compileTestJava.classpath
source sourceSets.testShared.java.srcDirs
destinationDir = file('build/testCommon')
}
taskToDependOn = testClasses
}
task testJar(type: Jar, dependsOn: taskToDependOn) {
classifier = 'tests'
from compileTestCommonJar.outputs
}
如何修改compileTestCommonJar
它以支持 kotlin?
解决方案
这是我们所做的:
- 在具有共享测试类的模块中,将
test
源集输出打包到 jar 中
configurations { tests }
...
task testJar(type: Jar, dependsOn: testClasses) {
baseName = "test-${project.archivesBaseName}"
from sourceSets.test.output
}
artifacts { tests testJar }
- 在依赖于共享类的模块中
dependencies {
testCompile project(path: ":my-project-with-shared-test-classes", configuration: "tests")
}
PS:老实说,我更希望有一个单独的 Gradle 模块和通用测试类,因为它是更明确的解决方案。
推荐阅读
- hibernate - Hibernate 不会从序列中获取 FK 键的默认值
- python - 得到 [
] 而不是数值 - excel - vba 使用单元格命令计算平均值
- javascript - 展平对象数组
- r - 提高特定代码效率 - *base R* 替代 for() 循环解决方案
- r - 如何通过 R 中的选定列值转置我的表?
- sql-server - SSRS - 可以隐藏分组行中的各个列而不隐藏整行吗?
- bartender - Bartender 2016 R9 - 打印通行证时触发的 VBScript(不是打印预览)
- r - SF:我怎样才能得到多边形内的所有点?
- java - Java中的继承和泛型