Search code examples
mavenpom.xmlflyway

Can I use property file in maven pom.xml for flyway configuration


<plugin>
    <groupId>com.googlecode.flyway</groupId>
    <artifactId>flyway-maven-plugin</artifactId>
    <version>1.7</version>
    <configuration>
        <driver>com.mysql.jdbc.Driver</driver>
        <url>jdbc:mysql://127.0.0.1:3306/db_abc</url>
        <user>db_user</user>
        <sqlMigrationPrefix>V</sqlMigrationPrefix>
    </configuration>
</plugin>

I don't want to mention driver, url and user here. I already have a abc.property on src/main/resources. How can use that file here?


Solution

  • Have a look at the properties-maven-plugin. It allows you to read properties from a file to then use them in your pom.

    Add the following plugin defintion:

      <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>src/main/resources/abc.properties</file>
              </files>
            </configuration>
          </execution>
        </executions>
      </plugin>
    

    If abc.properties contains:

    jdbc.driver = com.mysql.jdbc.Driver
    jdbc.url = jdbc:mysql://127.0.0.1:3306/db_ab
    jdbc.user = db_user
    

    You can then use the properties as follows:

    <!-- language: xml -->
    
    <driver>${jdbc.driver}</driver>
    <url>${jdbc.url}</url>
    <user>${jdbc.user}</user>