Get Path to Output Artifact of Dependent Module

Project A is a Java application which needs to access the executable JAR which is the output of Project B’s jar task. It needs the JAR itself. This is not a typical multi-module project with an implementation dependency. So Project A has a runtime dependency on Project B.

The following code in Project A works to get the absolute path to the output artifact from Project B, but there must be an easier, more declarative way.

run {
    project(':b').afterEvaluate {
        systemProperty 'jarAbsolutePath', project(':b').tasks.findByName('jar').outputs.files[0].path
    }
}