maven编译过滤(如excel)

现象

使用easyExcel 3.1.1操作excel

使用maven编译后,原先正常excel在读取的时候抛出了异常。

java.util.zip.ZipException: invalid stored block lengths

一开始是怀疑excel里面内容格式不对,打开excel检查后发现格式都是正常的。

然后开始看源码
debugcom.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>

原因

mavenpom文件中有时候会定义一些变量
通常都是定义在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>