Question

Please give me some lights about what I'm doing wrong here. First of all I'm newbie with Gradle and Groovy and for learning purposes I'm playing with them and DBUnit.

I tried the code listed below, my goal is to generate a dataset getting the data from a mysql db.

import groovy.sql.Sql
import org.dbunit.database.DatabaseConnection;
import org.dbunit.database.IDatabaseConnection;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.xml.FlatXmlDataSet;

repositories {
    mavenCentral()
}

configurations {
   dbunit    
}

dependencies {
   dbunit 'dbunit:dbunit:2.2', 
          'junit:junit:4.11', 
          'mysql:mysql-connector-java:5.1.25'
}

URLClassLoader loader = GroovyObject.class.classLoader
configurations.dbunit.each { File file -> loader.addURL(file.toURL()) }

task listJars << {    
    configurations.dbunit.each { File file -> println file.name }
}

task listTables << {    
    getConnection("mydb").eachRow('show tables') { row -> println row[0] }
}

task generateDataSet << {
    def IDatabaseConnection conn = new DatabaseConnection(getConnection("mydb").connection)
    def IDataSet fullDataSet = conn.createDataSet()
    FlatXmlDataSet.write(fullDataSet, new FileOutputStream("full.xml"))
}


static Sql getConnection(db) {
   def props = [user: 'dbuser', password: 'userpass', allowMultiQueries: 'true'] as Properties
   def url = (db) ? 'jdbc:mysql://host:3306/'.plus(db) : 'jdbc:mysql://host:3306/'
   def driver = 'com.mysql.jdbc.Driver'
   Sql.newInstance(url, props, driver)
}

What is weird to me is that all MySQL methods work well, I can get the list of tables and for instance the connection was done well so the mysql-connector-java.jar is being loaded (I think), but when I add the DBUnit stuff (import libs and the generateDataSet method) it seems the dbunit jar is not available for the script, I got the following errors:

FAILURE: Build failed with an exception.

* Where:
Build file '/home/me/tmp/dbunit/build.gradle' line: 5

* What went wrong:
Could not compile build file '/home/me/tmp/dbunit/build.gradle'.
> startup failed:
  build file '/home/me/tmp/dbunit/build.gradle': 5: unable to resolve class      org.dbunit.dataset.xml.FlatXmlDataSet
   @ line 5, column 1.
     import org.dbunit.dataset.xml.FlatXmlDataSet;
     ^

  build file '/home/me/tmp/dbunit/build.gradle': 2: unable to resolve class     org.dbunit.database.DatabaseConnection
   @ line 2, column 1.
     import org.dbunit.database.DatabaseConnection;
     ^

  build file '/home/me/tmp/dbunit/build.gradle': 3: unable to resolve class     org.dbunit.database.IDatabaseConnection
   @ line 3, column 1.
     import org.dbunit.database.IDatabaseConnection;
     ^

   build file '/home/me/tmp/dbunit/build.gradle': 4: unable to resolve class     org.dbunit.dataset.IDataSet
   @ line 4, column 1.
     import org.dbunit.dataset.IDataSet;
     ^

  4 errors

But if I call the listJars task, I got this:

:listJars
junit-4.11.jar
mysql-connector-java-5.1.25.jar
hamcrest-core-1.3.jar
xercesImpl-2.6.2.jar
xmlParserAPIs-2.6.2.jar
junit-addons-1.4.jar
poi-2.5.1-final-20040804.jar
commons-collections-3.1.jar
commons-lang-2.1.jar
commons-logging-1.0.4.jar
dbunit-2.2.jar

BUILD SUCCESSFUL

Which in my understanding means all those jars were loaded and are available for the script, am I right? or am I doing something wrong with the class loader stuff?

Thanks very much.

Was it helpful?

Solution

The GroovyObject.class.classLoader.addURL hack is not the right way to add a dependency to the build script class path. It's just sometimes necessary to get JDBC drivers to work with Groovy (long story). Here is how you add a dependency to the build script class path:

buildscript {
    repositories {
        mavenCentral()
    }

    dependencies {
        classpath "some:library:1.0"
    }
}

// library can be used in the rest of the build script

You can learn more about this in the Gradle User Guide.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top