代码之家  ›  专栏  ›  技术社区  ›  Vincent Ramdhanie

Maven根据配置文件更改文件中的值

  •  22
  • Vincent Ramdhanie  · 技术社区  · 16 年前

    我的应用程序中有一个名为ApplicationResources.properties的属性文件,该文件的属性根据环境的不同而变化。假设财产是:

         resources.location=/home/username/resources
    

    1 回复  |  直到 16 年前
        1
  •  50
  •   Pascal Thivent    16 年前

    我想做的是,基于使用中的Maven概要文件,以某种方式替换属性文件中resources.location的值。这有可能吗?

    在你的 ApplicationResources.properties ,声明要替换的令牌,如下所示:

    resources.location=${your.location}
    

    在POM中,添加一个 <filtering> <resource> 把它设置成这样:

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

    <your.location> 中的元素 <properties> 每个配置文件中的元素:

    <project>
      ...
      <profiles>
        <profile>
          <id>my-profile</id>
          ...
          <properties>
            <your.location>/home/username/resources</your.location>
          </properties>
          ...
        </profile>
        ...
      </profiles>
    </project>
    

    here here

    推荐文章