Maven项src/main/java目录下配置文件无法被导出或者生效的问题和处理方案

问题展示

原因剖析

处理方案

第一种:调整配置文件的位置(建议)

第二种:在pom.xml文件中配置


问题展示

今天试着大了maven+mybatis,以下是我的目录结构,我的xml文件是放在java文件夹下的

mappers 标签配置了需要加载的 Dept的sql映射配置文件DeptMapper.xml。

其单元测试访问的时候,报了一个错

找不到DeptMapper.xml文件

再看编译后的文件,竟然没有我们的DeptMapper.xml文件

 

我试着把DeptMapper.xml文件放到resouces目录下

运行单元测试,竟然成功了

再看编译后的文件,对应目录下也有我们的DeptMapper.xml文件

原因剖析

为什么DeptMapper.xml文件放在java文件夹下就没被编译导出来呢,而放在resouces目录下就可以,这肯定是和maven有关,原来,默认maven在src/main/java中只编译java文件,其他的文件会被忽略

处理方案

第一种:调整配置文件的位置(建议)

既然知道了默认maven在src/main/java中只编译java文件,其他的文件会被忽略,那么我们就把配置文件放在resouces文件夹下就好了。

第二种:在pom.xml文件中配置

在pom.xml文件的<build>标签中添加以下内容,把java目录下的文件也打包编译

 <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>

以上配置即可满足,当然为了更完善一点,那就用以下配置,把默认的也给配置上

 <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>

猜你喜欢

转载自blog.csdn.net/lgl782519197/article/details/109033172