Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gradle this tasks reference

I would like to move some extra code to closure and call it in the scope of task, how to do it?

I have something like, and it works fine

task AwesomeTest(type: Test) {
   filter {
      setIncludePatterns(filtered as String[])
   }
}

but would like to have something like (of course example below doesn't work)

task AwesomeTest(type: Test) {
   extraFilter (this)
}

ext.extraFilter = { task ->
   task.filter {
      setIncludePatterns(filtered as String[])
   }
}

Any ideas?

like image 743
hanskoff Avatar asked Aug 13 '26 16:08

hanskoff


1 Answers

this refers to project. An instance of Task is passed as first argument to the closure. You need to name it or use it. Here you go:

apply plugin: 'java'

ext.extraFilter = { task ->         
   configure(task) {
      filter {
         setIncludePatterns('*')
      }
   }
}

task AwesomeTest(type: Test) { t ->
   extraFilter(t)
}
like image 170
Opal Avatar answered Aug 15 '26 06:08

Opal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!