有人可以解释为什么即使我已经使用Grape安装了依赖项,Groovy也无法找到JDBC驱动程序吗?
$ grape --version
Groovy Version: 1.7.7 JVM: 1.6.0_20
$ grape install org.apache.derby derby 10.5.3.0
:: loading settings :: url = jar:file:/opt/groovy-1.7.7/lib/ivy-2.2.0.jar!/org/apache/ivy/core/settings/ivysettings.xml
:: resolving dependencies :: caller#all-caller;working
confs: [default]
found org.apache.derby#derby;10.5.3.0 in remote-repo
downloading http://127.0.0.1:8081/artifactory/repo/org/apache/derby/derby/10.5.3.0/derby-10.5.3.0.jar ...
[SUCCESSFUL ] org.apache.derby#derby;10.5.3.0!derby.jar (388ms)
$ grape resolve org.apache.derby derby 10.5.3.0
/home/alex/.groovy/grapes/org.apache.derby/derby/jars/derby-10.5.3.0.jar
$ groovy file_parser.groovy records.txt csv
Caught: java.lang.ClassNotFoundException: org.apache.derby.jdbc.EmbeddedDriver
at film_parser.run(film_parser.groovy:16)
$ groovy -cp /home/alex/.groovy/grapes/org.apache.derby/derby/jars/derby-10.5.3.0.jar file_parser.groovy records.txt csv
Inserted 1 rows.
答案 0 :(得分:5)
安装依赖项并不意味着它可以立即用于您的脚本,它只会将jar下载到本地缓存。
依赖的使用由注释控制
一个完整的例子,有不同的驱动程序,应该有帮助
@GrabConfig(systemClassLoader=true)
@Grab('org.hsqldb:hsqldb:2.0.0')
import groovy.sql.Sql
def sql = Sql.newInstance(
"jdbc:hsqldb:mem:database", "sa", "", "org.hsqldb.jdbcDriver")
println sql.firstRow('select * from information_schema.tables')