Maven根据配置文件更改文件中的值 [英] Maven Change a value in a file based on profile

查看:160
本文介绍了Maven根据配置文件更改文件中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有一个名为ApplicationResources.properties的属性文件,其属性会根据环境而变化.让我们说这个属性是:

I have a properties file called ApplicationResources.properties in my application with a property that changes depending on the environment. Let us say the property is:

     resources.location=/home/username/resources

并且在开发过程中执行应用程序和将其投入生产时此值是不同的.

and this value is different when the application is executed during development and when the application goes into production.

我知道我可以在Maven中使用不同的配置文件在不同的环境中执行不同的构建任务.我想要做的是基于所使用的Maven概要文件替换属性文件中resources.location的值.这有可能吗?

I know that I can use different profiles in Maven to perform different build tasks in different environments. What I want to do is somehow replace the value of the resources.location in the properties file based on the Maven profile in use. Is this even possible?

推荐答案

我想要做的是基于所使用的Maven概要文件替换属性文件中resources.location的值.这有可能吗?

What I want to do is somehow replace the value of the resources.location in the properties file based on the Maven profile in use. Is this even possible?

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

Yes it is. Activate resources filtering and define the value to replace in each profile.

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

In your ApplicationResources.properties, declare a token to replace like this:

resources.location=${your.location}

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

In your POM, add a <filtering> tag for the appropriate <resource> and set it to true like this:

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

然后,在每个配置文件内的<properties>元素内添加一个<your.location>元素:

Then, add a <your.location> element within the <properties> element inside each profile:

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

有关资源过滤的更多信息此处此处.

More on filtering of resources here and here.

这篇关于Maven根据配置文件更改文件中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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