我编写了一个java类,它使用从taskdef操作类路径加载的依赖jar Flyway-core-4.0.3.jar并调用flyway jar中的方法,以下是导致以下问题的代码部分:问题
public static boolean executeFlywayMigrate(String connectionUrl, String username, String password,
String scriptLocation, String[] databaseNames, Map<String, String> componentProperties,
boolean validateOnMigrate)
throws SQLException {
LOGGER.info("Database schemas for migration are :" + Arrays.toString(databaseNames));
try {
Flyway flyway = new Flyway();
flyway.setLocations(scriptLocation);
flyway.setSchemas(databaseNames);
flyway.setBaselineOnMigrate(true);
flyway.setDataSource(connectionUrl, username, password);
if (componentProperties != null) {
flyway.setPlaceholders(componentProperties);
}
flyway.setValidateOnMigrate(validateOnMigrate);
flyway.repair();
flyway.migrate();
} catch (RuntimeException e) {
LOGGER.error("Database migration failed.", e);
throw new SQLException(e);
}
我正在使用taskdef操作用ant触发该类。我已经给出了 taskdef 操作的类路径引用。
<taskdef name="ExecuteMigrationScripts" classname="com.install.common.db.action.ExecuteMigrationScripts"
classpathref="class.dependencies" loaderref="class.dependencies.loader"/>
<ExecuteMigrationScripts/>
仅当 Flyway 尝试搜索 db jar 时才会出现此问题(在我的情况下,我已尝试使用 mysql-connector-java-5.1.39-bin.jar 和 sqljdbc41.jar,但出现了相同的问题)。
注意:如果我触发与 taskdef 操作不同的类,该类尝试连接到数据库并创建示例数据库模式,则效果很好,并且可以从 taskdef 操作中提到的类路径加载相同的数据库 jar,但是仅当 Flyway 尝试查找数据库 jar 时才会出现此问题。
如果我在这里遗漏了什么,请帮助我。
最佳答案
即使在调用 taskdef 操作时将所有驱动程序 jar 添加到类路径中,flyway 仍无法获取 jdbc 驱动程序的类路径。
但是使用命令“ant -lib path_to_the_jdbc_driver_jars”设置类路径解决了我的问题。
关于java - org.flywaydb.core.api.FlywayException : Unable to instantiate JDBC driver when my java class is triggered from ant taskdef,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43485687/