Last active
August 29, 2015 14:05
-
-
Save breskeby/519cee19d3cb6127fff6 to your computer and use it in GitHub Desktop.
Extending objects in NamedDomainObjectContainers (GRADLE)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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