java - Gradle 相当于 Surefire classpathDependencyExclude

标签 java maven gradle build maven-surefire-plugin

我正在尝试将 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/

相关文章:

java - 在 Ant jar 任务期间创建目录结构

java - 按钮取消 JFileChooser 单击时仍加载文件

Java:通配符类型不匹配导致编译错误

java - 如何设置maven 3本地插件仓库

java - 在java中绘制带有轮廓的文本

java - 在 eclipse 和 maven 中使用 slf4j 配置 WebApps

scala - 在 IntelliJ Scala 插件中,我可以使用通过 `pom.xml` 中的 <dependency> 检索到的 scala-compiler.jar 吗?

gradle - 无法打开脚本的no_buildscript类缓存

scala - 为Gradle项目添加库根依赖

java - 抽屉布局类未找到错误