Groovy metadata builder and template spec, and a bit of refactoring