Gradle variable scope

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Gradle variable scope

pieter.smit
hi Hans,

Firstly, thanks for gradle, i think it's great. I need some help understanding how varable scoping works in gradle.

I've got some problems defining variables/tasks in the allprojects{} or subprojects{} groups. When resolving variables, it seems that the last project's variables are the ones that are resolved. Simple example follows:

// Copies the distrobution jar(s) to the collective distro directory
Closure distro = { task ->
  println "copy $task.project.name to colective"
  task.project.libs.archiveNames.each {archiveName ->
    ant.copy(file: task.project."$archiveName".archivePath, todir: 'collective')
  }
}

Closure simplePrint = { task ->
  println "i work when called directly, $task.project.name"
}

subprojects {
  usePlugin('java')
  type='jar'
  sourceCompatibility = 1.5
  targetCompatibility = 1.5
  version='1.0'
 
  createTask('printTest', simplePrint)
  createTask('distro', distro)
 
  task('libs').doLast {
    task('distro').execute()
  }

  createTask("copyDist", dependsOn:'distro') {
  }
}

project(':subProjectA') {
  srcDirNames = ['']
}

project(':subProjectB') {
  srcDirNames = ['']
}

example.tgz

When I execute libs, the 'distro' task will always print out "copy subProjectB to colective", and subProjectA's jar will not be in the directory. It works correctly if i execute task 'copyDist' (both jars are copied). The 'printTest' task also works as expected.

My problem is that i cannot always use the dependsOn method as I have some complex build requirements. It will make the build file a lot shorter and more elegant if I could count on variable scoping.

It basically boils downto this: I cannot create tasks in the root project that behaves differently when executed by subprojects based on certain criteria (variables).

Regards,
Pieter
Reply | Threaded
Open this post in threaded view
|

Re: Gradle variable scope

hans_d
Administrator
Hi Pieter,

On Jul 7, 2008, at 11:08 AM, pieter.smit wrote:

>
> hi Hans,
>
> Firstly, thanks for gradle, i think it's great. I need some help
> understanding how varable scoping works in gradle.
>
> I've got some problems defining variables/tasks in the allprojects
> {} or
> subprojects{} groups. When resolving variables, it seems that the last
> project's variables are the ones that are resolved. Simple example  
> follows:
>
> // Copies the distrobution jar(s) to the collective distro directory
> Closure distro = { task ->
>   println "copy $task.project.name to colective"
>   task.project.libs.archiveNames.each {archiveName ->
>     ant.copy(file: task.project."$archiveName".archivePath, todir:
> 'collective')
>   }
> }
>
> Closure simplePrint = { task ->
>   println "i work when called directly, $task.project.name"
> }
>
> subprojects {
>   usePlugin('java')
>   type='jar'
>   sourceCompatibility = 1.5
>   targetCompatibility = 1.5
>   version='1.0'
>
>   createTask('printTest', simplePrint)
>   createTask('distro', distro)
>
>   task('libs').doLast {
>     task('distro').execute()
>   }

The task('distro') is always resolved against the current project. If  
you do:

task('libs').doLast { task ->
     task.project.task('distro').execute()
} or

task('libs').doLast { task ->
     task.project.distro.execute()
}

Gradle does what you want.

BTW: For your usage scenario it might be interesting to pay attention  
to a Groovy trap described in Appendix A.1 of the user's guide.

- Hans

>
>   createTask("copyDist", dependsOn:'distro') {
>   }
> }
>
> project(':subProjectA') {
>   srcDirNames = ['']
> }
>
> project(':subProjectB') {
>   srcDirNames = ['']
> }
>
> http://www.nabble.com/file/p18312735/example.tgz example.tgz
>
> When I execute libs, the 'distro' task will always print out "copy
> subProjectB to colective", and subProjectA's jar will not be in the
> directory. It works correctly if i execute task 'copyDist' (both  
> jars are
> copied). The 'printTest' task also works as expected.
>
> My problem is that i cannot always use the dependsOn method as I  
> have some
> complex build requirements. It will make the build file a lot  
> shorter and
> more elegant if I could count on variable scoping.
>
> It basically boils downto this: I cannot create tasks in the root  
> project
> that behaves differently when executed by subprojects based on certain
> criteria (variables).
>
> Regards,
> Pieter
> --
> View this message in context: http://www.nabble.com/Gradle-variable- 
> scope-tp18312735p18312735.html
> Sent from the gradle-user mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>

--
Hans Dockter
Gradle Project lead
http://www.gradle.org





---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Gradle variable scope

pieter.smit
Thanks, work's like a charm.

Pieter