是否可以在Gradle build.gradle中使用@Grab?

时间:2018-05-03 15:35:10

标签: gradle groovy grape

是否可以在Gradle build.gradle文件中使用@Grab?

我的理解是Gradle构建脚本是用Groovy编写的,而Grape / @Grab是内置在Groovy中的。

但如果我尝试将@Grab注释添加到build.gradle文件中,它就不起作用。

e.g。中加入:

@Grab(group='org.springframework', module='spring-orm', version='3.2.5.RELEASE')
import org.springframework.jdbc.core.JdbcTemplate

它给了我错误

org.gradle.groovy.scripts.ScriptCompilationException: Could not compile build file.

这可能吗?

1 个答案:

答案 0 :(得分:0)

根据this post,答案是否定的,我们无法使用@Grab

但是,有一种方法可以声明依赖项并将其添加到类路径中,如下所示(示例的用法:gradle go):

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath group: 'org.springframework', name: 'spring-orm', version: '3.2.5.RELEASE'
    }
}

import org.springframework.jdbc.core.JdbcTemplate

task go() {
    doLast {
        println 'TRACER : ' + JdbcTemplate.class.getSimpleName()
    }
}
相关问题