加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – Gradle等效的Surefire classpathDependencyExclude

发布时间:2020-12-15 01:02:57 所属栏目:Java 来源:网络整理
导读:我正在尝试将 java项目从maven迁移到gradle.问题是现在测试的类路径依赖配置非常棘手. 我们的maven-surefire-plugin配置: includes include**/SomeTest1.java/include /includes classpathDependencyExcludes classpathDependencyExcludecom.sun.jersey:jer
我正在尝试将 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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读