Skip to content

Instantly share code, notes, and snippets.

@breskeby
Last active August 29, 2015 14:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save breskeby/519cee19d3cb6127fff6 to your computer and use it in GitHub Desktop.
Save breskeby/519cee19d3cb6127fff6 to your computer and use it in GitHub Desktop.
Extending objects in NamedDomainObjectContainers (GRADLE)
import org.gradle.internal.reflect.Instantiator
class Host {
String name
Host(String name){
this.name = name
}
}
class Foo {
def host
def someFooVar = "default"
public Foo(Host fooHost){
this.host = fooHost
}
}
project.extensions.hosts = project.container(Host, new NamedDomainObjectFactory<Host>(){
Host create(String name){
def instantiator = project.services.get(Instantiator.class)
return instantiator.newInstance(Host, name)
}
})
hosts.all{ currentHost ->
currentHost.extensions.create('foo', Foo, currentHost)
}
hosts {
host1 {
foo {
someFooVar = "bar"
}
}
}
task assertHostConfig << {
assert hosts.host1.foo.someFooVar == "bar"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment