[英]Maven read .properties to use in pom.xml
我试图从我的.properties文件中读取properties-maven-plugin。 Flyway(我试图使用这些属性)只是不断抛出有关db url格式错误的错误,但是如果我在pom.xml本身设置值,而不是使用从文件读取的属性,则可以正常工作。
我正在使用带有m2e插件的eclipse。
插件配置从.properties读取
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>src/main/resources/config.properties</file>
</files>
</configuration>
</execution>
</executions>
</plugin>
Flyway配置使用属性的位置
<plugin>
<groupId>com.googlecode.flyway</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<version>2.2.1</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>flyway:migrate</goal>
</goals>
</execution>
</executions>
<configuration>
<driver>${db.driver}</driver>
<url>${db.url}</url>
<user>${db.user}</user>
<password>${db.password}</password>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.26</version>
</dependency>
</dependencies>
</plugin>
config.properties位于/ src / main / resources /
# Database details
db.driver=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/dbname
db.user=username
db.pass=password
我试过通过其他几个stackoverflow线程,但没有一个解决方案似乎工作。 我是maven的新手,整个事情似乎在抛弃我,任何人都有光明之流?
基本上有两种方法可以执行Flyway迁移目标:
作为生命周期阶段的一部分:您已将Flyway插件配置为在编译阶段执行。 这意味着您只需输入mvn compile
,Flyway将与该生命周期阶段和之前所有阶段的所有其他目标一起执行。 一切正常,除了你有轻微的错误配置:目标不能作为前缀。 为了解决这个问题,你必须提供没有前缀的目标:
<goals> <goal>migrate</goal> </goals>
现在执行工作。 Flyway从properties-maven-plugin获取所有参数,因为它是在前一阶段执行的。
直接调用:如果使用mvn flyway:migrate
执行Flyway,则会独立于任何生命周期阶段调用插件。 由于没有执行阶段,因此它也不会执行properties-maven-plugin,因为它依赖于初始化阶段 - 实际上它不会设置任何参数。 这就是Flyway抱怨缺少参数的原因。
解决方案:如果您希望properties-maven-plugin与Flyway一起使用,则必须执行Flyway作为生命周期的一部分。 如果您不想在每个编译阶段调用它,您可以创建一个单独的配置文件,只有在需要使用mvn compile -PflywayMigration
迁移时才运行此配置文件:
<profiles>
<profile>
<id>flywayMigration</id>
<build>
<plugins>
<plugin>
<groupId>com.googlecode.flyway</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<version>2.2.1</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>migrate</goal>
</goals>
</execution>
</executions>
<configuration>
<driver>${db.driver}</driver>
<url>${db.url}</url>
<user>${db.user}</user>
<password>${db.password}</password>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.26</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</profile>
</profiles>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.