「Apache Groovy」- Grape,依赖管理工具(学习笔记) @20210213

问题描述

Grape,是 Groovy 的依赖管理工具,可以让我们快速添加 Maven 依赖,简化脚本的编写。

该笔记将记录:在 Groovy 中,如何使用 Grape 管理依赖,以及常见问题处理。

解决方法

在代码中,引入依赖:

@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

常见问题汇总

使用其他仓库

@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'

相关文章

「Groovy」- 处理路径地址
「Groovy」- 操作 HTML 文档
「Groovy」- 处理 Object 与 JSON String 之间的转换
「Groovy」- 操作文件(读取、写入)
「Groovy」- 连接数据库(使用 MySQL 演示)
「Apache Groovy」- 连接 SQLite 数据库
「Apache Groovy」- 运行 Shell 命令
「Groovy」- 彩色化输出日志

参考文献

Dependency management with Grape
groovy grape verbose
Groovy Goodness: Use GrabResolver for Custom Repositories
Groovy 使用 Garpe 依赖管理器


posted @ 2021-02-13 09:21  研究林纳斯写的  阅读(310)  评论(0编辑  收藏  举报