In Gradle, how to declare common dependencies in one place?

Maven has a very useful feature when you can define the dependency in the <dependencyManagement> section of the parent POM and refer to this dependency on the child modules without specifying the version or scope or something else.

What are the alternatives in Gradle?

+100
dependency-management gradle
Mar 03 2018-12-12T00:
source share
7 answers

You can declare common dependencies in the parent script:

 ext.libraries = [ // Groovy map literal spring_core: "org.springframework:spring-core:3.1", junit: "junit:junit:4.10" ] 

From a child script, you can use dependency declarations as follows:

 dependencies { compile libraries.spring_core testCompile libraries.junit } 

To separate dependency declarations with advanced configuration options, you can use DependencyHandler.create :

 libraries = [ spring_core: dependencies.create("org.springframework:spring-core:3.1") { exclude module: "commons-logging" force = true } ] 

Several dependencies can be shared under the same name:

 libraries = [ spring: [ // Groovy list literal "org.springframework:spring-core:3.1", "org.springframework:spring-jdbc:3.1" ] ] 

dependencies { compile libraries.spring } will then add both dependencies at once.

The only information you cannot use in this way is the configuration (scope in terms of Maven) to which the dependency should be assigned. However, in my experience, it’s better to be explicit anyway.

+167
Mar 03 '12 at 16:15
source share

This is a late answer, but you can also see: http://plugins.gradle.org/plugin/io.spring.dependency-management It provides the ability to import maven 'bom' and reuse the definitions defined in 'bom'. This is definitely a good help when you gradually move from maven to gradle! Enjoy it right now.

+7
Oct 31 '14 at 14:11
source share

Starting with Gradle 5.0 (or perhaps earlier?), Dependency restrictions are offered in the documentation as a way to achieve this. From https://docs.gradle.org/current/userguide/declaring_dependencies.html#declaring_a_dependency_without_version :

The recommended practice for larger projects is to declare dependencies without versions and use dependency restrictions to declare versions. The advantage is that dependency restrictions allow you to manage versions of all dependencies, including transitive ones, in one place.

In your parent build.gradle file:

 allprojects { plugins.withType(JavaPlugin).whenPluginAdded { dependencies { constraints { implementation("com.google.guava:guava:27.0.1-jre") } } } } 

Wrapping a dependency block with a Java plug-in check (... whenPluginAdded { ) is not strictly required, but then it will process the addition of a non-Java project to the same assembly.

Then in the gradle child project you can just omit the version:

 apply plugin: "java" dependencies { implementation("com.google.guava:guava") } 

Child assemblies can still choose a higher version. If a lower version is specified, it is automatically updated to the version in the restriction.

+4
Jan 02 '19 at 23:10
source share

io.spring.gradle:dependency-management-plugin plugin has problems with the new Gradle 3.x series, but stable for the 2.x series. See the bug report for support. Drop support for Gradle 3 # 115

In the case of Spring (the main promoter of using the spec), you can complete:

 buildscript { repositories { mavenLocal() jcenter() } dependencies { classpath 'io.spring.gradle:dependency-management-plugin:1.0.0.RELEASE' } } repositories { mavenLocal() jcenter() } apply plugin: 'java' apply plugin: 'io.spring.dependency-management' dependencyManagement { imports { mavenBom 'io.spring.platform:platform-bom:Athens-SR3' } } dependencies { compile 'org.springframework.boot:spring-boot-starter-web' testCompile 'org.springframework.boot:spring-boot-starter-test' } 

Please note that io.spring.platform:platform-bom has org.springframework.boot:spring-boot-starter-parent as the parent, so it is compatible with Spring Boot

You can check the actual resolution of dependencies with:

 $ gradle dependencies $ gradle dependencies --configuration compile $ gradle dependencies -p $SUBPROJ $ gradle buildEnvironment $ gradle buildEnvironment -p $SUBPROJ 

or with the task:

 task showMeCache { configurations.compile.each { println it } } 

Read the official Soring blog post on Better Dependency Management for Gradle to understand the reason for introducing io.spring.gradle:dependency-management-plugin .

+2
Feb 18 '17 at 7:37
source share

This blog post suggests managing dependencies and groups as configurations: https://www.javacodegeeks.com/2016/05/manage-dependencies-gradle-multi-project-build.html

I have not tried it myself, but it looks interesting.

Root build.gradle project

 subprojects { configurations { commonsIo } dependencies { commonsIo 'commons-io:commons-io:2.5' } } 

Subproject build.gradle

 configurations { compile.extendsFrom commonsIo } 
+1
Dec 01 '17 at 0:58
source share

You can centralize the dependency using the code below:

In gradle.properties

 COMPILE_SDK_VERSION=26 BUILD_TOOLS_VERSION=26.0.1 TARGET_SDK_VERSION=26 MIN_SDK_VERSION=14 ANDROID_SUPPORT_VERSION=26.0.2 

In each module add build.gradle :

 android { compileSdkVersion COMPILE_SDK_VERSION as int buildToolsVersion BUILD_TOOLS_VERSION as String defaultConfig { minSdkVersion MIN_SDK_VERSION as int targetSdkVersion TARGET_SDK_VERSION as int versionCode 1 versionName "1.0" } } dependencies { compile "com.android.support:appcompat-v7:${ANDROID_SUPPORT_VERSION}" compile "com.android.support:support-v4:${ANDROID_SUPPORT_VERSION}" compile "com.android.support:support-annotations:${ANDROID_SUPPORT_VERSION}" compile "com.android.support:support-vector-drawable:${ANDROID_SUPPORT_VERSION}" compile "com.android.support:design:${ANDROID_SUPPORT_VERSION}" } 
0
Oct 02 '17 at 7:32
source share

To save your clean file, we can group the dependencies into an array and implement them later.

  1. Add the version of such libraries to build.gradle (application level) outside the dependency block :

// declare library versions

 final RetrofitVersion = '2.3.0' final OkHttpVersion = '3.9.1' 
  1. Create an array of related dependencies so you can easily find it later. Add it to build.gradle (application level) outside the dependency block :

// Use the version in the library and add the dependency along with the access name (for example, modification (first))

 final networkDependencies = [ retrofit : "com.squareup.retrofit2:retrofit:${RetrofitVersion}", retrofitGsonConverter: "com.squareup.retrofit2:converter-gson:${RetrofitVersion}", retrofitRxJavaAdapter: "com.squareup.retrofit2:adapter-rxjava2:${RetrofitVersion}", okHttp3 : "com.squareup.okhttp3:okhttp:${OkHttpVersion}", okHttp3Logging : "com.squareup.okhttp3:logging-interceptor:${OkHttpVersion}" ] 
  1. And in the dependency block :

// Implement all array dependency

 dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation networkDependencies.values() } 



Thus, the final code will look like this:

 final RetrofitVersion = '2.3.0' final OkHttpVersion = '3.9.1' final networkDependencies = [ retrofit : "com.squareup.retrofit2:retrofit:${RetrofitVersion}", retrofitGsonConverter: "com.squareup.retrofit2:converter-gson:${RetrofitVersion}", retrofitRxJavaAdapter: "com.squareup.retrofit2:adapter-rxjava2:${RetrofitVersion}", okHttp3 : "com.squareup.okhttp3:okhttp:${OkHttpVersion}", okHttp3Logging : "com.squareup.okhttp3:logging-interceptor:${OkHttpVersion}" ] dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation networkDependencies.values() } 
0
Jun 24 '19 at 5:35
source share