Gradle: common resource dependency for multiple java projects

The approach I took was to use project reference

sourceSets {
    main {
        resources {
            srcDirs += [
                project(':data').sourceSets.main.resources
            ]
        }
    }
}

UPDATE: Tested with Gradle7 and it still works. Tested with java-library plugin.


One solution is to apply the Java plugin also to the data project, and then use regular project dependencies (e.g. dependencies { runtime project(":data") }). However, this would require a bit of effort to prevent shipping the test resources.

Another solution is not to make data a Gradle project but literally include its resource directories in the other two projects (sourceSets.main.resources.srcDir "../data/main/resources"; sourceSets.test.resources.srcDir "../data/test/resources").


You need to choose the project which will hold your resources. All the other projects that require those resources, will add them to the resources component of the sourceSets.

sourceSets {
    data {
        resources {
            srcDir "${project(':data').projectDir}/src/main/resources"
            include "your_pattern_here**"
        }
    }
    main {
        resources {
            srcDirs += [ data.resources ]
        }
    }
}