我正在尝试将 Java 项目从 Maven 迁移到 Gradle。现在的问题是测试的类路径依赖配置非常棘手。
我们的 maven-surefire-plugin 配置:
<includes>
<include>**/SomeTest1.java</include>
</includes>
<classpathDependencyExcludes>
<classpathDependencyExclude>com.sun.jersey:jersey-core</classpathDependencyExclude>
</classpathDependencyExcludes>
不同的测试类有不同的类路径。如何使用 Gradle 实现它?
最佳答案
首先,您需要将测试源分成不同的源集。比如说,我们需要从包 org.foo.test.rest 运行测试,运行时类路径与其他测试略有不同。因此,它的执行将转到 otherTest,其中剩余测试位于 test:
sourceSets {
otherTest {
java {
srcDir 'src/test/java'
include 'org/foo/test/rest/**'
}
resources {
srcDir 'src/test/java'
}
}
test {
java {
srcDir 'src/test/java'
exclude 'org/foo/rest/test/**'
}
resources {
srcDir 'src/test/java'
}
}
}
之后,您应该确保 otherTest 已正确设置所有必需的编译和运行时类路径:
otherTestCompile sourceSets.main.output
otherTestCompile configurations.testCompile
otherTestCompile sourceSets.test.output
otherTestRuntime configurations.testRuntime + configurations.testCompile
最后一件事是从测试中排除(或包含)不需要的运行时包:
configurations {
testRuntime {
exclude group: 'org.conflicting.library'
}
}
并为 otherTest 创建测试 Gradle 任务:
task otherTest(type: Test) {
testClassesDir = sourceSets.otherTest.output.classesDir
classpath += sourceSets.otherTest.runtimeClasspath
}
check.dependsOn otherTest
关于java - Gradle 相当于 Surefire classpathDependencyExclude,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32208463/