2017-01-12 20 views
0

我想从pom.xml中获取artifactId并使用此artifactId来填充属性文件。使用pom.xml填充环境属性文件proeprties

的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]" 

是有可能这样做?如果是的话,如果没有,那么请提出替代方案。

+0

属性文件是否已经存在,或者您是否想从头开始编写它?另外,你想在maven中做到这一点,或者它并不重要? – Kraylog

+0

是的属性文件存在,我希望它在maven中。 – rohanagarwal

回答

2

Maven提供了一种方法来filter resources

首先,文件application.preperties必须放入src/main/resources目录。然后,它的内容是:

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

最后(但并非最不重要的)只是这种配置添加到您的POM:

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

现在在资源目录中的所有文件都会被过滤,这意味着所有内部变量已解决。

+0

true意味着排除或包含? – rohanagarwal

+0

由于链接文档([Maven Filtering](https://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html))描述,所以' true'表示所有资源文件都被过滤在实际被放入目标目录之前。 – Seelenvirtuose