8

I'm writing a script in Groovy and I would like someone to be able to execute it simply by running ./myscript.groovy. However, this script requires a 3rd party library (MySQL JDBC), and I don't know of any way to provide this to the script other than via a -classpath or -cp argument, e.g.

`./monitor-vouchers.groovy -cp /path/to/mysql-lib.jar`

For reasons I won't go into here, it's not actually possible to provide the JAR location to the script using the -classpath/-cp argument. Is there some way that I can load the JAR from within the script itself? I tried using @Grab

import groovy.sql.Sql


@Grab(group='mysql', module='mysql-connector-java', version='5.1.19')
def getConnection() {
    def dbUrl = 'jdbc:mysql://database1.c5vveqm7rqgx.eu-west-1.rds.amazonaws.com:3306/vouchers_prod'
    def dbUser = 'pucaroot'
    def dbPassword = 'password'
    def driverClass = "com.mysql.jdbc.Driver"

    return Sql.newInstance(dbUrl, dbUser, dbPassword, driverClass)
}

getConnection().class

But this causes the following error:

Caught: java.sql.SQLException: No suitable driver
java.sql.SQLException: No suitable driver
        at monitor-vouchers.getConnection(monitor-vouchers.groovy:13)
        at monitor-vouchers.run(monitor-vouchers.groovy:17)

Is there a way I can execute this script using just ./monitor-vouchers.groovy

2 Answers 2

13

You should be able to do:

import groovy.sql.Sql

@GrabConfig(systemClassLoader=true)
@Grab('mysql:mysql-connector-java:5.1.19')
def getConnection() {
    def dbUrl = 'jdbc:mysql://database1.c5vveqm7rqgx.eu-west-1.rds.amazonaws.com:3306/vouchers_prod'
    def dbUser = 'pucaroot'
    def dbPassword = 'bigsecret'
    def driverClass = "com.mysql.jdbc.Driver"

    return Sql.newInstance(dbUrl, dbUser, dbPassword, driverClass)
}

getConnection().class
Sign up to request clarification or add additional context in comments.

Comments

10

Two more options:

  1. Put the jar in ${user.home}/.groovy/lib
  2. If the jar is in a known location, use this code to load it into the current class loader:

    this.class.classLoader.rootLoader.addURL( new URL() )

4 Comments

If you ever want to see what is in your classpath, run this this.class.classLoader.rootLoader.URLs.each{ println it } --for me, it seems ~/.groovy/lib is not in there.
If it is a static class or context (such as the main method of a script) you should use the class name in place of this
A Groovier variant of MarkHu's classpath display command: println this.class.classLoader.rootLoader.URLs.collect() { it.toString() }.join('\n')
re: MarkHu ~/.groovy/lib doesn't appear in classpath because that directory is never a classpath element. Jars in that directory (if any) get added to the classpath.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.