Can I use property file in maven pom.xml for flywa

2019-01-22 12:56发布

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

6条回答
Root(大扎)
2楼-- · 2019-01-22 13:21

in version 3.0 you have to use configFile like :

<configFile>src/main/resources/db/config/flyway.properties</configFile>
查看更多
虎瘦雄心在
3楼-- · 2019-01-22 13:27

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
查看更多
孤傲高冷的网名
4楼-- · 2019-01-22 13:28

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>
查看更多
聊天终结者
5楼-- · 2019-01-22 13:30

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>
查看更多
\"骚年 ilove
6楼-- · 2019-01-22 13:36

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.

查看更多
Anthone
7楼-- · 2019-01-22 13:41

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.

查看更多
登录 后发表回答