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 {
        mavenCentral()
    }
}
  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:

:classLoader
/home/colby/.gradle/caches/modules-2/files-2.1/commons-cli/commons-cli/1.2/2bf96b7aa8b611c177d329452af1dc933e14501c/commons-cli-1.2.jar
CliBuilder class: class groovy.util.CliBuilder
ParseException class: class org.apache.commons.cli.ParseException
ParseException object: org.apache.commons.cli.ParseException
  BUILD SUCCESSFUL

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