如何根据我的个人资料更改maven中的.properties文件?

ben*_*rre 24 maven-2 properties

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

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

Pas*_*ent 64

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

首先,启用资源过滤:

<project>
  ...
  <build>
    <resources>
      <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
      </resource>
    </resources>
    ...
  </build>
</project>
Run Code Online (Sandbox Code Playgroud)

然后,在你的地方申报一个占位符src/main/resources/my_config.properties,例如:

myprop1 = somevalue
myprop2 = ${foo.bar}
Run Code Online (Sandbox Code Playgroud)

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

<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>
Run Code Online (Sandbox Code Playgroud)

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

$ mvn process-resources -Denv=dev
[INFO] Scanning for projects...
...
$ cat target/classes/my_config.properties 
myprop1 = somevalue
myprop2 = othervalue

正如我所说,这种方法存在差异(例如,您可以将值放在文件中进行过滤),但这会让您开始.

参考

更多资源

  • 如果使用`Spring Boot`,瘦身会略有不同。https://docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html (2认同)