Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle - subprojects exclude parent defined project dependency

How can I exclude parent defined compile project dependency?

Structure of modules is as follows:

- build.gradle
- settings.gradle
- MAIN_MODULE_1
  - build.gradle
- MODULE1
  - build.gradle
- MODULE2
  - build.gradle
- MODULE3
  - build.gradle
- MODULE4
  - build.gradle

And dependency model should be:

- MODULE1 <- MAIN_MODULE_1
- MODULE2 <- MAIN_MODULE_1
- MODULE3 <- MAIN_MODULE_1
- MODULE4 <- MAIN_MODULE_1

Which means, having

// build.gradle
subprojects {
  apply plugin: 'java'
  dependencies {
    compile project('MAIN_MODULE_1')
  }
}

// settings.gradle
include ':MAIN_MODULE_1'
include ':MODULE1'
include ':MODULE2'
include ':MODULE3'
include ':MODULE4'

will create circular dependency of MAIN_MODULE_1 on itself, which is wrong.

and having the dependency in each module (of total count not 4 but 120) seems wrong to me.

like image 702
Marek Sebera Avatar asked Mar 13 '14 18:03

Marek Sebera


2 Answers

You can use the configuration block :

project(':MAIN_MODULE_1') {
  apply plugin: 'java'
}

configure(subprojects - project(':MAIN_MODULE_1')) {
  apply plugin: 'java'
  dependencies {
    compile project('MAIN_MODULE_1')
  }
}

The answer is coming from this other question Gradle exclude plugin in main project for specific subproject(s)

like image 143
lucbelanger Avatar answered Sep 21 '22 08:09

lucbelanger


You can conditionally apply the dependency only if you are not in project MAIN_MODULE_1:

  dependencies {
    if (!project.name.equals("MAIN_MODULE_1")) {
      compile project(':MAIN_MODULE_1')
    }
  }

The answer below is a cleaner solution.

like image 43
ditkin Avatar answered Sep 20 '22 08:09

ditkin