Search code examples
javamavenexecutionflyway

Try to use flyway for multiple DB instances


Running the Maven flyway-plugin

mvn flyway:migrate

with this configuration:

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

I try to create number of executions like in this solution: How to use Flyway configuration to handle multiple databases

Start from one execution:

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

See exception:

[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]

Looks like flyway can't see configuration inside (Interesting that, in link, I mentioned before, it works)

Please help to create flyway multyDB integration via maven.


Solution

  • When you have multiple (or just one) <execution> in your maven plugin configuration and are attempting to run a specific execution from the command line you need to specify the execution by the execution id like so in your case

    mvn flyway:migrate@migrate-database
    

    see also: How to execute maven plugin execution directly from command line?

    Lastly, if you want a specific execution to be the default, you can give it the execution id of default-cli as explained in these maven docs. Then you can simply run mvn flyway:migrate.