Richard Todd
Richard Todd

Reputation: 2496

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

Upvotes: 3

Views: 6271

Answers (2)

Opal
Opal

Reputation: 84884

If you navigate to the location where groovy is installed - might be under $GROOVY_HOME and list the folders you'll notice the lib folder. Listing the content of the lib folder shows that gpars-1.2.1.jar is present there (groovy v2.4.5).

All these jar file located in lib folder are added to classpath when groovy is started via groovysh or groovyConsole.

However this doesn't happen when it comes to gradle and as @Stanislav answered you need to add it to classpath manually.

Upvotes: 1

Stanislav
Stanislav

Reputation: 28136

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

Upvotes: 1

Related Questions