Maven war打包时过滤多余文件的办法

使用maven进行WAR打包服务的时候需要将部分多余的项目工程的文件过滤掉可以使用一下办法:

<build>
		<finalName>solr</finalName>

		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-war-plugin</artifactId>
				<version>2.6</version>
				<configuration>              
					<warSourceExcludes>WEB-INF/weblogic.xml</warSourceExcludes>
					<packagingExcludes>WEB-INF/lib/solr-core-5.3.0.jar</packagingExcludes>
					<webResources>
						
					</webResources>
				</configuration>
			</plugin>
		</plugins>

	</build>

   需要将lib目录中的一个solr -core-5.3.0.jar包过滤掉可以配置packagingExcludes这个节点。

 需要在将web-inf目录下的文件weblogic.xml过滤掉可以使用warSourceExcludes这个节点。

 这个两个配置虽然都是起到过滤文件的效果,但是还是有一点小区别的,那就是packagingExcludes在warSourceExcludes之后起作用。

 官方文档中是这样解释的:

  1. packagingExcludes:The comma separated list of tokens to exclude from the WAR before packaging. This option may be used to implement the skinny WAR use case. Note that you can use the Java Regular Expressions engine to include and exclude specific pattern using the expression %regex[]. Hint: read the about (?!Pattern).
  2. warSourceExcludes:The comma separated list of tokens to exclude when copying the content of the warSourceDirectory.

 因为warSourceExcludes过滤的是webapp目录下存在的资源文件,比如图片,jsp之类的资源文件,而jar包是在这个阶段额外从maven仓库中导入进来的,所以warSourceExcludes对WEB-INFO/lib下的jar包文件过滤是没有效果的。

需要过滤jar包必须要使用packagingExcludes ,配置才能生效。

   既然是这样的话,按照一下这样配置也能起到相同的效果:

  

<packagingExcludes>WEB-INF/lib/solr-core-5.3.0.jar,WEB-INF/weblogic.xml</packagingExcludes>

但是奇怪的是执行mvn war:inplace 的时候还是会将以上设置需要过滤的jar包放置到目标目录中去。 

 再看一个例子,在工程中如果使用了logback(slf4j作为facade)作为日志记录工具,需要在项目打包的时候将log4j,commons-logging 这些jar包过滤掉,可以采用以下配置:

<configuration>
	<packagingExcludes>
		WEB-INF/lib/commons-logging-*.jar,
		%regex[WEB-INF/lib/log4j-(?!over-slf4j).*.jar]    
       </packagingExcludes>
</configuration>

 官方文档参考:http://maven.apache.org/plugins/maven-war-plugin/war-mojo.html#warSourceExcludes

 

猜你喜欢

转载自mozhenghua.iteye.com/blog/2262577