FlywayDB 无法找到 Postgresql 驱动程序

FlywayDB is unable to find Postgresql Driver

我正在尝试在 Spigot/Bukkit MC 插件中设置 Flyway。目前每次执行我们的 SQL 代码时,我都会收到错误 `

[10:37:14 WARN]: org.flywaydb.core.api.FlywayException: Unable to instantiate JDBC driver: org.postgresql.Driver => Check whether the jar file is present
[10:37:14 WARN]:        at org.flywaydb.core.internal.jdbc.DriverDataSource.<init>(DriverDataSource.java:180)
[10:37:14 WARN]:        at org.flywaydb.core.internal.jdbc.DriverDataSource.<init>(DriverDataSource.java:144)
[10:37:14 WARN]:        at org.flywaydb.core.api.configuration.ClassicConfiguration.setDataSource(ClassicConfiguration.java:1341)
[10:37:14 WARN]:        at org.flywaydb.core.api.configuration.FluentConfiguration.dataSource(FluentConfiguration.java:803)
[10:37:14 WARN]:        at com.mclockup.core.database.SqlWrapper.<init>(SqlWrapper.java:42)
[10:37:14 WARN]:        at com.mclockup.core.database.SqlWrapper.initialize(SqlWrapper.java:78)
[10:37:14 WARN]:        at com.mclockup.core.LockupCore.onEnable(LockupCore.java:22)
[10:37:14 WARN]:        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:263)
[10:37:14 WARN]:        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:337)
[10:37:14 WARN]:        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:435)
[10:37:14 WARN]:        at org.bukkit.craftbukkit.v1_15_R1.CraftServer.enablePlugin(CraftServer.java:470)
[10:37:14 WARN]:        at org.bukkit.craftbukkit.v1_15_R1.CraftServer.enablePlugins(CraftServer.java:384)
[10:37:14 WARN]:        at org.bukkit.craftbukkit.v1_15_R1.CraftServer.reload(CraftServer.java:884)
[10:37:14 WARN]:        at org.bukkit.Bukkit.reload(Bukkit.java:654)
[10:37:14 WARN]:        at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:54)
[10:37:14 WARN]:        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:159)
[10:37:14 WARN]:        at org.bukkit.craftbukkit.v1_15_R1.CraftServer.dispatchCommand(CraftServer.java:752)
[10:37:14 WARN]:        at org.bukkit.craftbukkit.v1_15_R1.CraftServer.dispatchServerCommand(CraftServer.java:714)
[10:37:14 WARN]:        at net.minecraft.server.v1_15_R1.DedicatedServer.handleCommandQueue(DedicatedServer.java:469)
[10:37:14 WARN]:        at net.minecraft.server.v1_15_R1.DedicatedServer.b(DedicatedServer.java:431)
[10:37:14 WARN]:        at net.minecraft.server.v1_15_R1.MinecraftServer.a(MinecraftServer.java:1112)
[10:37:14 WARN]:        at net.minecraft.server.v1_15_R1.MinecraftServer.run(MinecraftServer.java:934)
[10:37:14 WARN]:        at java.lang.Thread.run(Thread.java:748)
[10:37:14 WARN]: Caused by: org.flywaydb.core.api.FlywayException: Unable to instantiate class org.postgresql.Driver : org.postgresql.Driver
[10:37:14 WARN]:        at org.flywaydb.core.internal.util.ClassUtils.instantiate(ClassUtils.java:59)
[10:37:14 WARN]:        at org.flywaydb.core.internal.jdbc.DriverDataSource.<init>(DriverDataSource.java:176)
[10:37:14 WARN]:        ... 22 more
[10:37:14 WARN]: Caused by: java.lang.ClassNotFoundException: org.postgresql.Driver
[10:37:14 WARN]:        at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
[10:37:14 WARN]:        at java.lang.ClassLoader.loadClass(ClassLoader.java:419)
[10:37:14 WARN]:        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
[10:37:14 WARN]:        at java.lang.ClassLoader.loadClass(ClassLoader.java:352)
[10:37:14 WARN]:        at java.lang.Class.forName0(Native Method)
[10:37:14 WARN]:        at java.lang.Class.forName(Class.java:348)
[10:37:14 WARN]:        at org.flywaydb.core.internal.util.ClassUtils.instantiate(ClassUtils.java:57)
[10:37:14 WARN]:        ... 23 more

` 我不认为这是一个依赖性问题,因为我在 build.gradle

中包含了 Flyway 和 postgresql

    plugins {
        id 'java'
        id 'maven-publish'
        id 'signing'
    }

    group = 'com.mclockup'
    version = '1.0.0-SNAPSHOT'
    description = 'Core functionality used everywhere in Lockup'

    archivesBaseName = 'core'
    sourceCompatibility = '1.8'

    repositories {
        mavenLocal()
        mavenCentral()
    }

    dependencies {
        // Spigot
        implementation group: 'org.spigotmc', name: 'spigot-api', version: '1.15.2-R0.1-SNAPSHOT'

        // Postgres
        compile group: 'org.postgresql', name: 'postgresql', version: '42.2.12'

        // Flyway
        compile group: 'org.flywaydb', name: 'flyway-core', version: '6.4.1'

        // MyBatis
        compile group: 'org.mybatis', name: 'mybatis', version: '3.5.4'

        // Log4J
        compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.13.2'
        compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.13.2'
    }

    jar {
        from {
            configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
        }
    }

    javadoc {
        options.links += "https://hub.spigotmc.org/javadocs/spigot/"
    }

    task sourcesJar(type: Jar) {
        from sourceSets.main.allJava
        archiveClassifier.set("sources")
    }

    task javadocJar(type: Jar, dependsOn: javadoc) {
        classifier 'javadoc'
        from javadoc.destinationDir
    }

它在我们的第 2 行出现错误


    Flyway flyway = Flyway.configure()
            .dataSource(getJdbcString(), DATABASE_USERNAME, DATABASE_PASSWORD)
            .load();
    flyway.migrate();

我可以确认 jdbcstring 和 username/password 是正确的,因为我能够通过 DriverManager 建立连接,但 Flyway 似乎找不到驱动程序。另外,我可以在我的输出 jar 中看到驱动程序,它位于 blah.jar\org\postgresql\Driver.class

感谢任何帮助!!谢谢

想通了! 事实证明,我们使用的 spigot 服务器软件使用与主 java 线程不同的 class 加载程序,因此在加载 jdbc 驱动程序时会导致问题。我们通过传递正确的 class 加载程序来解决此问题。

Flyway flyway = Flyway.configure(LockupCore.getInstance().getPublicClassLoader())