maven编译过滤(如excel)
现象
使用easyExcel 3.1.1
操作excel
使用maven
编译后,原先正常excel
在读取的时候抛出了异常。
java.util.zip.ZipException: invalid stored block lengths
一开始是怀疑excel里面内容格式不对,打开excel检查后发现格式都是正常的。
然后开始看源码
debug
到com.alibaba.excel.util.WorkBookUtil#createWorkBook
时想到打开一个普通的excel
文件为啥要zip
解压,是不是被压缩了。
随机在打包后的target
文件夹下去打开excel
,果然也是打不开了。
之后查阅资料,确认是maven
打包的时候损坏了文件。
那么问题就变成了excel
文件不通过maven
编译,直接打包进target
解决方案
关键就在于resource
标签的处理,2个resource
标签都需要加上
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<excludes>
<exclude>
template/*.xlsx
</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
<includes>
<include>
template/*.xlsx
</include>
</includes>
</resource>
</resources>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
原因
maven
的pom
文件中有时候会定义一些变量
通常都是定义在profiles
标签下,然后我们在resource
下一些xml
文件或者yml
文件中可以引用这些变量,常见的就是用来切换dev
环境和pro
环境
捞个网上的demo
xml中直接引用${config} yml中引用@config@
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<config>pathConfig</config>
</properties>
</profile>
</profiles>