我创建了带有过滤器的简单耳朵项目。我想为每个环境使用不同的设置,这些设置应该以application.xml
env-entries 的形式传递给生成的文件。ear包的生成是用maven-ear-plugin完成的,如下图:
<plugin>
<artifactId>maven-ear-plugin</artifactId>
<version>2.9</version>
<configuration>
<generateApplicationXml>true</generateApplicationXml>
<version>6</version>
<envEntries>
<env-entry>
<env-entry-name>customProperty</env-entry-name>
<env-entry-type>java.lang.String</env-entry-type>
<env-entry-value>${custom.property}</env-entry-value>
</env-entry>
</envEntries>
<applicationName>${custom.property}</applicationName>
</configuration>
</plugin>
为此,我必须使用另一个插件properties-maven-plugin。它成功地从文件中读取属性并将它们设置为 maven 项目属性,所以我可以使用 .xml 将它们插入到 pom.xml 文件中${}
。它适用于大多数 pom.xml 元素(即,不幸的是,当我将它放在我需要它的元素<applicationName>
内时,它没有成功查找。下面是生成的。env-entry
application.xml
<?xml version="1.0" encoding="UTF-8"?>
<application xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_6.xsd" version="6">
<application-name>default property</application-name>
<display-name>test</display-name>
<env-entry>
<env-entry-name>customProperty</env-entry-name>
<env-entry-type>java.lang.String</env-entry-type>
<env-entry-value>${custom.property}</env-entry-value>
</env-entry>
</application>
这可能是一个应该在Maven Ear Plugin发布的错误,但我在那里没有帐户。如果有人想自己检查一下,我还附上了存档的 maven 项目:test.zip。
编辑
在按照用户@skegg99建议创建文件后,我已经设法使用maven-resource-plugin
和过滤文件克服了这个问题。由于我无法替换此文件,我不得不将其复制到 META-INF 目录。我看起来不漂亮,我知道,但它现在解决了这个问题。以下是 的附加标记:application.xml
maven-resource-plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<configuration>
<outputDirectory>${basedir}/target/${project.artifactId}-${project.version}/META-INF</outputDirectory>
<filters>
<filter>src/main/filters/${env}.properties</filter>
</filters>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
还有这里:
<resources>
<resource>
<directory>${basedir}/target</directory>
<filtering>true</filtering>
<includes>
<include>application.xml</include>
</includes>
</resource>
</resources>
整个项目配置可以从这里下载。