【问题标题】:Populate environment properties file using pom.xml proeprties使用 pom.xml 属性填充环境属性文件
【发布时间】:2019-02-14 15:43:18
【问题描述】:

我想从 pom.xml 中获取 artifactId 并使用这个 artifactId 来填充属性文件。

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <groupId>xxxxx</groupId>
   <artifactId>fm-service</artifactId>
   <packaging>war</packaging>
   <version>x.x.x-SNAPSHOT</version>
</project>

application.preperties

email.subject = "ALERT - [artifactId from pom.xml]"

有可能这样做吗?如果是如何,如果不是,请提出替代方案。

【问题讨论】:

  • 属性文件是否已经存在,还是要从头开始编写?还有,你是想在maven里做,还是没关系?
  • 是的,属性文件存在,我想要它在 maven 中。

标签: java spring maven pom.xml


【解决方案1】:

Maven 提供了filter resources 的方法。

首先,必须将文件application.properties 放入src/main/resources 目录中。那么它的内容必须是:

email.subject = "ALERT - ${project.artifactId}"

最后(但并非最不重要)只需将此配置添加到您的 POM:

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

现在resources目录下的所有文件都会被过滤掉,也就是说里面的所有变量都被解析了。

【讨论】:

  • true 表示排除还是包含?
  • 如链接文档 (Maven Filtering) 所述,&lt;filtering&gt;true&lt;/filtering&gt; 表示所有资源文件在实际放入目标目录之前都经过过滤。
猜你喜欢
  • 1970-01-01
  • 2015-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-23
  • 2013-09-05
  • 1970-01-01
相关资源
最近更新 更多