如何避免IntelliJ中Maven开发环境生效packagingExcludes配置
解决Maven War插件环境差异化排除配置的问题
这问题其实挺常见的,核心就是把环境相关的配置拆分开,让开发和非开发环境用不同的规则,Maven的Profiles正好能搞定这个,不用再每次手动注释配置了。
你可以按下面的步骤来修改:
1. 在pom.xml中定义环境Profiles
把原来的war插件配置拆到两个Profile里,一个对应开发环境(不做文件排除),一个对应非开发环境(保留你原来的排除规则):
<profiles> <!-- 开发环境Profile,默认自动激活 --> <profile> <id>dev</id> <activation> <activeByDefault>true</activeByDefault> </activation> <build> <plugins> <plugin> <artifactId>maven-war-plugin</artifactId> <version>2.4</version> <configuration> <warSourceDirectory>web</warSourceDirectory> <failOnMissingWebXml>true</failOnMissingWebXml> <webXml>web/WEB-INF/web.xml</webXml> <!-- 开发环境不排除任何文件,留空或者直接删除这个配置项都可以 --> <packagingExcludes></packagingExcludes> </configuration> </plugin> </plugins> </build> </profile> <!-- 非开发环境Profile,需要手动激活 --> <profile> <id>prod</id> <build> <plugins> <plugin> <artifactId>maven-war-plugin</artifactId> <version>2.4</version> <configuration> <warSourceDirectory>web</warSourceDirectory> <failOnMissingWebXml>true</failOnMissingWebXml> <webXml>web/WEB-INF/web.xml</webXml> <!-- 保留你原来的文件排除规则 --> <packagingExcludes> WEB-INF/classes/application.properties, WEB-INF/classes/proxool.xml, WEB-INF/classes/log4j.properties, WEB-INF/classes/wetemplates/** </packagingExcludes> </configuration> </plugin> </plugins> </build> </profile> </profiles>
2. 配置IntelliJ使用开发Profile
因为我们把dev设为默认激活的Profile,平时在IntelliJ里构建项目时,会自动使用dev的配置,不会排除那些必要文件。如果你的IntelliJ没自动识别,也可以手动配置:
- 打开Run/Debug Configurations,找到你的Maven构建配置(比如
package或者tomcat7:run) - 在"Profiles"输入框里填入
dev,保存后运行就会自动用开发环境的规则了
3. 非开发环境构建时激活生产Profile
当你需要打包部署到非开发环境时,只需要在Maven命令里指定prod Profile即可:
mvn clean package -Pprod
这样就彻底告别手动注释配置的麻烦了,开发和生产环境的配置完全分离,省心很多。
内容的提问来源于stack exchange,提问作者Paul Zepernick
相关产品推荐
相关产品推荐

