title: Running Groovy script from Gradle using GroovyShell: Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/cli/ParseException
body: <p>I want to run a groovy command-line script from my Gradle build script. </p>
  
  <p>I'm using this code in my Gradle script:</p>
  
  <pre><code>def groovyShell = new GroovyShell();
  groovyShell.run(file('script.groovy'), ['arg1', 'arg2'] as String[])
  </code></pre>
  
  <p>Things work fine until my Groovy script (script.groovy) uses the CliBuilder class. Then I get the following exception:</p>
  
  <blockquote>
    <p>org.codehaus.groovy.runtime.InvokerInvocationException: java.lang.NoClassDefFoundError: org/apache/commons/cli/ParseException
    ...
    Caused by: java.lang.ClassNotFoundException: org.apache.commons.cli.ParseException</p>
  </blockquote>
  
  <p>I found lots of people with similar problems and errors, but "the solution" was difficult to extract from the numerous posts I read. Lots of people suggested putting the commons-cli jar on the classpath, but doing so for the GroovyShell was not at all apparent to me. Also, I had already declared @Grapes and @Grab for my required libraries in the script.groovy, so it should have everything it needed. </p>