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

时间:2019-02-18 16:00:23

标签: spring spring-boot flyway multi-module

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

project
|
|-> application
|-> approval-management
|-> database
|-> security
...

我的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>

我的迁移脚本位于文件夹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

现在通过这种配置,我希望在运行我的应用程序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!

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

PS。在以前的项目中(没有模块故障,但也有配置文件),完全相同的配置也很好

0 个答案:

没有答案