Grape 是一个内嵌在 Groovy 中的 JAR 依赖项管理器。它能使你在类路径上快速添加 Maven 库依赖项,更易于编写脚本。最简单的用法是在脚本上添加注释(annotation)
1.1 添加一个依赖项 1.2 指定附加依赖库 1.3 Maven 分类器 1.4 排除传递性依赖 1.5 JDBC 驱动 1.6 利用 Groovy Shell 来使用 Grape 1.7 代理设置 1.8 日志
用法示例 1 2 3 4 5 6 7 8 9 10 11 12 @Grapes ([ @GrabConfig (systemClassLoader=true ), @GrabResolver (name='maven' , root='https://repo1.maven.org/maven2/' ), @Grab (group='com.h2database' , module='h2' , version='1.4.193' , scope='test' ) ]) import javax.imageio.*import java.awt.image.*import java.awt.*import java.io.*import groovy.json.JsonBuilderimport groovy.sql.Sqlimport java.text.SimpleDateFormat
可能的问题 一直没任何响应
修改日志级别,打印grape download信息 1 groovy -Divy.message.logger.level=4 -Dgroovy.grape.report.downloads=true test.groovy
可以看到输出如下:一直停留在从 jcenter上下载jar包
1 2 3 4 5 6 7 8 9 10 Nbr of module to sort : 1 Sort dependencies of : com.h2database#h2;1.4.193 / Number of dependencies = 0 Sort done for : com.h2database#h2;1.4.193 storing dependency com.h2database#h2;1.4.193 in props resolved ivy file produced in cache :: downloading artifacts :: Preparing to download artifact com.h2database#h2;1.4.193!h2.jar trying https://jcenter.bintray.com/com/h2database/h2/1.4.193/h2-1.4.193.jar tried https://jcenter.bintray.com/com/h2database/h2/1.4.193/h2-1.4.193.jar `
之前设置的 GrabResolver 好像没有其作用
修改 grape 默认配置信息 在 ~/.grape目录下创建 grapeConfig.xml,内容如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <ivysettings > <settings defaultResolver ="downloadGrapes" /> <resolvers > <chain name ="downloadGrapes" returnFirst ="true" > <filesystem name ="cachedGrapes" > <ivy pattern ="${user.home}/.groovy/grapes/[organisation]/[module]/ivy-[revision].xml" /> <artifact pattern ="${user.home}/.groovy/grapes/[organisation]/[module]/[type]s/[artifact]-[revision](-[classifier]).[ext]" /> </filesystem > <ibiblio name ="localm2" root ="${user.home.url}/.m2/repository/" checkmodified ="true" changingPattern =".*" changingMatcher ="regexp" m2compatible ="true" /> <ibiblio name ="aliyun" root ="https://maven.aliyun.com/repository/jcenter/" m2compatible ="true" /> <ibiblio name ="ibiblio" m2compatible ="true" /> </chain > </resolvers > </ivysettings >
注意 :注销掉 jcenter 的 repo,因为不知为何,这个 repo 能正常访问到,但是无法下载 h2 的 jar,添加阿里云的repo,这里需要注意的是不能用网上教程给的 repo 地址,http://maven.aliyun.com/nexus/content/groups/public/ ,去阿里云页面上找最新的地址:https://maven.aliyun.com/mvn/view
使用 grape 命令 下载依赖包 如果采用默认的 jcenter 设置,即使用 grape 命令也是一样会卡在下载 jar 包的环节。
1 grape -V install com.h2database h2 1.4.193
输出如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 :: resolving dependencies :: caller#all-caller;working04 confs: [default] validate = false refresh = false resolving dependencies for configuration 'default' == resolving dependencies for caller#all-caller;working04 [default] == resolving dependencies caller#all-caller;working04->com.h2database#h2;1.4.193 [default->default] downloadGrapes: Checking cache for: dependency: com.h2database#h2;1.4.193 {default=[default]} downloadGrapes: module revision found in cache: com.h2database#h2;1.4.193 found com.h2database#h2;1.4.193 in jcenter == resolving dependencies caller#all-caller;working04->com.h2database#h2;1.4.193 [default->runtime] == resolving dependencies caller#all-caller;working04->com.h2database#h2;1.4.193 [default->compile] == resolving dependencies caller#all-caller;working04->com.h2database#h2;1.4.193 [default->master] resolved ivy file produced in cache :: downloading artifacts :: tried https://jcenter.bintray.com/com/h2database/h2/1.4.193/h2-1.4.193.jar downloading https://jcenter.bintray.com/com/h2database/h2/1.4.193/h2-1.4.193.jar ... jcenter: downloading https://jcenter.bintray.com/com/h2database/h2/1.4.193/h2-1.4.193.jar
参考文档