java source如何添加_如何向Gradle添加新的源集?

Gradle 4+:集成测试与类路径配置升级指南
本文介绍如何在Gradle 4及以上版本中更新集成测试配置,包括创建新的sourceSet、合并test依赖并自定义测试任务,以确保最佳实践和最小影响。

这是2016年曾为Gradle 2.x / 3.x编写的,已经过时了!!请查看Gradle 4及更高版本中记录的解决方案

总结两个旧答案(获得两个世界的最佳和最低可行性):

先说一些温暖的话:

首先,我们需要定义 sourceSet :

sourceSets {

integrationTest

}

接下来我们从 test 展开 sourceSet ,因此我们使用 test.runtimeClasspath (其中包括来自 test AND test 本身的所有依赖项)作为派生 sourceSet 的类路径:

sourceSets {

integrationTest {

compileClasspath += sourceSets.test.runtimeClasspath

runtimeClasspath += sourceSets.test.runtimeClasspath // ***)

}

}

note )不知何故需要重新声明/扩展 sourceSets.integrationTest.runtimeClasspath ,但应该是无关紧要的,因为 runtimeClasspath 总是扩展 output + runtimeSourceSet ,不要得到它

我们定义了一个专门用于运行集成测试的任务:

task integrationTest(type: Test) {

}

配置 integrationTest 测试类和类路径使用 . java 插件的默认值使用 test sourceSet

task integrationTest(type: Test) {

testClassesDir = sourceSets.integrationTest.output.classesDir

classpath = sourceSets.integrationTest.runtimeClasspath

}

(可选)测试后自动运行

integrationTest.dependsOn test

(可选)从 check 添加依赖项(因此它始终在执行 build 或 check 时运行)

tasks.check.dependsOn(tasks.integrationTest)

(可选)将java,资源添加到 sourceSet 以支持自动检测并在IDE中创建这些"partials" . 即IntelliJ IDEA将自动为每个集创建 sourceSet 目录java和资源(如果它不存在):

sourceSets {

integrationTest {

java

resources

}

}

tl;dr

apply plugin: 'java'

// apply the runtimeClasspath from "test" sourceSet to the new one

// to include any needed assets: test, main, test-dependencies and main-dependencies

sourceSets {

integrationTest {

// not necessary but nice for IDEa's

java

resources

compileClasspath += sourceSets.test.runtimeClasspath

// somehow this redeclaration is needed, but should be irrelevant

// since runtimeClasspath always expands compileClasspath

runtimeClasspath += sourceSets.test.runtimeClasspath

}

}

// define custom test task for running integration tests

task integrationTest(type: Test) {

testClassesDir = sourceSets.integrationTest.output.classesDir

classpath = sourceSets.integrationTest.runtimeClasspath

}

tasks.integrationTest.dependsOn(tasks.test)

指的是:

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值