【发布时间】:2018-10-24 04:41:59
【问题描述】:
我正在尝试创建一个简单的 gradle 项目,该项目应该仅用于使用 flywayDb 使用存储在另一个目录中的 JAR 文件中的迁移脚本来迁移数据库。该目录与 gradle 构建没有任何关系。它是使用项目中的另一种机制创建和填充的。
(至少对我而言)显而易见的脚本不起作用:
[...]
// flyway plugin
apply plugin: 'org.flywaydb.flyway'
// dependencies
dependencies {
compile fileTree(dir: '/workspace/WEB-INF/lib', include: '*.jar')
}
// flyway configuration
flyway {
[...]
locations = [ 'classpath:/META-INF/db' ]
}
调用 flyway 迁移任务时,我得到的只是
Unable to resolve location classpath:META-INF/db
并且没有任何东西被迁移。如果我提取 JAR 文件并使用 filesystem 位置,它可以正常工作。所以我猜flyway使用的类路径不包括JAR文件。
这让我想到了我的问题:flyway 使用什么类路径来搜索数据库脚本?以及如何将文件系统中的简单 JAR 文件添加到其中?
编辑:
注释后完成 build.gradle:
// add JDBC drivers (for flyway) and flyway itself to classpath
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath group: 'gradle.plugin.com.boxfuse.client', name: 'gradle-plugin-publishing', version: '5.0.7'
classpath group: 'mysql', name: 'mysql-connector-java', version: '5.1.42'
}
}
// flyway plugin
apply plugin: 'org.flywaydb.flyway'
// configurations
configurations {
flywayClasspath
}
// dependencies
dependencies {
flywayClasspath fileTree(dir: '/workspace/WEB-INF/lib', include: '*.jar')
flywayClasspath files('/workspace/WEB-INF/classes')
}
// get needed data for connection
def dbPassword = System.getenv('MYSQL_ROOT_PASSWORD')
def dbName = System.getenv('PROJECTNAME')
// flyway configuration
flyway {
configurations = [ 'flywayClasspath' ]
url = "jdbc:mysql://localhost/${dbName}?rewriteBatchedStatements=true&useUnicode=true&characterEncoding=utf-8&useLegacyDatetimeCode=false&serverTimezone=Europe/Berlin&useSSL=false"
driver = 'com.mysql.jdbc.Driver'
user = 'root'
password = dbPassword
locations = [ "classpath:/META-INF/db/mysql" ]
table = 'schema_version'
outOfOrder = true
ignoreMissingMigrations = true
}
【问题讨论】:
标签: database gradle database-migration flyway