maven - 如何在 Maven 依赖项中使用 JUnit 5.2 BOM?

标签 maven junit pom.xml junit5 maven-bom

根据新发布的JUnit v. 5.2,现在有一个BOM:

JUnit BOM: To ease dependency management using Maven or Gradle, a Bill of Materials POM is now provided under the org.junit:junit-bom:5.2.0 Maven coordinates.



作为起点,目前我的 POM 如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.bosspanda.tmp</groupId>
    <version>0.1-SNAPSHOT</version>

    <artifactId>tmp</artifactId>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <maven.compiler.source>10</maven.compiler.source>
        <maven.compiler.target>10</maven.compiler.target>
        <java.version>10</java.version>
        <junit.version>4.12</junit.version>
        <junit.jupiter.version>5.2.0</junit.jupiter.version>
        <junit.vintage.version>5.2.0</junit.vintage.version>
        <junit.platform.version>1.2.0</junit.platform.version>
    </properties>
        <dependencies>
            <dependency>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter-api</artifactId>
                <version>${junit.jupiter.version}</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter-engine</artifactId>
                <version>${junit.jupiter.version}</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter-params</artifactId>
                <version>${junit.jupiter.version}</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
                <version>${junit.vintage.version}</version>
                <scope>test</scope>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit.version}</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
</project>

如果我正确理解了上面的这个 BOM 发行说明,它是这个 junit <dependency> 的简化。标记到单个 BOM 依赖项中。

但是,我在将它集成到我的项目的 pom.xml 中时遇到了严重的麻烦。在查看了链接资源 (https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Importing_Dependencies) 之后,我得出结论,我必须用一个单独的依赖项替换不同的依赖项:
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.junit</groupId>
            <artifactId>junit-bom</artifactId>
            <version>5.2.0</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

但是有了这个 IntelliJ IDEA (v. 2018.1.3 Ultimate x64, Maven v. 3.5.3, target JDK 10.0.1) 似乎不知道 <dependencyManagement> 是什么标签是并且不解析其内容。项目结构中未注册任何模块。
但如果我删除 <dependencyManagement>标记,BOM 不会被 IDEA 加载。
我还尝试使用 maven 插件通过 IDEA 添加 BOM 的 maven 坐标,但是当它找到不同的 junit 包时,它没有找到 BOM 并且无法从 org.junit:junit-bom:5.2.0 下载任何东西

如何将此 BOM 文件添加到我的 pom 依赖项中?
谢谢!

最佳答案

引用 <dependencyManagement><dependencies> 下的 bom 文件仅管理要兼容的版本。您仍然需要在 <dependencies> 下声明所有需要的依赖项。但没有 <version> .这就是 Maven bom 引用的工作方式。 IntelliJ 也可以这样处理它们。

关于maven - 如何在 Maven 依赖项中使用 JUnit 5.2 BOM?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50241638/

相关文章:

java - 如何使用 GhostDriver 和 PhantomJS 减少从 Web 获取 URL 页面的时间

java - 使用 Gradle 从另一个项目运行 JUnit 测试?

java - Mocking 调用服务方法时

java - 无法构建 pom 文件。发生未知编译问题

shell - maven在Linux和Windows平台上调用外部脚本

java - 获取NoSuchMethodError : org. hamcrest.core.StringContains.containsStringIgnoringCase

java - 从内部存储库 maven 控制 jar 名称

java - 如何在 Spring Boot 中配置 Maven Liquibase 插件?

java - Maven 通过直接 http ://link 安装 .jar 文件

java - 模拟 BindingProvider 和生成的 stub 类时出现问题