【问题标题】:Maven Change a value in a file based on profileMaven 根据配置文件更改文件中的值
【发布时间】:2011-01-21 01:58:26
【问题描述】:

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

     resources.location=/home/username/resources

在开发过程中执行应用程序和应用程序投入生产时,此值是不同的。

我知道我可以在 Maven 中使用不同的配置文件在不同的环境中执行不同的构建任务。我想要做的是根据正在使用的 Maven 配置文件以某种方式替换属性文件中 resources.location 的值。这甚至可能吗?

【问题讨论】:

    标签: maven-2 profiles


    【解决方案1】:

    我想要做的是根据正在使用的 Maven 配置文件以某种方式替换属性文件中 resources.location 的值。这甚至可能吗?

    是的。激活资源过滤并在每个配置文件中定义要替换的值。

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

    resources.location=${your.location}
    

    在您的 POM 中,为适当的 <resource> 添加一个 <filtering> 标签并将其设置为 true,如下所示:

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

    然后,在每个配置文件的&lt;properties&gt; 元素中添加一个&lt;your.location&gt; 元素:

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

    更多关于过滤资源herehere

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-16
    • 1970-01-01
    • 2017-10-06
    • 1970-01-01
    • 1970-01-01
    • 2011-05-10
    • 2018-07-15
    相关资源
    最近更新 更多