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

时间:2010-02-25 16:02:18

标签: maven-2 profiles

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

     resources.location=/home/username/resources

,在开发期间和应用程序投入生产时执行应用程序时,此值不同。

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

1 个答案:

答案 0 :(得分:48)

  

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

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

ApplicationResources.properties中,声明要替换的令牌:

resources.location=${your.location}

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

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

有关资源herehere的过滤的详情。