groovy script classpath

后端 未结 2 1386
温柔的废话
温柔的废话 2020-12-10 17:30

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

相关标签:
2条回答
  • 2020-12-10 17:38

    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
    
    0 讨论(0)
  • 2020-12-10 17:48

    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() )

    0 讨论(0)
提交回复
热议问题