简体   繁体   English

Gradle加载mysql-connector jar但没有dbunit jar作为外部依赖项,为什么?

[英]Gradle loads mysql-connector jar but no dbunit jar as external dependencies, why?

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. 首先,我是Gradle和Groovy的新手,出于学习目的,我正在与他们和DBUnit一起玩。

I tried the code listed below, my goal is to generate a dataset getting the data from a mysql db. 我尝试了下面列出的代码,我的目标是生成一个从mysql数据库获取数据的数据集。

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: 让我感到奇怪的是,所有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

But if I call the listJars task, I got this: 但是,如果我调用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

Which in my understanding means all those jars were loaded and are available for the script, am I right? 以我的理解,这意味着所有这些jar都已加载并且可用于脚本,对吗? or am I doing something wrong with the class loader stuff? 还是我在类加载器方面做错了什么?

Thanks very much. 非常感谢。

The GroovyObject.class.classLoader.addURL hack is not the right way to add a dependency to the build script class path. GroovyObject.class.classLoader.addURL hack不是向构建脚本类路径添加依赖项的正确方法。 It's just sometimes necessary to get JDBC drivers to work with Groovy (long story). 有时仅需要使JDBC驱动程序与Groovy一起使用(长话短说)。 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 . 您可以在《 Gradle用户指南》中了解有关此内容的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM