How do I use include(Spec includeSpec)?


several task types implement the method

include(Spec<FileTreeElement> includeSpec)


How do I actually use this method from my build script? Particularly, I don’t know where the instance of Spec comes from.

Regards, Björn

This method is meant to be used by tasks/plugins implemented in Java. In this case, the caller implements the Spec interface, typically with an anonymous inner class. For usage from build script, there is another ‘include’ method which accepts a closure. This is a common pattern in the Gradle APIs.

A good way to learn about these things is to check out the DSL reference, which comes with many code examples.

It’s also common that there is a ‘Closure’ based override for any ‘Spec’ method that is more build script (i.e. Groovy) friendly:


Hi Peter, hi Luke,

thanks for your explanations. I did browse through the DSL and the API, but their sentence “Adds an include spec” didn’t really help. In the sources I only found one class which used the include spec with the interface, but only now do I get its idea after Peter’s comment.

Right, the variant with the closure is easier to use.

BTW: Thanks a lot for this great tool!

Hey guys, I’m looking for an answer to this question as well. I know this is an old topic, but lots of Googling hasn’t yielded any results. I understand that I need to pass in a “Spec”, but I still have no idea how to do this in a Gradle file.


Why don’t you use the closure based variant (‘include { … }’)?

Thanks for your reply Peter. I guess I don’t understand how that is enough information to solve the problem. Presumably there is a set of variables that I need to define in the closure. Isn’t what you are essentially saying is “why don’t you just pass in an object?” I am not used to convention based languages, so that is probably the root of my problems. I learn by example, but I’ve found that with Gradle, most examples are duplicating the same exact code, and giving no explanation as to why certain choices were made, what type of object is expected, etc.

Please describe your problem in detail, as a new post. From your first question it’s impossible for me to tell what other information you might need that hasn’t already been provided in this thread.