我正在使用Gradle编译Groovy项目,但我注意到当我在代码中使用@Grab注释时,我收到以下错误:
$ gradle compile :buildInfo :compileJava UP-TO-DATE :compileGroovy FAILED FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':compileGroovy'. > org/apache/ivy/core/report/ResolveReport
(此处为完整堆栈跟踪http://pastebin.com/0ty4jNct)
我发现让它工作的唯一方法是将'groovy'和'ivy'模块添加到 groovy 类路径中,但我想避免这种情况,因为不推荐使用 groovy 类路径。
这是Gradle bug吗?还是有更好的方法来管理@Grab依赖?
答案 0 :(得分:30)
@Grab
旨在用于未预编译的独立脚本,并且通常不会将它与编译代码一起使用。如果这样做,您可能需要将Ivy添加到groovyClasspath
。类似的东西:
repositories {
mavenCentral()
}
configurations {
ivy
}
dependencies {
ivy "org.apache.ivy:ivy:2.3.0"
compile "org.codehaus.groovy:groovy-all:2.1.5"
}
tasks.withType(GroovyCompile) {
groovyClasspath += configurations.ivy
}
尽管如此,更好的方法是使用Gradle管理依赖关系。
答案 1 :(得分:0)
接受的解决方案在编译时对我有用,但我在运行时仍遇到类似的问题。通过从编译中排除葡萄代码,以下内容对我有用:
compileGroovy {
groovyOptions.configurationScript = file("gradle/config.groovy")
}
...其中gradle/config.groovy
是一个单独的文件,其内容为:
withConfig(configuration) {
configuration.setDisabledGlobalASTTransformations(['groovy.grape.GrabAnnotationTransformation'] as Set)
}