Java – gradle equivalent surefire classpathdependencyexclude
I'm trying to migrate Java projects from Maven to gradle The problem is that the classpath dependency configuration tested now is very tricky
Our Maven surefire plugin configuration:
<includes> <include>**/SomeTest1.java</include> </includes> <classpathDependencyExcludes> <classpathDependencyExclude>com.sun.jersey:jersey-core</classpathDependencyExclude> </classpathDependencyExcludes>
Different test classes have different Classpaths How do I implement it using gradle?
Solution
First, you need to distinguish test sources from different sourcesets For example, we need to run the package org foo. test. The runtime classpath of tests in rest is different from other tests Therefore, its execution will go to othertest, where the test is still a 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' } } }
After that, you should ensure that othertest correctly sets all the necessary compile and run-time Classpaths:
otherTestCompile sourceSets.main.output otherTestCompile configurations.testCompile otherTestCompile sourceSets.test.output otherTestRuntime configurations.testRuntime + configurations.testCompile
The last thing is to exclude (or include) unnecessary runtime packages from test:
configurations { testRuntime { exclude group: 'org.conflicting.library' } }
And create a test gradle task for othertest:
task otherTest(type: Test) { testClassesDir = sourceSets.otherTest.output.classesDir classpath += sourceSets.otherTest.runtimeClasspath } check.dependsOn otherTest