Maven的pom.xml中resources标签的用法

spring-boot-starter-parent-2.4.1.pom文件中resources标签内容如下:

xml 复制代码
<build>
    <resources>
      <resource>
        <directory>${basedir}/src/main/resources</directory>
        <filtering>true</filtering>
        <includes>
          <include>**/application*.yml</include>
          <include>**/application*.yaml</include>
          <include>**/application*.properties</include>
        </includes>
      </resource>
      <resource>
        <directory>${basedir}/src/main/resources</directory>
        <excludes>
          <exclude>**/application*.yml</exclude>
          <exclude>**/application*.yaml</exclude>
          <exclude>**/application*.properties</exclude>
        </excludes>
      </resource>
    </resources>
</build>

先做一下实验,看一下结果,我们再来给出这个标签的含义是什么

rsource 下标签作用解释

其中包含的标签含义如下:

  • targetPath:指定了资源构建时的目标目录,默认为base direcory,即source目录结构下

    原文:This is not required if you simply put the resources in that directory structure at the source

  • filtering:决定是否将resources目录下的文件中的tokens(即通过@val@标识的变量)进行参数替换。这里的参数主要有两个来源,一个是来自于pom文件中的properties属性,一个是来源于外部的.properties文件(个人认为这个文件不可与springboot中的配置文件混为一谈)

    那么如何引用外部的.properties文件呢?通过另一个标签下的进行路径的指定。

  • directory:一个相对于POM文件的路径,指定了resources文件的路径。

  • includes:制定了包含文件的patterns,符合样式的且在directory目录下的文件都会包含进编译好的资源文件中

  • excludes:与includes对应,指定不包含在内的模式。如果和includes冲突,优先不包含。即includes和excludes同时包含某个文件,这个这个文件不会被包含打包进资源文件;

一、resources作用

1. 明确告诉Maven 需要打包编译的文件

在项目当中进行编译的时候,他默认是不会对某些文件进行编译的,例如在java文件夹下的mybatis当中的.xml文件,还有在resources文件夹当中有时候会存储一些资源文件,默认有些也是不进行编译的,这里的不进行编译指的是他不会编译到target文件夹当中,并且打包也是。

我们在正常开发项目的时候,有时候获取资源是获取的编译后的路径地址,他会在编译后的路径找不到文件(target文件夹),就是这个原因。

这时候就需要用到resources标签,在pom.xml添加,告诉maven我这些文件也需要编译,并且打包的时候需要打包进去。

2、配置文件取pom当中的值

在现实开发当中,我也遇到过,在pom文件当中配置参数,然后通过application.properties使用@@来进行取pom的值。假如我不想用application.properties取值,而是用了新创建的比如a.properties取值,这时候就需要在pom当中配置resources。

用法另见

二 实战

先在maven工程的resources目录下面创建2个配置文件

再修改pom文件,修改内容如下

xml 复制代码
<!--在properties标签中添加变量test.name-->
<properties>
		<test.name>测试内容test01</test.name>
</properties>
<!--在build标签中添加resources标签-->
<build>
		<resources>
			<resource>
				<directory>src/main/resources</directory>
				<filtering>true</filtering>
				<includes>
					<include>a.properties</include>
				</includes>
			</resource>
		</resources>
</build>

再在a.properties文件中引用pom文件中定义的变量值

xml 复制代码
a.name=${test.name}

b.properties文件中的内容如下

xml 复制代码
b.name=${test.name}

点击package 进行打包

打包完成后在target目录下的classes类路径下的内容如下

xml 复制代码
<build>
		<resources>
			<resource>
				<directory>src/main/resources</directory>
				<filtering>true</filtering>
				<includes>
					<include>a.properties</include>
				</includes>
			</resource>
		</resources>
</build>

从这里可以得出结论:上面这段配置的含义是如果filtering标签的值设置成true,默认值是false,则在工程进行编译打包的时候只会将工程的src/main/resources目录下的includes标签中的资源文件打包进去,并且会进行预编译(就是就将资源文件中引用pom文件中的properties标签中的变量,转化其对应的值),除了includes标签中的资源文件的其它资源文件都不会被打包进去

resources 标签下的 filtering标签 的用法和替换只另见

再修改pom文件的resources标签

xml 复制代码
<build>
		<resources>
			<resource>
				<directory>src/main/resources</directory>
				<filtering>true</filtering>
				<includes>
					<include>a.properties</include>
				</includes>
			</resource>
			<resource>
				<directory>src/main/resources</directory>
				<!--filtering标签的值默认是false,在这里可写可不写-->
				<filtering>false</filtering>
				<excludes>
					<exclude>a.properties</exclude>
				</excludes>
			</resource>
		</resources>
</build>

进行编译打包,结果如下

xml 复制代码
<resource>
    <directory>src/main/resources</directory>
    <!--filtering标签的值默认是false,在这里可写可不写-->
    <filtering>false</filtering>
    <excludes>
        <exclude>a.properties</exclude>
    </excludes>
</resource>

由此可知这段配置的含义是:在项目进行打包的时候会将src/main/resources目录下除了a.properties资源文件的其它资源文件打包进去,并且这些打包进去的资源文件的内容不会进行预编译,之前是什么样子,打完包之后还是什么样子

2.1 总结

如果项目打包的时候,想对某个资源文件中的内容进行预编译再打包进去,而对其他的资源文件不进行预编译处理打包到项目中,则可以按照如下配置进行处理

xml 复制代码
<resources>
			<resource>
				<directory>src/main/resources</directory>
				<filtering>true</filtering>
				<includes>
					<include>需要进行预编译的资源文件名称如:a.properties</include>
				</includes>
			</resource>
			<resource>
				<directory>src/main/resources</directory>
				<!--filtering标签的值默认是false,在这里可写可不写-->
				<filtering>false</filtering>
				<excludes>
					<exclude>上面includes标签中的资源文件名称如:a.properties</exclude>
				</excludes>
			</resource>
</resources>

2.2 resources标签结合mybatis使用

如果想要将mapper.xml文件和mapper接口同一个包中,并且在项目打包的时候需要作为资源文件也要打包进去,比如项目的结构如下

可以在pom文件中进行如下配置

xml 复制代码
<build>
		<resources>
			<!--将src/main/java目录下的所有xml文件都作为项目的资源文件,
				项目打包的时候也会打包进去
			-->
			<resource>
				<directory>src/main/java</directory>
				<includes>
                    <include>**/*.xml</include>
                </includes>
			</resource>
			<!--将项目的src/main/resources目录下的所有文件都作为项目的资源文件
				项目打包的时候也会打包进去
			-->
			<resource>
				<directory>src/main/resources</directory>
			</resource>
		</resources>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
</build>
相关推荐
码畜也有梦想1 天前
Maven中optional的作用
java·jenkins·maven
上官浩仁2 天前
springboot maven 多环境配置入门与实战
java·spring boot·maven
元直数字电路验证2 天前
新建Jakarta EE项目,Maven Archetype 选项无法加载出内容该怎么办?
java·maven
麦兜*2 天前
Docker 部署 MongoDB:单节点与副本集的最佳实践
java·spring boot·mongodb·spring cloud·docker·容器·maven
杨杨杨大侠3 天前
附录 1:🚀 Maven Central 发布完整指南:从零到成功部署
java·github·maven
nightunderblackcat3 天前
新手向:实现验证码程序
java·spring boot·spring·java-ee·kafka·maven·intellij-idea
代码雕刻家3 天前
3.1.Maven-课程介绍
java·maven
产幻少年3 天前
maven
java·maven
叶 落5 天前
[Maven 基础课程]再看下第一个 Maven 项目
maven·maven 入门·maven 基础课程