简体   繁体   English

Groovy无法解析类但在groovyconsole中工作

[英]Groovy unable to resolve class but works in groovyconsole

Trying to import the gpars withPool method into my project. 尝试将gpars withPool方法导入到我的项目中。 The import works in groovyconsole but not when building in gradle. 导入在groovyconsole中工作,但在gradle中构建时不起作用。

Both groovyconsole and gradle are running groovy version 2.4.5 groovyconsole和gradle都运行groovy版本2.4.5

Any ideas? 有任何想法吗?

Groovy Console Groovy控制台

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 Gradle compileGroovy

Same import step as above: 与上面相同的导入步骤:

import static groovyx.gpars.GParsPool.withPool

Gradle output: Gradle输出:

: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

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: 如果您希望将某些内容导入构建脚本,那么您必须将其作为构建脚本依赖项提供,以便让Gradle知道,在哪里找到它,就像:

buildscript {    
    repositories {
        mavenCentral()
        jcenter()
    }

    dependencies {
        classpath group: 'org.codehaus.gpars', name: 'gpars', version: '1.1.0'
    }
}

import static groovyx.gpars.GParsPool.withPool

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. 如果您导航到安装了groovy的位置 - 可能在$GROOVY_HOME并列出您将注意到lib文件夹的文件夹。 Listing the content of the lib folder shows that gpars-1.2.1.jar is present there (groovy v2.4.5). 列出lib文件夹的内容显示gpars-1.2.1.jar存在于那里(groovy v2.4.5)。

All these jar file located in lib folder are added to classpath when groovy is started via groovysh or groovyConsole . 当通过groovyshgroovyConsole启动groovy时,位于lib文件夹中的所有这些jar文件都会添加到类路径中。

However this doesn't happen when it comes to gradle and as @Stanislav answered you need to add it to classpath manually. 然而,当涉及到gradle并且@Stanislav回答你需要手动将它添加到类路径时,这不会发生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM