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

Vin*_*nie 25 maven-2 profiles

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

     resources.location=/home/username/resources
Run Code Online (Sandbox Code Playgroud)

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

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

Pas*_*ent 48

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

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

在你的ApplicationResources.properties,声明一个令牌替换如下:

resources.location=${your.location}
Run Code Online (Sandbox Code Playgroud)

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

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

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

<project>
  ...
  <profiles>
    <profile>
      <id>my-profile</id>
      ...
      <properties>
        <your.location>/home/username/resources</your.location>
      </properties>
      ...
    </profile>
    ...
  </profiles>
</project>
Run Code Online (Sandbox Code Playgroud)

更多关于在这里这里过滤资源的信息.