如何为所有子项目的项目设置依赖项?

Gan*_*nus 5 project-structure dependency-management gradle

我不喜欢我junit为主项目和子项目重复每个存储库依赖(让我们说).是否有可能使子项目使用主项目的依赖项.或者还有另一种逃避重复的方法吗?

Vya*_*ets 10

与接受的答案不同,最好使用以下内容:

allprojects {
  plugins.withType(JavaPlugin) {
    dependencies {
      testCompile 'junit:junit:4.12'
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

如果java插件已经存在,或者将注意添加并稍后应用,则会立即应用更改.

更新

目前我使用更好的方法来控制插件的配置 - pluginManager.效果与for相同plugins.withType,但您不必知道插件的类名:

org.springframework.boot插件示例:

apply plugin: 'org.springframework.boot'

allprojects {
  pluginManager.withPlugin('org.springframework.boot') {
    springBoot {
      buildInfo()
      layout 'DIR'
    }
  }
}
Run Code Online (Sandbox Code Playgroud)


lan*_*ava 5

root / build.gradle

allprojects {
    if (plugins.hasPlugin('java')) {
        dependencies {
            testCompile 'junit:junit:4.12'
        }
    }
}
Run Code Online (Sandbox Code Playgroud)