Maven EAR排除Web模块后,如何同步移除配置文件中的模块条目?
你的问题核心在于:因为generateApplicationXml设为false,maven-ear-plugin不会自动根据<excluded>配置修改手动维护的application.xml,同时jboss-deployment-structure.xml也不在插件的自动处理范围内。要同步移除这两个文件里的模块条目,需要结合Maven资源过滤来动态处理配置内容。
步骤1:将配置文件纳入资源过滤范围
首先确保application.xml和jboss-deployment-structure.xml放在src/main/resources目录下(如果原来在其他位置,比如src/main/application,需要调整资源配置),然后在pom的<build>节点下添加资源过滤配置:
<resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> <includes> <include>application.xml</include> <include>jboss-deployment-structure.xml</include> </includes> </resource> </resources>
步骤2:修改application.xml,添加条件注释
在application.xml中找到test-support模块的条目,用Maven的属性条件来控制是否注释该模块:
<!-- ${exclude.test.war}?<!-- : --> <module> <web> <web-uri>test-support-${app.jndi.context}.war</web-uri> <context-root>${contextRoot}</context-root> </web> </module> <!-- -->
当exclude.test.war为true时,这段代码会被注释掉;为false时正常生效。
步骤3:修改jboss-deployment-structure.xml,同样添加条件注释
找到文件中对应test-support的模块引用,做同样的条件处理:
<!-- ${exclude.test.war}?<!-- : --> <module name="test-support" /> <!-- -->
步骤4:确认maven-ear-plugin配置
确保插件的<filtering>已经设为true(你当前配置里已经有了),同时如果application.xml的输出位置需要调整,可以在插件配置里指定:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-ear-plugin</artifactId> <configuration> <!-- 原有配置保持不变 --> <filtering>true</filtering> <applicationXml>${project.build.outputDirectory}/application.xml</applicationXml> </configuration> </plugin>
这样配置后,当你通过-Dexclude.test.war=true构建EAR时,test-support模块不仅不会被打包到target目录,application.xml和jboss-deployment-structure.xml里的对应条目也会被自动注释,不会影响生产环境部署。
内容的提问来源于stack exchange,提问作者Alexander Rumanovsk

