maven pom文件 resources详解 filtering详解

maven打包时,默认不会将当前pom文件所在module下的file文件打包进target,但似乎有时业务是需要存放一些资源文件

你可能会出现:资源找不到mybatis mapper报错:Invalid bound statement (not found) 等错误
所以现在讲一下关于<resources>配置


directory:配置生效的文件夹目录
includes:指定文件夹目录下的指定资源文件都会被打包 可用通配符
excludes:指定文件夹目录下的指定资源文件都不会被打包 可用通配符
filtering:是否对指定文件夹目录下的指定资源文件进行占位符填充处理

** 表示多级目录匹配。* 表示一级目录匹配

<resources>
    <resource>
        <directory>src/main/java</directory>
        <includes>
            <!-- 把src/main/java下xml结尾的文件打包到target-->
            <include>**/*.xml</include>
        </includes>
        <excludes>
            <!-- 把src/main/java下A.xml文件过滤掉-->
            <exclude>**/A.xml</exclude>
        </excludes>
    </resource>
    <resource>
        <directory>src/main/resources</directory>
        <includes>
            <include>**/*.xml</include>
            <include>**/*.png</include>
            <include>**/*.jpg</include>
            <include>**/*.pdf</include>
            <include>**/*.properties</include>
            <include>**/*.yml</include>
        </includes>
        <!--用pom文件中定义的属性替换指定目录下properties文件里的占位符(${XXX} / @XXX@)-->
        <filtering>true</filtering>
    </resource>
</resources>

不配置resources的默认情况:
src/main/resources:Maven 会将文件夹中的内容全部复制打包到 target 中
src/main/java: Maven 只会将文件夹中的 Java 文件编译,然后将编译后的 .class 文件打包到 target 中


filtering

false不开启配置文件变量替换 true开启配置文件变量替换
在配置文件中使用${xx.xx} @xx.xx@ 占位符进行替换
默认是false

在 src/main/resources 下创建一个配置文件 test.properties

app.var1=${app.var1}
app.var2=@app.var2@

我们在 pom.xml 文件中设置

 <properties>
     <app.var1>this is a constant1</app.var1>
     <app.var2>this is a constant2</app.var2>
 </properties>

设置filtering = true
进行mvn编译,在target中可以看到变量被替换了:

在这里插入图片描述

<think>好的,我现在需要帮助用户了解Spring Boot项目中pom.xml文件的常见标签及其功能和配置方法。首先,我要回忆一下用户提供的引用内容,确保引用正确的信息。根据引用[1],pom.xml是Maven项目的核心配置文件,管理依赖、插件、构建配置等。关键点包括继承spring-boot-starter-parent,使用starter依赖,配置插件,以及用properties和dependencyManagement统一版本。引用[3]提到了如何引入本地JAR包,这涉及到dependencies标签的使用。 接下来,我需要确定用户的需求是了解各个标签的作用和配置方法,比如dependencies、plugins、parent等。我需要逐个解释这些标签,并给出示例。同时,用户可能作为刚接触Spring Boot的开发者,需要清晰的步骤和示例代码,所以应该用简单明了的语言,避免过于技术性的术语。 首先,parent标签用于继承Spring Boot的父项目,这样可以简化配置,统一版本管理。这部分在引用[1]中提到过,应该引用。然后是dependencies标签,这里需要说明如何引入starter依赖,比如spring-boot-starter-web,以及如何管理版本。还要提到如何添加本地依赖,如引用[3]中的例子。接着是properties标签,用于定义属性,比如Java版本或Spring Boot版本。然后是dependencyManagement,虽然用户可能较少直接使用,但需要说明它在多模块项目中的作用。 然后是build和plugins部分,需要介绍spring-boot-maven-plugin的作用,如何配置打包和运行,这也是引用[1]中的内容。modules标签可能在多模块项目中使用,但用户的问题可能更关注单项目,所以可以简要提及。最后,profiles用于不同环境的配置,如开发和生产环境的不同设置。 在组织内容时,需要结构清晰,每个标签作为一个部分,先说明作用,再给配置示例,并适当引用提供的资料。同时,要确保示例代码的格式正确,使用正确的XML语法。可能还需要检查是否有其他常见标签被遗漏,比如repositories或pluginRepositories,但根据用户的问题,可能集中在最常用的几个标签上。 最后,生成相关问题,帮助用户进一步学习,比如版本管理方法、多模块配置、环境配置差异等。确保问题与主题相关,并且能够引导用户深入理解pom.xml的更多细节。</think>### Spring Boot项目中pom.xml文件常见标签详解 #### 1. `<parent>`:继承父项目 **作用**:通过继承`spring-boot-starter-parent`,项目自动继承Spring Boot的默认配置(如依赖版本、插件配置等),无需手动指定版本号[^1]。 **配置示例**: ```xml <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.1.0</version> <relativePath/> <!-- 本地无缓存时从远程仓库获取 --> </parent> ``` --- #### 2. `<dependencies>`:声明项目依赖 **作用**:定义项目所需的第三方库或模块,例如: - **Starter依赖**:如`spring-boot-starter-web`(Web开发)、`spring-boot-starter-data-jpa`(数据库访问)。 - **本地JAR包**:通过`<dependency>`标签引入本地仓库中的自定义库[^3]。 **配置示例**: ```xml <dependencies> <!-- Spring Boot Web模块 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 自定义本地库 --> <dependency> <groupId>com.example</groupId> <artifactId>custom-library</artifactId> <version>1.0.0</version> </dependency> </dependencies> ``` --- #### 3. `<properties>`:统一管理属性 **作用**:定义全局变量(如版本号、编码格式),便于集中维护。 **配置示例**: ```xml <properties> <java.version>17</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <spring-boot.version>3.1.0</spring-boot.version> </properties> ``` --- #### 4. `<dependencyManagement>`:多模块依赖管理 **作用**:在多模块项目中统一管理子模块的依赖版本,避免版本冲突。 **配置示例**: ```xml <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring-boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> ``` --- #### 5. `<build>`:构建配置 **作用**:定义构建过程中使用的插件和资源配置,核心是`spring-boot-maven-plugin`,支持打包为可执行JAR[^1]。 **配置示例**: ```xml <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> <!-- 资源文件过滤 --> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> </build> ``` --- #### 6. `<profiles>`:环境配置 **作用**:定义不同环境(开发、测试、生产)的差异化配置,如激活条件、依赖切换。 **配置示例**: ```xml <profiles> <profile> <id>dev</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <env>development</env> </properties> </profile> <profile> <id>prod</id> <properties> <env>production</env> </properties> </profile> </profiles> ``` --- #### 7. `<modules>`:多模块项目配置 **作用**:声明子模块,适用于大型项目分模块开发。 **配置示例**: ```xml <modules> <module>user-service</module> <module>order-service</module> </modules> ``` --- ### 配置对比表 | 标签 | 典型应用场景 | 是否必选 | |-----------------------|-------------------------------------|------------------| | `<parent>` | 继承Spring Boot默认配置 | 推荐使用 | | `<dependencies>` | 引入功能模块依赖 | 必选 | | `<build>` | 配置打包插件和资源过滤 | 必选 | | `<profiles>` | 多环境配置切换 | 可选 |
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值