问题发生原因
项目构建使用Maven,因为代码较老有一些jar包是本地项目打包生成,一部分jar是古老第三方包,不准备上传私有仓库。
在war包构建时使用maven插件maven-war-plugin。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webResources>
<resource>
<directory>${project.basedir}/../out/artifacts/AAA/</directory>
<targetPath>WEB-INF/lib</targetPath>
<filtering>true</filtering>
<includes>
<include>**/BBB.jar</include>
</includes>
</resource>
<resource>
<directory>${project.basedir}/../AAA/lib/</directory>
<targetPath>WEB-INF/lib</targetPath>
<filtering>true</filtering>
<includes>
<include>**/CCC.jar</include>
<include>**/DDD.jar</include>
</includes>
</resource>
</webResources>
</configuration>
</plugin>
生成后war在tomcat下运行,提示zip无法解压,文件损坏。
实际解压war包内BBB.jar的时候,确实文件与IDEA构建jar结构不同。
原因
官方解释(https://maven.apache.org/plugins/maven-war-plugin/examples/adding-filtering-webresources.html)
To prevent corrupting your binary files when filtering is enabled, you can configure a list of file extensions that will not be filtered.
翻译:为了避免开启 filter后你的二进制文件被损毁,你可配置不被过滤的文件拓展名列表
在war插件增加如下配置
<nonFilteredFileExtensions>
<!-- default value contains jpg,jpeg,gif,bmp,png -->
<nonFilteredFileExtension>jar</nonFilteredFileExtension>
</nonFilteredFileExtensions>
究其原因可能下官方文档一下内容得到解释
Note: In versions 2.2 and earlier of this plugin the platform encoding was used when filtering resources. Depending on what that encoding was you could end up with scrambled characters after filtering. Starting with version 2.3 this plugin respects the property project.build.sourceEncoding when filtering resources. One notable exception to this is that .xml files are filtered using the encoding specified inside the xml-file itself.
2.2和之前的版本过滤资源时使用平台的默认编码。从这里可以猜测过滤资源时会使用到字符编码。
因项目使用GBK编码,是否在二进制包转移时候进行UTF8进行编码?
最终pom.xml改为
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>jar</nonFilteredFileExtension>
</nonFilteredFileExtensions>
<webResources>
<resource>
<directory>${project.basedir}/../out/artifacts/AAA/</directory>
<targetPath>WEB-INF/lib</targetPath>
<filtering>true</filtering>
<includes>
<include>**/BBB.jar</include>
</includes>
</resource>
<resource>
<directory>${project.basedir}/../AAA/lib/</directory>
<targetPath>WEB-INF/lib</targetPath>
<filtering>true</filtering>
<includes>
<include>**/CCC.jar</include>
<include>**/DDD.jar</include>
</includes>
</resource>
</webResources>
</configuration>
</plugin>
巨人的肩膀
maven-war-plugin二进制文件损坏问题_war打包 二进制文件损坏
标签:maven,lib,plugin,project,jar,war,Maven,编译,War From: https://www.cnblogs.com/hr0552/p/17698564.html