Gradle Include Dependency on another Project and Execution Order

By Arvind Rai, September 20, 2014
In this page we will learn how to refer gradle dependency of another project. We will also see that if there are more than one project, then how will be their execution order. Gradle provide keywords like include and project, which helps to achieve the dependency of another project. To understand these concept, we have taken three project in our example. These are ProjectOne, ProjectTwo and MainProeject. ProjectOne and ProjectTwo are being used in MainProject. Find the project structure in eclipse.
Gradle Include Dependency on another Project and Execution Order

Gradle Include Dependency on another Project

Find the gradle script for ProjectOne.
build.gradle in ProjectOne
apply plugin: 'java'
apply plugin: 'eclipse'
repositories {
    mavenCentral()
}
dependencies {
    compile 'commons-lang:commons-lang:2.6'
} 
Find the gradle script for ProjectTwo.
build.gradle in ProjectTwo
apply plugin: 'java'
apply plugin: 'eclipse'
repositories {
    mavenCentral()
}
dependencies {
    compile 'commons-lang:commons-lang:2.6'
} 
We need to create a file for project settings in MainProject. To include a project dependency in gradle, gradle provides the following settings.
settings.gradle in MainProject
include   ':ProjectOne', ':ProjectTwo'
project(':ProjectOne').projectDir = new File(settingsDir, '../ProjectOne')
project(':ProjectTwo').projectDir = new File(settingsDir, '../ProjectTwo') 
Use project keyword to define the location. Here settingsDir is the combination of two word settings + Dir. The file name will be with first word that is settings.gradle. Now find the MainProject build.gradle.
build.gradle in MainProject
apply plugin: 'java'
apply plugin: 'eclipse'
repositories {
    mavenCentral()
}
dependencies {
    compile project(':ProjectOne')
    compile project(':ProjectTwo')
} 
We will use compile project command to refer the dependency.
compile project(':ProjectOne')
compile project(':ProjectTwo') 

Gradle Execution Order

Gradle Execution Order is acyclic. Gradle guarantees the execution order according to their dependency. We need not to worry about the order. If there are two project and if they are not mutually dependent then the execution order will be alphabetical. In our Example when we run gradle clean build on MainProject, then the execution order will be ProjectOne then projectTwo. These projects are not mutually dependent , so their execution order will be alphabetical. Check the build Log.
:clean
:ProjectOne:clean
:ProjectTwo:clean
:ProjectOne:compileJava
:ProjectOne:processResources UP-TO-DATE
:ProjectOne:classes
:ProjectOne:jar
:ProjectTwo:compileJava
:ProjectTwo:processResources UP-TO-DATE
:ProjectTwo:classes
:ProjectTwo:jar
:compileJava
:processResources UP-TO-DATE
:classes
:jar
:assemble
:compileTestJava UP-TO-DATE
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test UP-TO-DATE
:check UP-TO-DATE
:build
:ProjectOne:assemble
:ProjectOne:compileTestJava UP-TO-DATE
:ProjectOne:processTestResources UP-TO-DATE
:ProjectOne:testClasses UP-TO-DATE
:ProjectOne:test UP-TO-DATE
:ProjectOne:check UP-TO-DATE
:ProjectOne:build
:ProjectTwo:assemble
:ProjectTwo:compileTestJava UP-TO-DATE
:ProjectTwo:processTestResources UP-TO-DATE
:ProjectTwo:testClasses UP-TO-DATE
:ProjectTwo:test UP-TO-DATE
:ProjectTwo:check UP-TO-DATE
:ProjectTwo:build

BUILD SUCCESSFUL 

Download Source Code

POSTED BY
ARVIND RAI
ARVIND RAI
LEARN MORE








©2024 concretepage.com | Privacy Policy | Contact Us