如何为多模块项目配置 Flyway?

Akk*_*rek 7 spring multi-module flyway spring-boot

我有一个多模块项目,其中一个模块负责处理我的迁移,所以我的项目结构如下所示:

project
|
|-> application
|-> approval-management
|-> database
|-> security
...
Run Code Online (Sandbox Code Playgroud)

pom.xml的数据库模块如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
    <artifactId>application</artifactId>
    <groupId>com.test</groupId>
    <version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>database</artifactId>


<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
</properties>


<profiles>
    <profile>
        <id>local</id>
        <properties>
            <activatedProperties>local</activatedProperties>
        </properties>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
    <profile>
        <id>test</id>
        <properties>
            <activatedProperties>test</activatedProperties>
        </properties>
    </profile>
</profiles>


<dependencies>
    <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-actuator -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
        <version>2.1.2.RELEASE</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>2.1.2.RELEASE</version>
    </dependency>


    <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <version>2.1.2.RELEASE</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>5.1.4.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>1.4.197</version>
        <scope>compile</scope> <!-- use compile, as we also use H2 locally to run it -->
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.3.7.Final</version>
    </dependency>
    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>ojdbc7</artifactId>
        <version>12.1.0.2</version>
    </dependency>
    <dependency>
        <groupId>org.flywaydb</groupId>
        <artifactId>flyway-core</artifactId>
        <version>5.2.4</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

<build>
    <finalName>application</finalName>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>8</source>
                <target>8</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.flywaydb</groupId>
            <artifactId>flyway-maven-plugin</artifactId>
            <version>5.2.4</version>
        </plugin>
    </plugins>
</build>
Run Code Online (Sandbox Code Playgroud)

我的迁移脚本在文件夹中:database.serc.main.resources.db.migration 看起来像这样:(V1.0.1.001__TABLE.sql我的架构是空的)

现在因为我在这种情况下使用配置文件,我将使用默认的本地配置文件,它使用 application-local.properties

application-local.properties 在模块数据库中如下:

spring.h2.console.settings.trace=false
spring.h2.console.enabled=true
spring.datasource.platform=h2
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.generate-ddl=false
spring.jpa.hibernate.ddl-auto=none
spring.datasource.initialize=false
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

  ## FlyWay
flyway.enabled=true
flyway.baselineOnMigrate=false
flyway.table=FLYWAY_SCHEMA_HISTORY
flyway.locations=classpath:db/migration
flyway.h2ConsoleEnabled=true
flyway.h2Port=8011

flyway.sqlMigrationPrefix=V
flyway.sqlMigrationSeparator=__
flyway.sqlMigrationSuffix=.sql
flyway.validateOnMigrate=true
Run Code Online (Sandbox Code Playgroud)

现在根据这个配置,我希望在运行我的应用程序时会自动触发并运行迁移,但它不会(甚至在日志中的任何地方都看不到 Flyway)。

此外,当我尝试使用以下命令从命令行运行迁移时: mvn compile flyway:migrate 这会导致错误:

Failed to execute goal org.flywaydb:flyway-maven-plugin:5.2.4:migrate (default-cli) on project application: org.flywaydb.core.api.FlywayException: Unable to connect to the database. Configure the url, user and password!
Run Code Online (Sandbox Code Playgroud)

现在,由于所有内容都在 application-local.properties 下配置,我想我的属性没有以某种方式被提取,但我看不出问题出在哪里,请看一看并告诉我哪里出错了?

附注。在以前的项目中(没有模块分解,但也有配置文件)完全相同的配置工作得很好