When defining dependencies in Gradle, you’ll end up with a list in your build.gradle
file of Maven Group-Artifact-Version items that you want to import, together with when you want access to them (compile, runtime etc). Here is an example, taken from the docs:
1// within build.gradle2dependencies {3 implementation 'org.hibernate:hibernate-core:3.6.7.Final'4 api 'com.google.guava:guava:23.0'5 testImplementation 'junit:junit:4.+'6}
Now imagine that you have a multi-project build, perhaps as described in the previous post here. Each project has its own dependencies, many of which are common. How is best to deal with this?
If you’re using the Gradle Wrapper (and you should be), then your wrapper jar will be in a directory off the project root directory called gradle. This is one place where it might make sense to establish a convention for your project about where dependencies can be defined. Let’s create a file containing definitions of our dependencies as per the example above:
1// gradle/libraries.gradle2ext {3 hibernateCoreVersion = "3.6.7.Final"4 guavaVersion = "23.0"5 junitVersion = "4.+"67 libs = [8 hibernate-core: "org.hibernate:hibernate-core:$hibernateCoreVersion",9 guava: "com.google.guava:guava:$guavaVersion",10 junit: "junit:junit:$junitVersion"11 ]12}
Now, to enable each sub-project / module to be able to reference these, you’ll need to apply the gradle file to each project. This is done very simply by adding to the top level build.gradle
file the following:
1// root project build.gradle2subprojects {3 apply from "$rootDir/gradle/libraries.gradle"4}
Now, within each module’s build.gradle
(or mymodule.gradle
if your module is called mymodule
and you renamed your module build files for clarity as I showed you here), you can do the following:
1// module's build.gradle2dependencies {3 implementation libs.hibernate.core4 api libs.guava5 testImplementation libs.junit6}
The result is that all your dependencies are defined in one location but each module is free to include only the modules that are needed by that module.