无法使用gradle

时间:2018-04-17 13:13:59

标签: gradle build.gradle flyway gradle-plugin spring-boot-gradle-plugin

我创建了一个简单的spring-boot Gradle项目。我想在构建项目时通过SQL文件中的flyway插件创建我的数据库。以下是我的代码段。项目成功构建,但SQL文件中提到的表不是在mydb1数据库中创建的。

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

放置在resources / db / migration

下的V1.init1.sql文件
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;

我错过了什么?

1 个答案:

答案 0 :(得分:1)

我怀疑它无法找到您的迁移脚本。尝试:

locations "filesystem:${project.projectDir}/src/main/resources/db/migration"

locations "classpath:db/migration"