使用mysql-connector时,在buildscript中的ClassNotFoundException

时间:2016-11-06 06:57:31

标签: gradle mysql-connector jooq build-script

当我从jooq plugin运行ClassNotFoundException任务时,我收到java.lang.ClassNotFoundException: com.mysql.jdbc.Driver消息generateTestDatabaseJooqSchema。我不知道为什么会这样,因为我在第7行的buildscript依赖项中包含了mysql:mysql-connector-java:6.0.5

buildscript {
    repositories {
        jcenter()
        mavenCentral()
    }
    dependencies {
        classpath 'mysql:mysql-connector-java:6.0.5'
        classpath 'nu.studer:gradle-jooq-plugin:2.0.0'
    }
}

plugins {
    id 'nu.studer.jooq' version '2.0.0'
    id 'org.flywaydb.flyway' version '4.0.3'
}

apply plugin: 'java'
apply plugin: 'idea'

// database connection information
def database_name = 'cloudpass-api'
def database_url = 'jdbc:mysql://localhost:3306/cloudpass-api'
def database_user = 'root'
def database_pass = 'password'

jooq {
    testDatabase(sourceSets.main) {
        jdbc {
            driver = 'com.mysql.jdbc.Driver'
            url = database_url
            user = database_user
            password = database_pass
            schema = database_name
        }
        generator {
            name = 'org.jooq.util.JavaGenerator'
            database {
                name = 'org.jooq.util.mysql.MySQLDatabase'
                inputSchema = database_name
            }
            generate {
                relations = true
                deprecated = false
                records = true
                immutablePojos = true
                fluentSetters = true
            }
            target {
                packageName = 'cloudpass.database'
            }
        }
    }
}

flyway {
    url = database_url
    user = database_user
    password = database_pass
}

repositories {
    jcenter()
    mavenCentral()
}

dependencies {
    compile 'org.slf4j:slf4j-api:1.7.21'
    compile 'mysql:mysql-connector-java:6.0.5'
    compile 'org.jooq:jooq'
    compile 'com.maxmind.geoip2:geoip2:2.8.0'
    testCompile 'junit:junit:4.12'
}

当我运行任务时,这是我得到的完整输出:

C:\Users\Thomas\Projects\cloudpass-api-java>gradle generateTestDatabaseJooqSchemaSource
Starting a Gradle Daemon (subsequent builds will be faster)
:generateTestDatabaseJooqSchemaSource
nov. 06, 2016 7:54:15 AM org.jooq.tools.JooqLogger info
INFO: Initialising properties  : C:\Users\Thomas\Projects\cloudpass-api-java\build\tmp\jooq\config.xml
nov. 06, 2016 7:54:16 AM org.jooq.tools.JooqLogger error
SEVERE: Cannot read C:\Users\Thomas\Projects\cloudpass-api-java\build\tmp\jooq\config.xml. Error : com.mysql.jdbc.Driver
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
        at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        at org.jooq.util.GenerationTool.loadClass(GenerationTool.java:591)
        at org.jooq.util.GenerationTool.run(GenerationTool.java:248)
        at org.jooq.util.GenerationTool.generate(GenerationTool.java:198)
        at org.jooq.util.GenerationTool.main(GenerationTool.java:170)

:generateTestDatabaseJooqSchemaSource FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':generateTestDatabaseJooqSchemaSource'.
> Process 'command 'C:\Program Files\Java\jdk1.8.0_101\bin\java.exe'' finished with non-zero exit value -1

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 7.831 secs

我仍然对gradle感到不舒服,所以这可能是一个菜鸟问题,所以我感谢您给予的任何帮助。

1 个答案:

答案 0 :(得分:4)

根据文档,您需要将相应的数据库驱动器类放入特殊的jooqRuntime配置,这不起作用:

  

根据您要连接的数据库,您需要输入   生成器类路径上的相应驱动程序。

     

dependencies { jooqRuntime 'postgresql:postgresql:9.1-901.jdbc4' }