我不太明白它如何使用。文件中定义了一个属性。我尝试使用maven属性插件来读取它并保存。该属性在liquibase插件中使用:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-1</version>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>src/main/resources/properties/app.properties</file>
</files>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<version>2.0.5</version>
<configuration>
<propertyFile>src/main/resources/db/config/${env}-data-access.properties</propertyFile>
<changeLogFile>src/main/resources/db/changelog/db.changelog-master.xml</changeLogFile>
<migrationSqlOutputFile>src/main/resources/db/gen/migrate.sql</migrationSqlOutputFile>
<!--<logging>debug</logging>-->
<logging>info</logging>
<promptOnNonLocalDatabase>false</promptOnNonLocalDatabase>
<!--<verbose>false</verbose>-->
<dropFirst>true</dropFirst>
</configuration>
</plugin>
根据文档,为了读取属性并保存它,我必须运行:
mvn properties:read-project-properties
。但在这种情况下我收到以下错误:[错误] 无法在项目 SpringWebFlow 上执行目标 org.codehaus.mojo:properties-maven-plugin:1.0-alpha-2:read-project-properties (default-cli): 目标 org.codehaus.mojo:properties-maven-plugin:1.0-alpha-2:read-project-properties 的参数"file"丢失或无效 -> [帮助 1]
我更改了 pom.xml,删除了 <execution>
部分并移动了 <configuration>
部分:
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-1</version>
<configuration>
<files>
<file>src/main/resources/properties/app.properties</file>
</files>
</configuration>
好的。现在,当我运行 mvnproperties:read-project-properties 时,错误消失了。但在这种情况下,属性(property)保存在哪里呢?因为当我启动以下 Maven 目标时:
mvn liquibase:update
我可以看到 ${env} 属性未定义。 Liquibase 尝试使用 src/main/resources/db/config/${env}-data-access.properties
文件。
我做错了什么?如何从文件中读取属性,以便可以从不同的 Maven 插件访问它?
最佳答案
问题是“mvn liquibase:update”是一个特殊的插件目标,不是 Maven 生命周期的一部分。因此它永远不会通过初始化阶段,因此属性插件不会被执行。
以下内容将起作用
mvn initialize liquibase:update
一种解决方案是在 maven 生命周期阶段之一(例如编译、打包...)中调用 liquibase:update,但随后它将在每次构建时执行。
或者使用 maven-exec 插件从 maven 调用“initialize liquibase:update”。或者您创建一个配置文件,将 liquibase:update 绑定(bind)到生命周期阶段初始化,并在调用
时执行 udatemvn initialize -Pliquibase
我不知道解决这个问题的更好方法,也找不到合适的解决方案。
供引用: Maven lifecycle
关于java - 在maven中使用属性文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16476955/