Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scope of the extra properties in Gradle?

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:

  • Delete existing directory
  • Checkout code from SVN into new directory (both directory and SVN url depend on environment and version)
  • Copy some settings files (paths depend on version)
  • Edit some of those settings files (values depend on environment and version)
  • Set some environment variables (JAVA_HOME, ANT_HOME...) (depends on version)
  • Run three build commands (${ANT_HOME}/bin/ant -f $checkedOutCodeDirectory/Build/build-all.xml target1, then target2 and target3)

This needs to be executed for each environment

like image 755
zorglub76 Avatar asked Jul 06 '15 13:07

zorglub76


People also ask

What is the use of Gradle properties?

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.

What is Ext task in Gradle?

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 ).

What is project properties in Gradle?

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.


1 Answers

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()
}
like image 110
Opal Avatar answered Oct 11 '22 16:10

Opal