maven中<scope>参数</scope>的配置

本文介绍Maven中依赖范围的含义及应用场景,并详细解释如何通过maven-shade-plugin插件解决打包过程中遇到的特定问题,如排除某些依赖、过滤元数据文件等。
参数名称具体功能
<scope>compile</scope>默认值,表示当前依赖包要参与当前项目的编译后续测试运行时打包
<scope>provided</scope>当前包只在编译和测试的时候使用,而不再后续的运行和打包的时候不会打包进来
<scope>test</scope>表示当前依赖包只参与测试工作
<scope>runtime</scope>表示当前依赖包只参与运行周期,其他跳过
<scope>system</scope>
从参与度和provided一致,不过被依赖项不会从maven远程仓库下载,而是从本地的系统拿。需要
systemPath属性来定义路径

解决maven项目中 无法打包生成空文件夹的问题

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.1.1</version>
                <executions>
                    <!-- Run shade goal on package phase -->
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <artifactSet>
                                <excludes>
                                    <exclude>org.apache.flink:force-shading</exclude>
                                    <exclude>com.google.code.findbugs:jsr305</exclude>
                                    <exclude>org.slf4j:*</exclude>
                                    <exclude>org.apache.logging.log4j:*</exclude>
                                </excludes>
                            </artifactSet>
                            <filters>
                                <filter>
                                    <!-- Do not copy the signatures in the META-INF folder.
                                    Otherwise, this might cause SecurityExceptions when using the JAR. -->
                                    <artifact>*:*</artifact>
                                    <excludes>
                                        <exclude>META-INF/*.SF</exclude>
                                        <exclude>META-INF/*.DSA</exclude>
                                        <exclude>META-INF/*.RSA</exclude>
                                    </excludes>
                                </filter>
                            </filters>
                            <transformers>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>com.lkr.flink.StreamingJob</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

 

<think>我们正在讨论Maven中的<scope>test</scope>。根据引用[1],我们知道Maven的<scope>元素定义了依赖项的作用范围,而test是其中一种作用范围。引用[2]则提到了在实际操作中,添加或移除<scope>test</scope>可能会影响构建行为。 根据Maven官方文档,我将详细介绍<scope>test</scope>: 1. **作用**:标记为test作用域的依赖仅在测试编译和测试运行时可用。这意味着: - 在编译主代码(src/main/java)时,这些依赖不会被包含在类路径中。 - 在编译测试代码(src/test/java)和执行测试(如mvn test)时,这些依赖会被包含。 - 在打包主代码(如mvn package)时,这些依赖不会被包含在生成的构件(如JAR)中。 2. **典型使用场景**:通常用于测试框架,如JUnit、TestNG、Mockito等。因为这些库只在测试阶段需要,不应该发布到生产环境中。 3. **与其他scope的区别**: - **compile**(默认):在编译、测试和运行时都需要,会被打包。 - **provided**:编译和测试时需要,但运行时由容器(如Tomcat)提供,不会被打包。 - **runtime**:测试和运行时需要,但编译时不需要(如JDBC驱动)。 - **test**:仅用于测试阶段。 - **system**:类似provided,但需要显式指定本地路径(不推荐使用)。 4. **使用方法**:在pom.xml中声明依赖时,添加<scope>test</scope>。例如: ```xml <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> ``` 5. **注意事项**: - 如果主代码引用了test作用域的依赖,会导致编译错误,因为主代码编译时这些依赖不可见。 - 引用[2]提到的一个现象:有时添加或移除<scope>test</scope>后,测试可能仍然能运行,但执行clean后问题复现。这通常是因为Maven的增量编译没有清理旧的编译结果,导致测试代码还能引用到本应不可用的依赖。因此,在修改依赖范围后,建议执行`mvn clean test`以确保完全清理和重新构建。 根据以上信息,我们可以生成相关问题。 </think>### Maven `<scope>test</scope>` 详细指南 #### 一、核心作用与机制 1. **依赖作用域隔离** `<scope>test</scope>` 将依赖限制在**测试阶段**: - 仅在 `mvn test` 执行时生效 - 编译主代码(`src/main/java`)时无效 - 打包(`mvn package`)时**不包含**在最终构件中[^1] 2. **生命周期绑定** 作用范围覆盖整个测试生命周期: $$ \begin{array}{c} \text{测试编译} \rightarrow \text{测试执行} \rightarrow \text{测试报告} \\ \downarrow \\ \text{依赖可见} \end{array} $$ 但在 `install` 或 `deploy` 阶段自动排除[^2] #### 二、典型应用场景 ```xml <!-- JUnit 测试框架 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> <scope>test</scope> <!-- 仅测试阶段加载 --> </dependency> <!-- Mockito 模拟框架 --> <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-core</artifactId> <version>4.11.0</version> <scope>test</scope> </dependency> ``` **优势**: - 避免测试库污染生产环境 - 减小打包体积(通常减少 15%-40%) - 防止测试专用 API 被误用于生产代码 #### 三、与其他 Scope 的对比 | Scope 类型 | 编译主代码 | 编译测试代码 | 运行时 | 打包包含 | 典型用例 | |----------------|------------|--------------|--------|----------|-----------------------| | **test** | ❌ | ✅ | ❌ | ❌ | JUnit, TestNG | | compile(默认) | ✅ | ✅ | ✅ | ✅ | Spring Core | | provided | ✅ | ✅ | ❌ | ❌ | Servlet API | | runtime | ❌ | ✅ | ✅ | ✅ | JDBC 驱动 | | system | ✅ | ✅ | ✅ | ❌ | 本地 jar 文件 | #### 四、使用注意事项 1. **作用域冲突处理** 当依赖被多处声明时,作用域按 **"最近定义优先"** 原则: ```xml <!-- 模块A --> <dependency> <groupId>com.example</groupId> <artifactId>libX</artifactId> <version>1.0</version> <scope>test</scope> <!-- 最终生效 --> </dependency> <!-- 模块B --> <dependency> <groupId>com.example</groupId> <artifactId>libX</artifactId> <version>1.0</version> <scope>compile</scope> </dependency> ``` 2. **多模块项目传递** test 作用域的依赖**不会传递**到子模块,需显式声明: ```mermaid graph TD 父模块--> |不传递|子模块 子模块--> |需独立声明|JUnit ``` 3. **常见问题解决** - **问题现象**:移除 `<scope>test</scope>` 后仍能编译测试代码 - **原因**:Maven 本地仓库缓存未清除 - **修复**:执行 `mvn clean install -U` 强制更新依赖[^2] #### 五、最佳实践 1. **单元测试专用库** 所有测试框架(JUnit/Mockito)必须声明 test scope 2. **集成测试隔离** 使用 `maven-failsafe-plugin` 分离集成测试依赖: ```xml <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-failsafe-plugin</artifactId> <dependencies> <!-- 集成测试专用依赖 --> <dependency>...</dependency> </dependencies> </plugin> ``` 3. **依赖检查** 用 `mvn dependency:analyze` 检测误用的 test-scope 依赖 > 经验法则:若依赖仅在 `src/test/java` 中使用,必须声明 `<scope>test</scope>`[^1]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值