一、问题描述

(1)默认 Spring Boot 项目结构,资源文件放置在 src/main/resources 中,测试的资源文件在 src/test/resources 中。

src/main/resources 与 src/test/resources 的区别:

  • src/main/java 里面的 java 文件只能直接加载 src/main/resources 下的资源,不能直接加载 src/test/resources 下的资源;
  • src/test/java 里面的 java 文件既能加载 src/test/resources 下的资源,又能加载 src/main/resources 下的资源,当两个 resources 下都有要加载的同名资源时候,优先选择 src/test/resources 下的资源。

(2)将项目打包后,解压 jar 可以发现原先 src/main/resources 目录下的资源文件已经被打包进来了。

(3)但有时我们的资源文件并不一定是放在 src/main/resources 目录下,比如我的项目通常会将资源文件放在 src/test/resources 目录下。

原因:根据实践经验表明,测试完后的配置项是最完整的,且经常会忘记替换正式版参数,因而选择将配置文件全部放置在 src/test/resources 目录下。

(4)又比如 mybatis 的 mapper.xml 文件,我们习惯把它和 Mapper.java 放一起。

重新把 mapper.xml 文件 指向 resources 资源文件下

        <resources><resource><directory>src/main/java</directory><includes><include>**/*.xml</include></includes></resource></resources>

(5)但上面这两种情况的资源文件,在使用 maven 打包时是不会被打包进 jar 的。这时候我们便要指定需要打包的资源文件,这个有如下两种方法可以实现。

二、使用 <resources> 标签

(1)<resources> 标签位于 <build> 标签内,用于指定项目资源文件的位置。比如下面配置我们指定了 src/test/resources 也是资源文件目录:

<build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins><!-- 重新指明资源文件位置 --><resources><resource><directory>src/test/resources</directory></resource><resource><directory>src/main/resources</directory></resource></resources>
</build>
<build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins><!-- 重新指明资源文件位置 --><resources><resource><directory>src/test/resources</directory></resource><resource><directory>src/main/resources</directory></resource></resources>
</build>

(2)而对于写在包下的 Mapper.xml 文件,我们则可以通过如下配置指明资源文件位置:

提示:其中 */ 这样的写法,是为了保证各级子目录下的资源文件被打包。

<build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins><!-- 重新指明资源文件位置 --><resources><resource><directory>src/main/java</directory><includes><include>**/*.xml</include></includes></resource><resource><directory>src/main/resources</directory></resource></resources>
</build>

(3)我们还可以通过 excludes 标签剔除不需要的资源:

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

三、使用 maven-resources-plugin 插件

(1)除了使用 <resources> 标签外,我们也可以使用 maven-resources-plugin 插件实现同样的目的。比如下面配置把 src/test/resources 目录下的资源文件打包到 classes 目录下:

<build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-resources-plugin</artifactId><executions><execution><id>my-resources</id><phase>process-resources</phase><goals><goal>copy-resources</goal></goals><configuration><outputDirectory>${basedir}/target/classes</outputDirectory><resources><resource><directory>${basedir}/src/test/resources</directory><includes><include>*.properties</include><include>*.xml</include></includes></resource></resources></configuration></execution></executions></plugin></plugins>
</build>

(2)而对于写在包下的 Mapper.xml 文件,我们也可以通过 maven-resources-plugin 插件将其打包到相应位置:

<build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-resources-plugin</artifactId><executions><execution><id>copy-xmls</id><phase>process-sources</phase><goals><goal>copy-resources</goal></goals><configuration><outputDirectory>${basedir}/target/classes</outputDirectory><resources><resource><directory>${basedir}/src/main/java</directory><includes><include>**/*.xml</include></includes></resource></resources></configuration></execution></executions></plugin></plugins>
</build>

(3)使用 maven-resources-plugin 插件时,我们同样可以通过 excludes 标签剔除不需要的资源:

<build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-resources-plugin</artifactId><executions><execution><id>my-resources</id><phase>process-resources</phase><goals><goal>copy-resources</goal></goals><configuration><outputDirectory>${basedir}/target/classes</outputDirectory><resources><resource><directory>${basedir}/src/test/resources</directory><includes><include>**/*.*</include></includes><excludes><exclude>log4j2-spring.xml</exclude></excludes></resource></resources></configuration></execution></executions></plugin></plugins>
</build>

SpringBoot - resource资源文件的打包配置详解(指定资源文件位置)相关推荐

  1. SpringBoot——slf4j+logback日志处理及配置详解

    SpringBoot--sl4j+logback日志处理及配置详解 日志的级别 打印级别:ALL > TRACE > FATAL > DEBUG > INFO > WAR ...

  2. SpringBoot配置文件中spring.profiles.active配置详解

    SpringBoot配置文件中spring.profiles.active配置详解 1.多环境配置 我们在开发Spring Boot应用时,通常同一套程序会被应用和安装到几个不同的环境,比如:开发.测 ...

  3. python怎么导入文件-Python文件如何引入?详解引入Python文件步骤

    python基本语法--引入Python文件 1.新建python文件 :在同目录lib下创建mylib.py和loadlib.py两个文件 2.在mylib.py文件中创建一个Hello的类 并且给 ...

  4. vue单文件props写法_详解Vue 单文件组件的三种写法

    详解Vue 单文件组件的三种写法 JS构造选项写法 export defaul { data, methods, ...} JS class写法 @Component export default c ...

  5. SpringBoot application.properties和application.yml配置详解

    #SPRING CONFIG(ConfigFileApplicationListener) spring.config.name =#配置文件名(默认 为 'application' ) spring ...

  6. maven打包配置详解

    用了几个月的maven了 , 其实主要的话就两方面 (1)用maven管理一些jar包,(2)构建打包项目 maven有三种生命周期, clean,default,site clean  就是清除项目 ...

  7. 压缩命令_Linux环境下文件压缩打包命令详解

    你好,我是goldsunC 让我们一起进步吧! 前言 我们知道,在面向对象的程序设计中,一切皆对象.而在Linux操作系统中,一切皆文件,因此我们总会跟文件打交道. Linux文件系统很庞大复杂,不过 ...

  8. vue.config.js配置,webpack打包配置详解

    注意: vue-cli3 脚手架搭建完成后,项目目录中没有 vue.config.js 文件,需要手动创建 第一步:手动创建vue.config.js文件, 一般放在和package.json同级目录 ...

  9. java日志文件log4j.properties配置详解

    一.Log4j配置 第一步:加入log4j-1.2.8.jar到lib下. 第二步:在CLASSPATH下建立log4j.properties.内容如下: 放在src下的话就不用配置 否则得去web. ...

最新文章

  1. 在幕后看看Swift中的Map,Filter和Reduce的实现
  2. mysql二进制文件复制_MySQL 主从复制:基于二进制文件复制配置详解
  3. python getopt参数参数自动补全_如何在Python中使用getopt / OPTARG?如果给出过多的参数(9),如何转移参数?...
  4. windows7卸载linux系统,win7下安全卸载linux系统
  5. 日本定了一个小目标,在2030年让五分之一的汽车实现自动驾驶
  6. 随机生成验证码(JAVA代码)
  7. java list去重工具_开发常用小工具类:list集合去重
  8. 差分管电路图_电子管差分放大电路改造方案
  9. 数学建模常见算法:插值算法
  10. php大量邮件,PHPMailer批量发送邮件的实例代码
  11. 计算机缓存怎样更改,计算机的缓存大小在哪设置?
  12. 1080p和1080i有什么区别?
  13. 通信原理SNR EbN0 EsN0理解
  14. layer进度条ajax,layui动态进度条详细。
  15. ps cc2019版为什么做图一复制图层就卡死_你所不知道的十个被藏起来的PS功能,超级实用!...
  16. 什么是幽默?什么是高级幽默?
  17. 如何在手机APP中通过H5方式集成监控摄像头实时直播画面
  18. Building dependency tree… Done Package aptitude is not available, but is referred to by another pac
  19. 华雨欢:多日震荡趋势明显,本周即将结束要开启每周大行情了
  20. Java 两个中文字符串异或问题

热门文章

  1. 如何更好的设计原理图符号(转)
  2. Java获取sql语句where条件后的语句
  3. 宝塔面板wordpress“知更鸟”主题升级到php7.3,出现“警告错误提示”的解决办法
  4. 用C8T6坐小车的第三天
  5. Java开发 - 消息队列之Kafka初体验
  6. Microsoft Project 常用快捷键
  7. 人鱼小姐主题曲我痛苦的爱铃声 人鱼小姐主题曲我痛苦的爱手机...
  8. 最短路径:Dijkstra算法(求单源最短路径)Floyd算法(求各顶点之间最短路径)
  9. 你需要的物流运输类报表,都在这里
  10. Spring配置数据源没有maxActive和maxWait参数解决方法