(1)默认 Spring Boot 项目结构,资源文件放置在 src/main/resources 中,测试的资源文件在 src/test/resources 中。
src/main/resources src/test/resources的区别:
(2)将项目打包后,解压 可以发现原先
(3)但有时我们的资源文件并不一定是放在 src/main/resources目录下,比如我的项目通常会将资源文件放在src/test/resources
**原因:**根据实践经验表明,测试完后的配置项是最完整的,且经常会忘记替换正式版参数,因而选择将配置文件全部放置在 src/test/resources 目录下。
(4)又比如 mybatis 的 mapper.xml 文件,我们习惯把它和 Mapper.java 放一起
(5)但上面这两种情况的资源文件,在使用 maven 打包时是不会被打包进 jar 的。这时候我们便要指定需要打包的资源文件,这个有如下两种方法可以实现。
(1) 标签位于 标签内,用于指定项目资源文件的位置。比如下面配置我们指定了 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``>`
(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``>`
(1) 除了使用 标签外,我们也可以使用 maven-resources-plugin 插件实现同样的目的。比如下面配置把 src/test/resources 目录下的资源文件打包到 classes 目录下
org.springframework.boot
spring-boot-maven-plugin
org.apache.maven.plugins
maven-resources-plugin
my-resources
process-resources
copy-resources
${basedir}/target/classes
${basedir}/src/test/resources
*.properties
*.xml
(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``>`
原文出自: 转载请保留原文链接: https://www.hangge.com/blog/cache/detail_2887.html
```plugins
>```build``>`
原文出自: 转载请保留原文链接: https://www.hangge.com/blog/cache/detail_2887.html