Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a dependency for a project with all subprojects?

I don't like that I repeat every repository dependency (let us say, junit), for the main project and for subprojects. Is there a possibility to make the subproject to use the dependencies of the main project. Or is there another way to escape that repetition?

like image 523
Gangnus Avatar asked Jan 26 '17 14:01

Gangnus


2 Answers

Unlike the accepted answer it's better to use the following:

allprojects {
  plugins.withType(JavaPlugin) {
    dependencies {
      testCompile 'junit:junit:4.12'
    }
  }
}

The changes will be applied immediately if java plugin already exists or will watch for it to be added and apply later.

Updated

At the moment I use better way to control configuration for plugin - pluginManager. The effect is the same as for plugins.withType, but you don't have to know plugin's class name:

Example for org.springframework.boot plugin:

apply plugin: 'org.springframework.boot'

allprojects {
  pluginManager.withPlugin('org.springframework.boot') {
    springBoot {
      buildInfo()
      layout 'DIR'
    }
  }
}
like image 103
Vyacheslav Shvets Avatar answered Sep 22 '22 14:09

Vyacheslav Shvets


root/build.gradle

allprojects {
    if (plugins.hasPlugin('java')) {
        dependencies {
            testCompile 'junit:junit:4.12'
        }
    }
}
like image 21
lance-java Avatar answered Sep 19 '22 14:09

lance-java