How to add external jar files to gradle build script

Viewed 37446

Now I use gradle as my build tool. One of my tasks needs to access to a mysql database. Following is my gradle script:

import groovy.sql.Sql

buildscript {
    dependencies {
        classpath files('/usr/share/java/mysql-connector-java.jar')
    }
}


task connectToDb << {
    def props = [user: 'user', password: 'password', allowMultiQueries: 'true'] as Properties
    def url = 'jdbc:mysql://mysqlhost:3306/db'
    def driver = 'com.mysql.jdbc.Driver'
    def sql = Sql.newInstance(url, props, driver)

    sql.eachRow('show tables') { row ->
        println row[0]
    }
}

I try to run it in a Ubuntu Lucid box but it always fails. The gradle complains with the information: Execution failed for task ':connectToDb'. Cause: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

It seems that the build script doesn't include the mysql connector jar library.Can anyone please tell me how to configure the external jar file properly ? Thanks.

3 Answers
Related