Running flyway commands from command line using spring boot properties for maven

Viewed 1183

I have a spring boot project using maven that I've included flyway in:

pom.xml:

    <dependency>
        <groupId>org.flywaydb</groupId>
        <artifactId>flyway-core</artifactId>
        <version>6.5.0</version>
    </dependency>

and application.properties:

#LOCAL
spring.datasource.url=jdbc:postgresql://localhost:5432/theDatabase
spring.datasource.username=theRightUser
spring.datasource.password=theRightPassword

and it works as expected when I run the application.

However, I'm trying to run mvn flyway:clean from the command line, and it doesn't seem to recognize the configuration correctly:

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

I tried adding spring.flyway properties (user/pass/url) in the application.properties file but it gave me the same error. What do I need to do to get flyway to read from the application.properies like it does when the application runs normally?


EDIT: I have made slight progress: I was able to reference my application.properties as a flyway config file by adding this to the pom.xml:

        <plugin>
            <groupId>org.flywaydb</groupId>
            <artifactId>flyway-maven-plugin</artifactId>
            <version>6.5.0</version>
            <configuration>
                <configFiles>${project.basedir}/src/main/resources/application.properties</configFiles>
            </configuration>
        </plugin>

So now in that file, I have flyway.url, flyway.user and flyway.password. This allows me to run flyway goals from the command line, but is not totally the solution I want. I am looking into using this plugin to try and read the properties into the pom.xml file, and then using those values in the flyway-maven-plugin's <configuration> area.

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>properties-maven-plugin</artifactId>
            <version>1.0.0</version>
            <executions>
                <execution>
                    <phase>initialize</phase>
                    <goals>
                        <goal>read-project-properties</goal>
                    </goals>
                    <configuration>
                        <files>
                            <file>${project.basedir}/src/main/resources/application.properties</file>
                        </files>
                    </configuration>
                </execution>
            </executions>
        </plugin>

which would allow me to do this:

        <plugin>
            <groupId>org.flywaydb</groupId>
            <artifactId>flyway-maven-plugin</artifactId>
            <version>6.5.0</version>
            <configuration>
                <url>${spring.datasource.url}</url>
                <user>${spring.datasource.username}</user>
                <password>${spring.datasource.password}</password>
            </configuration>
        </plugin>
1 Answers

When you run flyway as maven goal it will not pick up the properties from the application.properties, instead it will use the configuration provided by the flyway-maven-plugin, you can configure the flyway-maven-plugin in the following way -

Add the following plugin to pom.xml -

<plugin>
    <groupId>org.flywaydb</groupId>
    <artifactId>flyway-maven-plugin</artifactId>
    <version>6.5.0</version>
</plugin>

Then we will configure the flyway-maven-plugin, the Flyway Maven plugin can be configured in a wide variety of following ways (most convenient),

Configuration section of the plugin

The easiest way is to simply use the plugin’s configuration section in your pom.xml:

<plugin>
    <groupId>org.flywaydb</groupId>
    <artifactId>flyway-maven-plugin</artifactId>
    <version>6.5.0</version>
    <configuration>
        <driver>org.hsqldb.jdbcDriver</driver>
        <url>jdbc:hsqldb:file:${project.build.directory}/db/flyway_sample;shutdown=true</url>
        <user>SA</user>
        <password>mySecretPwd</password>
        <connectRetries>10</connectRetries>
        <initSql>SET ROLE 'myuser'</initSql>
        <schemas>
            <schema>schema1</schema>
            <schema>schema2</schema>
            <schema>schema3</schema>
        </schemas>
        <callbacks>
            <callback>com.mycompany.project.CustomCallback</callback>
            <callback>com.mycompany.project.AnotherCallback</callback>
        </callbacks>
        <skipDefaultCallbacks>false</skipDefaultCallbacks>
        <cleanDisabled>false</cleanDisabled>
        <skip>false</skip>
        <configFiles>
            <configFile>myConfig.conf</configFile>
            <configFile>other.conf</configFile>
        </configFiles>
        <workingDirectory>/my/working/dir</workingDirectory>
    </configuration>
</plugin>

Maven properties

To make it easy to work with Maven profiles and to logically group configuration, the Flyway Maven plugin also supports Maven properties, update the properties section in pom.xml as follows:

<project>
    ...
    <properties>
        <!-- Properties are prefixed with flyway. -->
        <flyway.user>myUser</flyway.user>
        <flyway.password>mySecretPwd</flyway.password>

        <!-- List are defined as comma-separated values -->
        <flyway.schemas>schema1,schema2,schema3</flyway.schemas>

        <!-- Individual placeholders are prefixed by flyway.placeholders. -->
        <flyway.placeholders.keyABC>valueXYZ</flyway.placeholders.keyABC>
        <flyway.placeholders.otherplaceholder>value123</flyway.placeholders.otherplaceholder>
    </properties>
    ...
</project>

External Configuration File

Another way is to create a separate .properties file, the default configuration file name is flyway.properties and it should reside in the same directory as the pom.xml file. Encoding is specified by flyway.encoding (Default is UTF-8):

flyway.user=databaseUser
flyway.password=databasePassword
flyway.schemas=schemaName
...

If you are using any other name (e.g customConfig.properties) as the configuration file, then it should be specified explicitly when invoking the Maven command:

$ mvn <goals> -Dflyway.configFile=customConfig.properties

After configuring your flyway-maven-plugin, with the desired configuration, we will be able to execute flyway maven goals from command line.

You can do further reading here.

Hope this helps!

Related