I have define a custom configuration and dependencies.
repositories {
mavenCentral()
}
configurations {
myConfig
}
dependencies {
myConfig 'org.foo:foo:+'
}
How can I create ClassLoader to load a class dynamically?
task myTask {
def classLoader = configurations.myConfig.????
def foo = Class.forName( "org.foo.Foo", true, classLoader ).newInstance();
}
I found this solution now. I hope there is nicer solution.
def classLoader = getClassLoader( configurations.myConfig )
ClassLoader getClassLoader( Configuration config ) {
ArrayList urls = new ArrayList()
config.files.each { File file ->
urls += file.toURI().toURL()
}
return new URLClassLoader( urls.toArray(new URL[0]) );
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With