java – Gradle等效的Surefire classpathDependencyExclude

我正在尝试将 java项目从maven迁移到gradle.问题是现在测试的类路径依赖配置非常棘手.

我们的maven-surefire-plugin配置:

<includes>
     <include>**/SomeTest1.java</include>
 </includes>
 <classpathDependencyExcludes>
     <classpathDependencyExclude>com.sun.jersey:jersey-core</classpathDependencyExclude>
 </classpathDependencyExcludes>

不同的测试类有不同的classpathes.如何使用Gradle实现它?

解决方法

首先,您需要将测试源与不同的sourceSets区分开来.比如,我们需要运行包org.foo.test.rest中的测试,其运行时类路径与其他测试不同.因此,它的执行将转到otherTest,其中测试仍然是测试:
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

最后一件事是从test中排除(或包含)不需要的运行时包:

configurations {
    testRuntime {
        exclude group: 'org.conflicting.library'
    }
}

并为otherTest创建Test Gradle任务:

task otherTest(type: Test) {
    testClassesDir = sourceSets.otherTest.output.classesDir
    classpath += sourceSets.otherTest.runtimeClasspath
}

check.dependsOn otherTest

相关文章

最近看了一下学习资料,感觉进制转换其实还是挺有意思的,尤...
/*HashSet 基本操作 * --set:元素是无序的,存入和取出顺序不...
/*list 基本操作 * * List a=new List(); * 增 * a.add(inde...
/* * 内部类 * */ 1 class OutClass{ 2 //定义外部类的成员变...
集合的操作Iterator、Collection、Set和HashSet关系Iterator...
接口中常量的修饰关键字:public,static,final(常量)函数...