结果证明这是一个 Maven 问题,而不是 Vaadin 问题。我还是会在 Vaadin 的背景下回答这个问题。
回顾:我只想维护一个web.xml。那应该是我的 -ui 模块中的那个,因为所有源代码通常都驻留在那里,并且 -production 模块中应该没有源代码。
问题:maven-war-plugin 的覆盖功能不会将覆盖中的 web.xml 复制或合并到目标中。
我已经通过几个步骤解决了这个问题。首先,我在 -ui 模块中使用以下内容增强了我的 web.xml:
...
<context-param>
<description>Vaadin turn debugging mode on/off</description>
<param-name>productionMode</param-name>
<param-value>${vaadin.productionmode}</param-value>
</context-param>
...
然后在我添加的 -ui 模块的 pom.xml 中:
<properties>
...
<vaadin.productionmode>false</vaadin.productionmode>
</properties>
在我添加的 -production 模块的 pom.xml 中:
<profiles>
<profile>
<id>production</id>
<properties>
<vaadin.productionmode>true</vaadin.productionmode>
</properties>
...
现在我们有一个 Maven 属性,vaadin.productionmode,默认情况下它的值为 false,除非生产配置文件处于活动状态,在这种情况下值为 true。我们还为 Maven 过滤准备了 web.xml。但是我们还需要激活过滤(-ui模块的POM):
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
...
<!-- Do filtering on web.xml -->
<filteringDeploymentDescriptors>true</filteringDeploymentDescriptors>
</configuration>
(除非您设置此配置参数,否则 Maven WAR 插件不会对 web.xml 文件进行过滤)
您还需要在 -production 模块的 POM 上激活 web.xml 过滤:
<profiles>
<profile>
<id>production</id>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
...
<configuration>
<!-- Do filtering on web.xml -->
<filteringDeploymentDescriptors>true</filteringDeploymentDescriptors>
<!-- Use web.xml from UI module -->
<webXml>../myapp-ui/src/main/webapp/WEB-INF/web.xml</webXml>
<overlays>
...
最后,正如您在上面的 XML 中看到的,我告诉 -production 模块它应该使用 -ui 项目中的 web.xml。这才是真正的诀窍!您现在可以从 -production 模块中删除 web.xml 文件,因为它不再使用。
已经完成了什么:
- 使用 Maven 过滤功能,web.xml 的内容变得可变。当生产配置文件在 Maven 中处于活动状态时,变量
vaadin.productionmode 的设置会有所不同。
- -production 模块现在不再有自己的 web.xml。相反,它从 -ui 模块复制文件(在复制后进行适当的过滤)。