0

请给我一些关于我在这里做错的事情。首先,我是 Gradle 和 Groovy 的新手,出于学习目的,我正在使用它们和 DBUnit。

我尝试了下面列出的代码,我的目标是生成一个从 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)
}

对我来说奇怪的是所有 MySQL 方法都运行良好,我可以获得表列表,例如连接做得很好,所以 mysql-connector-java.jar 正在加载(我认为),但是当我添加DBUnit 的东西(导入库和 generateDataSet 方法)似乎 dbunit jar 不适用于脚本,我收到以下错误:

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

但是如果我调用 listJars 任务,我得到了这个:

: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

在我的理解中,这意味着所有这些 jar 都已加载并可用于脚本,对吗?还是我对类加载器的东西做错了什么?

非常感谢。

4

1 回答 1

1

GroovyObject.class.classLoader.addURLhack 不是将依赖项添加到构建脚本类路径的正确方法。只是有时需要让 JDBC 驱动程序与 Groovy 一起工作(长篇大论)。以下是向构建脚本类路径添加依赖项的方法:

buildscript {
    repositories {
        mavenCentral()
    }

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

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

您可以在Gradle 用户指南中了解更多相关信息。

于 2013-05-31T18:22:56.420 回答