Gradle Parent Build File | Common Dependency Example
September 21, 2014
In this page we will see how to use parent build file or any common gradle file that can be used in more than one gradle project. Create a gradle build file and define the dependency and then it can be included in any gradle project using apply from referring relative path or any http URL.
To understand the concept, find the project and gradle build file structure.
common.gradle | | MainProject | | -- build.gradle
common.gradle
dependencies { compile 'org.springframework:spring-jdbc:4.1.0.RELEASE' } ext.libs = [ spring_aop: 'org.springframework:spring-aop:4.1.0.RELEASE', junit: 'junit:junit:4.10', spring_core: dependencies.create('org.springframework:spring-core:4.1.0.RELEASE') { exclude module: "commons-logging" force = true }, hibernate: [ 'org.hibernate:hibernate-annotations:3.5.6-Final', 'org.hibernate:hibernate-core:4.3.6.Final' ] ]
1. Using relative path:
apply from: '../common.gradle'
2. Using URL:
apply from: 'http://anydomain.com/common.gradle'
Now find the sample build file which are using common.gradle.
build.gradle
apply plugin: 'java' apply plugin: 'eclipse' apply from: '../common.gradle' repositories { mavenCentral() } dependencies { compile libs.hibernate compile libs.spring_core compile libs.junit }
dependencies { compile 'org.springframework:spring-jdbc:4.1.0.RELEASE' }
ext.libs = [ spring_aop: 'org.springframework:spring-aop:4.1.0.RELEASE', junit: 'junit:junit:4.10' ]
dependencies { compile libs.spring_aop compile libs.junit }
ext.libs = [ spring_core: dependencies.create('org.springframework:spring-core:4.1.0.RELEASE') { exclude module: "commons-logging" force = true } ]
ext.libs = [ hibernate: [ 'org.hibernate:hibernate-annotations:3.5.6-Final', 'org.hibernate:hibernate-core:4.3.6.Final' ] ]