I have a Gradle script similar to this:
ext {
dir = null
}
task init << {
build()
}
task buildAll(type: Exec){
workingDir ext.dir
commandLine 'cmd', '/c', "echo %JAVA_HOME%"
}
def build(){
ext.dir = "asdf"
buildAll.execute()
}
When I run the script, I get:
groovy.lang.MissingPropertyException: Cannot get property 'dir' on extra properties extension as it does not exist
Whatever I tried, I couldn't get a task to read a property from "ext". It can be seen from methods (like "build()" in my example), but not from any other task except the default one ("init" in my example).
I understand that the "ext" properties should be accessible from anywhere inside the project, so what am I doing wrong?
UPDATE: The workflow I'm trying to achieve (as asked by Opal):
I have several environments I need to build with one script. Each of these environments is listed in a CSV file with a line: <environment>,<version>
.
Script then needs to do the following:
${ANT_HOME}/bin/ant -f $checkedOutCodeDirectory/Build/build-all.xml target1
, then target2
and target3
)This needs to be executed for each environment
Properties in a build script can easily become a maintenance headache and convolute the build script logic. The gradle. properties helps with keeping properties separate from the build script and should be explored as viable option. It's a good location for placing properties that control the build environment.
ext is shorthand for project. ext , and is used to define extra properties for the project object. (It's also possible to define extra properties for many other objects.) When reading an extra property, the ext. is omitted (e.g. println project. springVersion or println springVersion ).
A Gradle project property is a property that can be accessed in your project's build. gradle and gets passed in from an external source when your build is executed.
Extra properties should be created via ext
but referred via project
instance without any instance at all so: project.dir
or dir
, so the first change to script will be:
ext {
dir = null
}
task init << {
build()
}
task buildAll(type: Exec){
workingDir dir // ext.dir -> dir
commandLine 'cmd', '/c', "echo %JAVA_HOME%"
}
def build(){
ext.dir = "asdf"
buildAll.execute()
}
Now, before any task or method is executed the script is read and parsed, sot the whole body of buildAll
will be configured before any other part is run. Thus it will always fail, since dir
property has no value. Proof:
ext {
dir = null
}
task init << {
build()
}
task buildAll(type: Exec){
workingDir dir ? dir : project.rootDir
commandLine 'cmd', '/c', "echo %JAVA_HOME%"
}
def build(){
ext.dir = "asdf"
buildAll.execute()
}
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