How to get file path to binary JAR produced by another subproject?

Quick question: How to get file path to binary JAR with classes produced by another subproject?

Longer version: Multi-project build. Subproject ‘App’ depends in subproject ‘Shared’. In my plugin I need get access to compiled classes from ‘Shared’ used in ‘App’ module. Initially I just used:

project(':shared').sourceSets.main.output.classesDir.flatten()

but I spotted that Gradle creates a JAR file and uses it as a dependency. So I need to take that JAR use a reference to it (e.g. as ‘File’) to do processing. How can I do that?

A project dependency is effectively a dependency on a file (or collection of files). If the project you depend on is a Java project, then in that case you are already describing a dependency to the JAR. In order to isolate the artifact dependency it’s often convenient to create a ‘Configuration’ for that one dependency.

configurations {

foo {

transitive false

}

}

dependencies {

foo project(’:shared’)

}

Then you can access the JAR through the configuration.

configurations.foo.singleFile

I hoped for something simpler, but in the end that solves my problem. Thanks Mark.

Accidentally I have found one more solution which works:

project(':shared').jar.outputs.files.getFiles()

It is less flexible, but in many cases it should give the same result. WDYT?

1 Like

In general you should use project dependencies to convey dependencies across projects. Dependencies to individual project tasks is typically more brittle and doesn’t necessarily properly describe the “output” of that project.