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

Van*_*kog 4 junit pom.xml maven maven-bom junit5

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

JUnit BOM:为了简化使用Maven或Gradle的依赖管理,现在在org.junit:junit-bom:5.2.0 Maven坐标下提供了物料清单POM。

首先,目前我的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>
Run Code Online (Sandbox Code Playgroud)

如果我正确地理解了上面的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>
Run Code Online (Sandbox Code Playgroud)

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

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

Jen*_*gsa 6

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


小智 6

以下使用surefire.plugin.version 2.22.2对我有用

junit-jupiter artifact 引入了所有其他必要的 artifact,在这种情况下不需要单独的直接依赖项。

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.junit</groupId>
      <artifactId>junit-bom</artifactId>
      <version>5.5.2</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>
<dependencies>
  <dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter</artifactId>
    <scope>test</scope>
  </dependency>
  <!--Optional: Supports running Junit4 along with Junit5 -->
  <dependency>
    <groupId>org.junit.vintage</groupId>
    <artifactId>junit-vintage-engine</artifactId>
    <scope>test</scope>
  </dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud)