Creating Test Jars in Maven for multi-module projects
Thomas

Thomas @tleipzig

About: Working with Spring and Spring Boot for over a decade, I wrote https://bootify.io - a Spring Boot App Generator

Location:
Berlin
Joined:
Aug 17, 2020

Creating Test Jars in Maven for multi-module projects

Publish Date: Mar 3
1 0

In multi-module projects, individual modules often contain classes or resources that should also be used in modules based on them - e.g. for initializing test users. In Maven, the maven-jar-plugin can be used to create a referenceable test library for this purpose.

Suppose we have a simple Spring Boot application with two modules "base" and "web". An integration test in "web" now needs to access a method initTestUsers, which is stored inside the "src/test/java" section in "base".

First, the pom.xml of the module "base" must be extended to provide a jar of the test classes.

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.4.2</version>
            <executions>
                <execution>
                    <goals>
                        <goal>test-jar</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
Enter fullscreen mode Exit fullscreen mode

  Adding maven-jar-plugin to the module providing the test data

Now the new library can be referenced from "web". The scope "test" makes it available for testing only, so it will not be included in the final "fat jar" of our Spring Boot executable.

<dependency>
    <groupId>${project.groupId}</groupId>
    <artifactId>base</artifactId>
    <classifier>tests</classifier>
    <type>test-jar</type>
    <version>${project.version}</version>
    <scope>test</scope>
</dependency>
Enter fullscreen mode Exit fullscreen mode

  Referencing the new test jar

Using Bootify, prototypes for Spring Boot applications can be created within minutes - develop a concept within the team first and then directly focus on the business logic. In the Professional plan, the multi-module option is available, which automatically sets up the maven-jar-plugin for Maven projects.

» Learn more

Comments 0 total

    Add comment