Search code examples
groovygradleimportgroovy-console

Groovy unable to resolve class but works in groovyconsole


Trying to import the gpars withPool method into my project. The import works in groovyconsole but not when building in gradle.

Both groovyconsole and gradle are running groovy version 2.4.5

Any ideas?

Groovy Console

import static groovyx.gpars.GParsPool.withPool
withPool(2) { (1..5).collectParallel { println it.toString() } }

Output:

1
3
2
4
5
Result: [null, null, null, null, null]

Gradle compileGroovy

Same import step as above:

import static groovyx.gpars.GParsPool.withPool

Gradle output:

:compileGroovystartup failed:
C:\Programming\Projects\groovy\src\main\groovy\lib.groovy: 18: unable to resolve class groovyx.gpars.GParsPool
 @ line 18, column 1.
   import static groovyx.gpars.GParsPool.withPool
   ^

1 error

 FAILED

Solution

  • If you wish to import something into your build script, then you have to provide it as a build script dependency, in order to make Gradle know, where to find it, just like:

    buildscript {    
        repositories {
            mavenCentral()
            jcenter()
        }
    
        dependencies {
            classpath group: 'org.codehaus.gpars', name: 'gpars', version: '1.1.0'
        }
    }
    
    import static groovyx.gpars.GParsPool.withPool