如何根据自己的个人资料更改Maven中的.properties文件? [英] How can I change a .properties file in maven depending on my profile?
问题描述
如何根据个人资料在Maven中更改.properties文件?根据应用程序是构建为在工作站上运行还是数据中心运行,文件my_config.properties的部分会更改(但不是全部).
How can I change a .properties file in maven depending on my profile? Depending on whether the application is built to run on a workstation or the datacenter parts of the file my_config.properties change (but not all).
当前,我在hudson构建每个版本之后手动更改.war文件中的.properties文件.
Currently I manually change the .properties file within the .war file after hudson builds each version.
推荐答案
通常,有几种方法可以实现这种事情.但其中大多数是围绕相同功能的变体:配置文件和过滤.我将展示最简单的方法.
As often, there are several ways to implement this kind of things. But most of them are variations around the same features: profiles and filtering. I'll show the most simple approach.
首先,启用资源过滤:
<project>
...
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
...
</build>
</project>
然后,在您的src/main/resources/my_config.properties
中声明一个占位符,例如:
Then, declare a place holder in your src/main/resources/my_config.properties
, for example:
myprop1 = somevalue
myprop2 = ${foo.bar}
最后,在配置文件中声明属性及其值:
Finally, declare properties and their values in a profile:
<project>
...
<profiles>
<profile>
<id>env-dev</id>
<activation>
<property>
<name>env</name>
<value>dev</value>
</property>
</activation>
<properties>
<foo.bar>othervalue</foo.bar>
</properties>
</profile>
...
</profiles>
</project>
并使用给定的配置文件运行maven:
And run maven with a given profile:
$ mvn process-resources -Denv=dev
[INFO] Scanning for projects...
...
$ cat target/classes/my_config.properties
myprop1 = somevalue
myprop2 = othervalue
就像我说的那样,这种方法存在一些差异(例如,您可以在文件中放置要过滤的值),但这会帮助您入门.
As I said, there are variation around this approach (e.g. you can place values to filter in files), but this will get you started.
- Introduction to Build Profiles
- Maven Resources Filtering
- A Maven2 multi-environment filter setup
- Maven project filtering
- Using Maven profiles and resource filtering
- Building For Different Environments with Maven 2
这篇关于如何根据自己的个人资料更改Maven中的.properties文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!