尝试将flyway用于多个数据库实例

时间:2016-10-03 19:21:29

标签: java maven execution flyway

运行Maven flyway-plugin

mvn flyway:migrate

使用此配置:

    <plugin>
      <groupId>org.flywaydb</groupId>
      <artifactId>flyway-maven-plugin</artifactId>
      <version>4.0.3</version>
      <configuration>
        <driver>com.mysql.jdbc</driver>
        <url>jdbc:mysql://localhost:3306/schema2?createDatabaseIfNotExist=true</url>
        <user>root</user>
        <password>root</password>
      </configuration>
    </plugin>

我尝试在此解决方案中创建多个执行: How to use Flyway configuration to handle multiple databases

从一次执行开始:

    <plugin>
      <groupId>org.flywaydb</groupId>
      <artifactId>flyway-maven-plugin</artifactId>
      <version>4.0.3</version>
      <executions>
        <execution>
          <id>migrate-database</id>
          <phase>compile</phase>
          <goals>
            <goal>migrate</goal>
          </goals>
          <configuration>
            <driver>com.mysql.jdbc</driver>
            <url>jdbc:mysql://localhost:3306/schema2?createDatabaseIfNotExist=true</url>
            <user>root</user>
            <password>root</password>
          </configuration>
        </execution>
      </executions>
    </plugin>

见例外:

[ERROR] Failed to execute goal org.flywaydb:flyway-maven-plugin:4.0.3:migrate (default-cli) on project UrbanLife: org.flywaydb.core.api.FlywayException: Unable to connect to the database. Configure the url, user and password! -> [Help 1]

看起来flyway无法看到里面的配置 (有趣的是,在链接中,我之前提到过,它有效)

请帮助通过maven创建flyway multyDB集成。

2 个答案:

答案 0 :(得分:3)

如果你的maven插件配置中有多个(或只有一个)<execution>,并且正在尝试从命令行运行特定的执行,则需要通过执行id指定执行,如此在你的情况下

mvn flyway:migrate@migrate-database

另见:How to execute maven plugin execution directly from command line?

最后,如果您希望特定执行成为默认执行,则可以按these maven docs中的说明为其指定执行ID default-cli。然后,您只需运行mvn flyway:migrate

答案 1 :(得分:1)

驱动程序的FQN不正确,应为com.mysql.jdbc.Driver,或者您也可以将其删除,因为它会从网址自动检测到。

您还需要添加驱动程序作为插件的依赖项,方法是添加

<plugin>
    ...
    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.39</version>
        </dependency>
    </dependencies>
</plugin>