认识
官网:https://groovy-lang.org/
文档:https://groovy-lang.org/grape.html
仓库:None
Grape,是 Groovy 的内置依赖管理工具,可以让我们快速添加 Maven 依赖,简化脚本的编写。该笔记将记录:在 Groovy 中,如何使用 Grape 管理依赖,以及常见问题处理。
组成
—— 该部分将介绍 Grape 工具的组成,例如,架构、组成、工作原理。但鉴于我们以使用为主,暂时不探究 Grap 的组成及架构。
构建
—— 该部分将介绍如何构建 Grape 依赖工具,以便于我们在后续的 Groovy 编程中使用 Grape 来加载依赖。
根据文档 … Grape is a JAR dependency manager embedded into Groovy … 描述,我们不需要单独安装 Grape 工具,我们可以直接使用其来管理依赖。
性质
使用其他仓库
@GrabResolver(name='custom', root='http://customserver/repo', m2Compatible='true') @Grab('com.mrhaki:groovy-samples:1.0') import com.mrhaki.groovy.Sample def s = new Sample() s.justToShowGrabResolver() // Just a sample
如下示例,在 Groovy 中,使用阿里云镜像仓库:
@Grapes([ @GrabResolver(name='aliyun', root='https://maven.aliyun.com/repository/central', m2Compatible='true'), @Grab(group='org.xerial',module='sqlite-jdbc',version='3.7.2'), @GrabConfig(systemClassLoader=true) ])
调试下载过程 | 调试信息
在安装扩展的过程中,我们希望显示扩展的下载信息,以了解下载进度,判断下载是否阻塞。此时,可以增加调试级别:
groovy -Divy.message.logger.level=4 -Dgroovy.grape.report.downloads=true example.groovy // 或者,使用环境变量 export JAVA_OPTS="$JAVA_OPTS -Divy.message.logger.level=4 -Dgroovy.grape.report.downloads=true"
通过代理加速下载
groovy -Dhttp.proxyHost=yourproxy -Dhttp.proxyPort=8080 yourscript.groovy // 或者,使用环境变量 export JAVA_OPTS="$JAVA_OPTS -Dhttp.proxyHost=127.0.0.1 -Dhttp.proxyPort=8123 -Dhttps.proxyHost=127.0.0.1 -Dhttps.proxyPort=8123"
应用
在代码中,引入依赖:
@Grab(group='org.springframework', module='spring-orm', version='3.2.5.RELEASE') import org.springframework.jdbc.core.JdbcTemplate // 或者,使用简写 @Grab('org.springframework:spring-orm:3.2.5.RELEASE') import org.springframework.jdbc.core.JdbcTemplate
… java.lang.RuntimeException: Error grabbing Grapes — [download failed: …
Groovy – Grab – download failed – Stack Overflow
Try deleting ~/.m2 directory and also ~/.groovy/grapes directories.
参考
Dependency management with Grape
groovy grape verbose
Groovy Goodness: Use GrabResolver for Custom Repositories
Groovy 使用 Garpe 依赖管理器