如何根据我的个人资料在 Maven 中更改 .properties 文件? [英] How can I change a .properties file in maven depending on my profile?

查看:15
本文介绍了如何根据我的个人资料在 Maven 中更改 .properties 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何根据我的个人资料在 maven 中更改 .properties 文件?根据应用程序是在工作站上运行还是在数据中心运行,文件 my_config.properties 的部分会发生变化(但不是全部).

目前我在 hudson 构建每个版本后手动更改 .war 文件中的 .properties 文件.

解决方案

通常,有几种方法可以实现此类事情.但它们中的大多数都是围绕相同功能的变体:配置文件和过滤.我将展示最简单的方法.

首先,启用资源过滤:

...<构建><资源><资源><目录>src/main/resources</directory><filtering>true</filtering></资源></资源>...</build></项目>

然后,在您的 src/main/resources/my_config.properties 中声明一个占位符,例如:

myprop1 = somevaluemyprop2 = ${foo.bar}

最后,在配置文件中声明属性及其值:

...<个人资料><个人资料><id>env-dev</id><激活><财产><name>env</name><value>dev</value></属性></激活><属性><foo.bar>其他值</foo.bar></属性></个人资料>...</个人资料></项目>

并使用给定的配置文件运行 maven:

<前>$ mvn process-resources -Denv=dev[信息] 正在扫描项目......$ cat target/classes/my_config.propertiesmyprop1 = 某个值myprop2 = 其他值

正如我所说,这种方法有多种变化(例如,您可以在文件中放置值以进行过滤),但这会让您开始.

参考资料

更多资源

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

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.

First, enable filtering of resources:

<project>
  ...
  <build>
    <resources>
      <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
      </resource>
    </resources>
    ...
  </build>
</project>

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>

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.

References

More resources

这篇关于如何根据我的个人资料在 Maven 中更改 .properties 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆