Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle: common resource dependency for multiple java projects

Tags:

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   sub-project-2     /main/resources   data     /main/resources     /test/resources 

As you can see I have multiple sub-projects (all java), how can I make them depend on some common resources (the "data" project, which contains no code but only resources), as well as their own separate resources?

Also it's best that the intellij-idea can pick up these dependencies with JetGradle automatically (JetGradle do just fine picking up the default gradle java project dependencies within each sub project).

Thanks a lot!

like image 440
Eric Wu Avatar asked Nov 22 '13 03:11

Eric Wu


People also ask

What is subproject in Gradle?

subprojects { sourceCompatibility = 11 } It adds the sourceCompatibility property to all sub-projects. This property is then used by the Gradle build Java Plugin: Java version compatibility to use when compiling Java source. Default value: version of the current JVM in use JavaVersion.

How do I specify Java 11 in Gradle?

Right click on the deploy or any other task and select "Open Gradle Run Configuration..." Then navigate to "Java Home" and paste your desired java path.


2 Answers

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.

like image 124
Tono Wiedermann Avatar answered Oct 03 '22 00:10

Tono Wiedermann


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").

like image 29
Peter Niederwieser Avatar answered Oct 03 '22 01:10

Peter Niederwieser