maven 资源过滤:让 Maven 能解析资源文件中的标识符,替换其中的配置(例如: ${} )信息为实际的值
问题现象
maven 打包后,target下相应的pdf文件异常,手动打开提示文件损坏,查看文件详情发现文件大小变了
以下摘自maven文档
Warning: Do not filter files with binary content like images! This will most likely result in corrupt output.
If you have both text files and binary files as resources it is recommended to have two separated folders. One folder src/main/resources (default) for the resources which are not filtered and another folder src/main/resources-filtered for the resources which are filtered.
也就是说二进制文件不能过滤,要排除需要打包的二进制文件。
解决方案
方案一:
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>pdf</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
</plugin>
方案二:
<resources>
<resource>
<!--排除pdf,不打包到classpath下,自然就不会过滤-->
<directory>src/main/resources</directory>
<filtering>true</filtering>
<excludes>
<exclude>**/*.pdf</exclude>
</excludes>
</resource>
<resource>
<!--将pdf打包到classpath下,但是不进行资源过滤-->
<directory>src/main/resources</directory>
<filtering>false</filtering>
<includes>
<include>**/*.pdf</include>
</includes>
</resource>
</resources>