Gradle test classes needed in another sub-project

I’m converting a very large maven project to Gradle.
I have 2 sub-projects:




Tests in A:G:X require test classes from A:B:C, how do I include them?
I already have:
compile project(':A:B:C') which I can see in debug output includes the jar but, of course, the jar doesn’t contain test classes.


As explained here, a ProjectDependency is a dependency on the default configuration of a particular project.

For a java project, the default dependency is an extension of the runtime dependency (as stated in here).

In your case, you want to depend on the testRuntime dependency, with something like compile project(':A:B:C', configuration: 'testRuntime')

If this doesn’t suit you, you can always create a custom configuration, and depend on it:

in :A:B:C project:
configurations{ fooConf }
task testJar(type: Jar){ from sourceSets.test.output }
artifacts { fooConf testJar }

in :A:G:X project:
dependencies { compile project(':A:B:C', configuration: 'fooConf')
1 Like

Tried this. Didn’t work. This did. In the A:G:X project:
testCompile project(‘A:B:C’).sourceSets.test.output

Did you rewrite my example to be
dependencies { testCompile project(':A:B:C', configuration: 'fooConf')
when you say it’s not working ?

and small optimization: if you only need those classes at test execution, you can use testRuntime instead of testCompile

Not saying you’re not correct, just saying I couldn’t make it work. Would provide the link to the whole thing on Stack but I just had to delete it because they marked it as a duplicate. The other guy hadn’t even selected an answer. IMHO, those guys lack social skills.

In general, it’s a bit hacky to depend on a specific sourceSet of another subproject, don’t do that. You should define a configuration plus an artifact in :A:B:C and depend on that instead, see @Francois_Guillot’s post above.

I don’t know what the code looked like in 2015, but for anyone coming across this in 2020, the syntax is:

dependencies { testImplementation project(path: ':A:B:C', configuration: 'testRuntime')

As the argument needs to be a Map.