SnoopDougg
SnoopDougg

Reputation: 1609

How to reference sibling module in a test for my maven spring boot multi-module project?

How can I include a sibling module as a test dependency for my spring boot tests in maven?

I have a little multi-module maven project with a spring boot web app (querying-api ), a common library (domain-definitions), and an initializing script (database-loader).

I'm writing test cases in my querying-api module to test my API, and I would like to run my database-loader service before the tests begin to load the database with predefined test data, simultaneously testing that the loader still works. However I'm getting the following error when running mvn clean package:

[ERROR] Failed to execute goal on project querying-api: Could not resolve dependencies for project com.my.little.package:querying-api:jar:1.0.0: Failure to find com.my.little.package:database-loader:jar:1.0.0 in http://mycompanysnexusrepo.com/nexus/content/repositories/EAJAVA/ was cached in the local repository, resolution will not be reattempted until the update interval of EAJAVA has elapsed or updates are forced -> [Help 1]

The tests have no problem referencing the common domain-definitions library...


Parent POM File:

<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>
    <packaging>pom</packaging>
    <name>my-little-project</name>
    <description>Project for my little microservice</description>
    <groupId>com.my.little.package</groupId>
    <artifactId>my-little-project</artifactId>
    <version>1.0.0</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.8.RELEASE</version>
        <relativePath/>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring.security.version>2.3.5.RELEASE</spring.security.version>
        <springfox.version>2.7.0</springfox.version>
    </properties>

    <modules>
        <module>domain-definitions</module>
        <module>database-loader</module>
        <module>querying-api</module>
    </modules>

    <distributionManagement>
        <repository>
            <id>EAJAVA</id>
            <url>http://mycompanysnexusrepo.com/nexus/content/repositories/EAJAVA/</url>
        </repository>
    </distributionManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.my.little.package</groupId>
                <artifactId>domain-definitions</artifactId>
                <version>${project.version}</version>
            </dependency>

            <dependency>
                <groupId>com.my.little.package</groupId>
                <artifactId>database-loader</artifactId>
                <version>${project.version}</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

querying-api POM file (Spring boot app for querying the database):

<?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>
    <artifactId>querying-api</artifactId>
    <packaging>jar</packaging>
    <name>querying-api</name>
    <description>Spring boot app for querying the database</description>

    <parent>
        <groupId>com.my.little.package</groupId>
        <artifactId>my-little-project</artifactId>
        <version>1.0.0</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>com.my.little.package</groupId>
            <artifactId>domain-definitions</artifactId>
        </dependency>
        <dependency>
            <groupId>com.my.little.package</groupId>
            <artifactId>database-loader</artifactId>
            <version>${project.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <finalName>querying-api</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

domain-definitions POM file (Common library):

<?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>
    <parent>
        <groupId>com.my.little.package</groupId>
        <artifactId>my-little-project</artifactId>
        <version>1.0.0</version>
    </parent>

    <artifactId>domain-definitions</artifactId>
    <name>domain-definitions</name>
    <description>Classes to define the structure of the tables</description>
</project>

database-loader POM file:

<?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>
    <artifactId>database-loader</artifactId>
    <packaging>jar</packaging>
    <name>database-loader</name>
    <description>Java script to load data into the database for the first time</description>

    <parent>
        <groupId>com.my.little.package</groupId>
        <artifactId>my-little-project</artifactId>
        <version>1.0.0</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>com.my.little.package</groupId>
            <artifactId>domain-definitions</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

Upvotes: 0

Views: 1481

Answers (1)

Mohit Singh
Mohit Singh

Reputation: 534

This might be because of the library jar not found in the local repository. You need to first execute :
mvn clean install. This will install the lib jar in your local repository and maven can easily find it when you execute package goal.

Explained here: How are "mvn clean package" and "mvn clean install" different?

Furthermore, if you are using the sibling module only in tests then you can define the scope as test in dependency.

     <dependency>
        <groupId>com.my.little.package</groupId>
        <artifactId>domain-definitions</artifactId>
        <scope>test</scope>
    </dependency>

Upvotes: 0

Related Questions