Classloader issue when using jdbc driver in task in buildSrc

I’m trying to create a database in a custom task defined in buildSrc.
Although the driver seems on the classpath, when performing Class.forName("org.h2.Driver"); I get a ‘No suitable driver found’.
When creating the Driver class manually via Driver driver = new Driver(); it just creates the instance.

I’ve set up an example at just run ./gradlew clean createDatabase to reproduce the issue.

I solved it by using:


instead of: