I've setup a multi-module Gradle build, with several modules inside. What I'm trying to do is very simple - when I create a distribution, I'd always like to use a directory called "dist/lib", with the exception of one project, for which it should just be "dist".
The obvious solution - make a variable called "distLibDir", and overwrite it for the specific project in question, does not appear to be working.
Here's the code for the sub-module:
project.version = '1.1'
project.ext.distLibDir = 'dist'
dependencies {
....
}
And here's the code for the top-level project:
subprojects {
apply plugin: 'java'
apply plugin: 'eclipse'
if (! project.hasProperty('distLibDir')) {
project.ext.distLibDir = 'dist/lib'
}
task copyLib(type: Copy) {
into project.distLibDir
from configurations.runtime
}
task dist(type: Copy, dependsOn: [clean, jar, copyLib]) {
from 'build/libs'
into project.distLibDir
}
}
No matter what I try, the directory always comes out to be "dist/lib" and I can't overwrite it to be different for just that one module. Does anyone have insight into what is going wrong here?
By default, build scripts of parent projects get evaluated before build scripts of child projects. Hence the property doesn't exist at the time that you check for its existence.
There are several solutions. One is to set the property's value in the parent script's suprojects
block, based on the name or path of the current project (e.g. if (project.path == ":foo") {...} else {...}
). Another is to move the body of the subprojects
block (or at least the part that makes use of the property) into a script plugin and apply that plugin from every build script (e.g. apply from: "$rootDir/gradle/foo.gradle"
). This gives you a chance to set the property's value before applying the script plugin. Yet another solution (which I personally try to avoid) is to keep things as they are and call evaluationDependsOnChildren()
before accessing the property from the parent build script.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With