我创建了一个简单的 spring-boot Gradle 项目。我希望在构建项目时通过 SQL 文件中的 flyway 插件创建我的数据库。下面是我的代码片段。该项目已成功构建,但未在 mydb1 数据库中创建 SQL 文件中提到的表。
build.gradle 文件
buildscript {
ext {
springBootVersion = '2.0.1.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
plugins {
id "org.flywaydb.flyway" version "5.0.7"
}
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
group = 'com.flywayex'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
runtime('com.h2database:h2')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
task createDatabase() {
doLast {
println 'Hello Gradle to copy'
flyway {
println "Flyway: "
driver = 'org.h2.Driver'
url = 'jdbc:h2:mem:mydb1'
user = 'sa'
locations = ['filesystem:db/migration']
println "Flyway: $locations"
}
}
}
build.dependsOn createDatabase
v1.init1.sql 文件放在 resources/db/migration 下
CREATE TABLE users (
id bigint(20) NOT NULL AUTO_INCREMENT,
username varchar(100) NOT NULL,
first_name varchar(50) NOT NULL,
last_name varchar(50) DEFAULT NULL,
PRIMARY KEY (id),
UNIQUE KEY UK_username (username)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
我错过了什么?