I\'m developing a multi-module project with gradle/intellij-idea, and here is the structure of my project home:
project/
sub-project-1
/main/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 ]
}
}
}
Ok, I figured it out. It's actually pretty simple. Just treat the "data" folder as another project and add dependency declarations to sub projects will do the trick. For example:
dependencies {
compile project (':data')
testCompile project (':data')
}
And here is version for Kotlin DSL - to sync all resources from :data module to root and all sub-modules build/resources folders:
// Synchronizing resources from :data module to project root: build/resources
synchronizeSharedResources()
subprojects {
// Synchronizing resources from :data module to all submodules: build/resources
synchronizeSharedResources()
}
fun Project.synchronizeSharedResources() {
sourceSets {
main {
resources.srcDir(project(":data").sourceSets["main"].resources.srcDirs)
}
test {
resources.srcDir(project(":data").sourceSets["test"].resources.srcDirs)
}
}
}
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"
).
The approach I took was to use project reference
sourceSets {
main {
resources {
srcDirs += [
project(':data').sourceSets.main.resources
]
}
}
}
sourceSets {
main {
resources {
srcDirs += [
'../sub-project-1/src/main/resources',
'../sub-project-2/src/main/resources',
'../data/src/main/resources'
]
}
}
test {
resources {
srcDir '../data/src/test/resources'
}
}
}