我有一个Gradle多项目构建,如下所示:
rootProject
build.gradle
settings.gradle
shared/
SharedLib
SharedLib2
plugins/
FirstPlugin
SecondPlugin
我想将目录shared
中的所有项目作为依赖项添加到plugins
中的所有项目。
更一般地说:如何按目录配置子项目?
settings.gradle的内容:
include 'plugins:FirstPlugin', 'plugins:SecondPlugin', 'shared:SharedLib', 'shared:SharedLib2'
build.gradle的内容:
task wrapper(type: Wrapper) { gradleVersion = "2.1" }
allprojects{ apply plugin:"java" }
subprojects {
group = "eu.test.myGroup"
repositories { mavenCentral() }
dependencies { testCompile "junit:junit:4.11" }
}
答案 0 :(得分:0)
将此添加到build.gradle会实现我想要实现的目标:
// Define what's a plugin and what's a shared library by directory paths.
// Ignore empty projects
def plugins = subprojects.findAll{ it.path.contains("plugins") && hasSrc(it) }
def sharedLibs = subprojects.findAll{ it.path.contains("shared") && hasSrc(it)}
/** Checks whether a project has a source directory */
def hasSrc(proj){
return new File(proj.projectDir, "src").exists()
}
// Configure the plugins to depend on the shared libraries at compile time
configure(plugins) { dependencies { sharedLibs.each{compile it} } }