Classpath not recognized by groovy classes

The groovy class CLIBuilder depends on commons-cli. I have a build that requires using CLIBuilder, Thus I put commons-cli in the buildscript.dependencies block. However, whatever is loading up CLIBuilder doesn’t seem to be recognizing it and thus I can’t create the CLIBuilder class. Do the groovy classes have a different classLoader?

Here’s a simple build.gradle that recreates the issue.

buildscript {
    dependencies {
        classpath "commons-cli:commons-cli:1.2"
    repositories {
  import org.apache.commons.cli.ParseException
task classLoader {
    doLast {
        buildscript.configurations.classpath.each { println it }
        println "CliBuilder class: ${CliBuilder.class}"
        println "ParseException class: ${ParseException.class}"
        def pe = new ParseException()
        println "ParseException object: ${pe}"
      def cli = new CliBuilder() // java.lang.NoClassDefFoundError: org/apache/commons/cli/ParseException

In this example, the ParseException is perfectly resolved and created. Here’s the output:

CliBuilder class: class groovy.util.CliBuilder
ParseException class: class org.apache.commons.cli.ParseException
ParseException object: org.apache.commons.cli.ParseException

If you uncomment line 20, the whole thing barfs when trying to create a CLIBuilder object and can’t resolve the same ParseException class.

Execution failed for task ':classLoader'.
> org/apache/commons/cli/ParseException
Caused by: java.lang.ClassNotFoundException: org.apache.commons.cli.ParseException