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

maba
  • 47,113
  • 10
  • 108
  • 118
Garry
  • 678
  • 1
  • 9
  • 21

6 Answers6

40

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>
dan carter
  • 4,158
  • 1
  • 33
  • 34
Sébastien Le Callonnec
  • 26,254
  • 8
  • 67
  • 80
  • 6
    For future reference, this works great, but you need to invoke mvn initialize explicitly before calling the flay plugin e.g. `mvn initialize flyway:migrate` – pmckeown Apr 20 '13 at 00:55
  • 2
    what if I have different properties files according to profiles? like for dev, abc-dev.properties, for prod abc-prod.properties – usr_11 Apr 24 '18 at 07:23
  • Hey where should we define the properties in pom.xml(which u mentioned it as " ${jdbc.driver} ${jdbc.url} ${jdbc.user}"} – shaik Aug 02 '18 at 13:52
5

in version 3.0 you have to use configFile like :

<configFile>src/main/resources/db/config/flyway.properties</configFile>
abk
  • 106
  • 1
  • 4
4

In my opinion, the best and the most flexible approach is to:

a) use profiles and filtering - keep all configuration properties for specific profile (development, test, etc.), e.g. in development.properties:

jdbc.url=jdbc:mysql://127.0.0.1:3306/testdb?useSSL=false
jdbc.user=testuser
jdbc.password=testpass
jdbc.driver=com.mysql.jdbc.Driver

Then, in your pom file (possibly in root pom) define a profile, e.g:

...
<profiles>
    <profile>
        <id>development</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <build>
            <filters>
                <filter>../filters/development.properties</filter>
            </filters>
        </build>
        ...

here you can see that development profile is activated by default. If you want to use another profile set it with

-p [profile-id]


b) set flyway.properties with filtered values - your flyway.properties should sit e.g. in src/main/resources and the values should be used from the parameters defined in the profile properties file:

flyway.driver = ${jdbc.driver}
flyway.url = ${jdbc.url}
flyway.user = ${jdbc.user}
flyway.password = ${jdbc.password}

c) reference flyway.properties from build directory - use simple plugin configuration (I really like clean poms):

...
    <build>
        <resources>
            <!-- This way we instruct maven to inject values from filters into the resources -->
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.flywaydb</groupId>
                <artifactId>flyway-maven-plugin</artifactId>
                <configuration>
                    <configFile>${project.build.directory}/classes/flyway.properties</configFile>
                    <locations>
                        <location>classpath:migration/mysql</location>
                    </locations>
                </configuration>
            </plugin>
        </plugins>
    </build>
    ...

Don't forget to enable filtering in resources as shown in many examples here. My flyway-maven-plugin version is 3.2.1 and it is managed in pluginManagement in parent pom, therefore version is not visible here. I also use explicit sql scripts with locations configuration.

user2310395
  • 297
  • 3
  • 6
2

There are several ways to deal with this. One approach is to do it the other way around.

That means that the properties to use are saved as properties inside the pom.xml and that the file abc.properties only has placeholders that will be filled in at build time.

I will show you how it can be configured.

This is what the pom.xml will look like:

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

    <groupId>com.stackoverflow</groupId>
    <artifactId>Q12619446</artifactId>
    <version>1.0-SNAPSHOT</version>

    <name>${project.artifactId}-${project.version}</name>

    <properties>
        <jdbc.driver>com.mysql.jdbc.Driver</jdbc.driver>
        <jdbc.url>jdbc:mysql://127.0.0.1:3306/db_abc</jdbc.url>
        <jdbc.user>db_user</jdbc.user>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>com.googlecode.flyway</groupId>
                <artifactId>flyway-maven-plugin</artifactId>
                <version>1.7</version>
                <configuration>
                    <driver>${jdbc.driver}</driver>
                    <url>${jdbc.url}</url>
                    <user>${jdbc.user}</user>
                </configuration>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>
</project>

And this will be your src/main/resources/abc.properties (use the key names of your choice):

jdbcDriver = ${jdbc.driver}
jdbcUrl = ${jdbc.url}
jdbcUser = ${jdbc.user}

After the build the target/classes/abc.properties will look like this:

jdbcDriver = com.mysql.jdbc.Driver
jdbcUrl = jdbc:mysql://127.0.0.1:3306/db_abc
jdbcUser = db_user

As stated this is only one of several ways to do it. It might not suit your exact needs but it could.

maba
  • 47,113
  • 10
  • 108
  • 118
  • 3
    thanks for quick reply But the main issue is, i don't want to declare properties in POM.XML. – Garry Sep 27 '12 at 11:43
2

Garry,

there is one more way not to place database connection parameters to your pom-file. In particular, one can add them to settings.xml file in .m2 sub-folder of the user's folder [1]. The benefit is that the db paraneters do not live in the project folder and are not transmitted to the repository, so each developer may use her own settings.

The example of the settings file could look like the example below.

<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
  <profiles>
    <profile>
      <id>fw</id>
      <properties>
        <flyway.url>jdbc:oracle:thin:@//localhost:1521/xe</flyway.url>
        <flyway.user>Your login</flyway.user>
        <flyway.password>Your password</flyway.password>
      </properties>
    </profile>
  </profiles>
  <activeProfiles>
    <activeProfile>fw</activeProfile>
  </activeProfiles>
</settings>

After that your can modify your pom-file according to the following snippet.

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    ...
    <dependencies>
        ...
    </dependencies>

    <profiles>
        <profile>
            <id>fw</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.flywaydb</groupId>
                        <artifactId>flyway-maven-plugin</artifactId>
                        <version>3.2.1</version>
                        <configuration>
                            <url>${flyway.url}</url>
                            <user>${flyway.user}</user>
                            <password>${flyway.password}</password>
                        </configuration>
                        <dependencies>
                            <dependency>
                                <groupId>com.oracle</groupId>
                                <artifactId>ojdbc7</artifactId>
                                <version>12.1.0.1.0</version>
                            </dependency>
                        </dependencies>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
</project>

In the example above Oracle's driver is used, but you can replace it with the required one.

To print current settings execute the following.

mvn help:effective-settings
javaeeeee
  • 681
  • 8
  • 13
  • OK, your example works BUT what shoudl I do to load values from my application-local.yml or application-dev.yml file instead of set values in pom (hard coded)? – Matley Jan 07 '19 at 00:25
0

In version 3.x you have configFile option

By default- flyway.properties in the same directory as the project POM.

You can add external property file in configuration section of pom or can be pass when running maven goal example- command line-

mvn <goal> -Dflyway.configFile=myConfig.properties 

Pom file-

<plugin>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<version>3.2.1</version>
<configuration>
    <driver/>
    <url/>
    <user/>
    <password/>
    <baselineVersion>1.0</baselineVersion>
    <baselineDescription>Base Migration</baselineDescription>
    <skip>false</skip>
    <configFile>myConfig.properties</configFile>
    </configuration>
</plugin>
Mr.Pramod Anarase
  • 1,454
  • 2
  • 15
  • 19