Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use JUnit 5.2 BOM in Maven dependency?

According to the newly released v. 5.2 of JUnit, there is now a 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.

As a starting point, currently my POM looks like this:

<?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>

If I understand this BOM release note above correctly, it is a simplification of this junit <dependency> tags into a single BOM dependency.

However, I have stark troubles integrating it into my project's pom.xml. After having a look at the linked resource (https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Importing_Dependencies) I came to the conclusion that I have to replace the distinct dependencies with a single:

<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>

But with this in place IntelliJ IDEA (v. 2018.1.3 Ultimate x64, Maven v. 3.5.3, target JDK 10.0.1) does not seem to know what the <dependencyManagement> tag is and does not parse its content. No modules are registered in the project structure.
But also if I remove the <dependencyManagement> tag, the BOM does not get loaded by IDEA.
I also tried adding the maven coordinates of the BOM via IDEA by using the maven plugin, but while it finds the distinct junit packages, it does not find the BOM and cannot download anything from org.junit:junit-bom:5.2.0

How do I add this BOM file to my pom dependency?
Thank you!

like image 353
Vankog Avatar asked May 08 '18 20:05

Vankog


1 Answers

Referencing a bom file under <dependencyManagement><dependencies> only manages versions to be compatible. You still need to declare all needed dependencies under <dependencies> but without <version>. Thats how Maven bom references work. IntelliJ can handle them that way too.

like image 67
Jens Piegsa Avatar answered Sep 22 '22 23:09

Jens Piegsa