今天寫新項目的時候遇到一個問題,在resources目錄下存儲的.xlsx文件,編譯過后會增大幾kb,無法打開。
Google了一番之后,發現問題源自於maven-resources-plugin這個插件。這個插件會把resources目錄下的文本文件進行轉碼,但它無法正確的識別哪些是文本文件,因而會錯誤的將不需要進行轉碼的二進制文件也進行轉碼,導致這些二進制文件無法打開。
解決的方法是在pom.xml中的maven-resources-plugin下,將不需要轉碼的文件擴展名填入nonFilteredFileExtensions,格式如下:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> <configuration> <encoding>UTF-8</encoding> <nonFilteredFileExtensions> <nonFilteredFileExtension>xls</nonFilteredFileExtension> <nonFilteredFileExtension>xlsx</nonFilteredFileExtension> </nonFilteredFileExtensions> </configuration> </plugin>