Reputation: 1742
I'm quite new to gradle, and I like this build system for its expressive power. However some things look quite tricky.
Consider the configuration code:
project(":A") {
apply plugin: "X"
apply plugin: "Y"
someVar = "aa"
dependencies {
compile project(":C1")
compile project(":C2")
compile project(":C3")
}
}
project(":B") {
apply plugin: "X"
apply plugin: "Z"
someVar = "bb"
dependencies {
compile project(":C1")
compile project(":C2")
}
}
I want to make this config as DRY as possible. What I naively tried to do:
void myProjectType(someVarValue){
apply plugin: "X"
someVar = someVarValue
dependencies {
compile project(":C1")
compile project(":C2")
}
}
project(":A") {
myProjectType("aa");
apply plugin: "Y"
dependencies {
compile project(":C3")
}
}
project(":B") {
myProjectType("bb");
apply plugin: "X"
}
Looks nice for me, but it doesn't work in gradle. Any suggestions, how to do it the right way?
If found the solution to half of the problem, which looks like this:
project(":A"){
...
apply from: "${rootProject.projectDir}/gradle-config/config.gradle"
...
}
however such approach doesn't allow explicit parametrization of the 'applied' part (someVarValue as a parameter from previous example)
Upvotes: 1
Views: 127
Reputation: 84756
You need to pass an instance of the project
to the method - otherwise gradle won't know what is configured. So it will be:
subprojects {
apply plugin: 'java'
}
project(':A') {
apply plugin: 'war'
common(project, 'aa')
dependencies {
compile project(':C3')
}
}
project(':B') {
apply plugin: 'groovy'
common(project, 'bb')
}
def common(p, value) {
p.with {
ext.someVar = value
dependencies {
compile project(':C1')
compile project(':C2')
}
}
}
['A', 'B'].each { n ->
configure(project(":$n")) { p ->
task printVar << {
println "project -> $p.name, var -> $someVar"
}
task printDeps << {
p.configurations.compile.each { d ->
println "project -> $p.name, dependency -> $d"
}
}
}
}
Run gradle printDeps printVar
to verify if it works correctly. Full demo is here. Feel free to ask in case of any questions.
Upvotes: 1