如何在我的Maven Spring Boot多模块项目的测试中引用同级模块?

时间:2019-11-25 15:47:20

标签: maven spring-boot spring-boot-test multi-module

如何在Maven中将同级模块作为我的Spring Boot测试的测试依赖项?

我有一个带有弹簧启动Web应用程序( querying-api ),一个公共库( domain-definitions )和一个初始化程序的多模块maven项目。脚本(数据库加载器)。

我正在 querying-api 模块中编写测试用例以测试我的API,并且我想在测试开始之前运行 database-loader 服务使用预定义的测试数据加载数据库,同时测试加载程序是否仍在工作。但是,运行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]

测试没有问题,可以引用常见的 domain-definitions 库...


父POM文件:

<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文件(用于查询数据库的Spring Boot应用程序):

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

域定义 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>
    <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>

数据库加载器 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>
    <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>

1 个答案:

答案 0 :(得分:0)

这可能是由于在本地存储库中找不到库jar。您需要先执行:
mvn clean install。 这会将lib jar安装在本地存储库中,并且在执行 package 目标时,maven可以轻松找到它。

此处解释:How are "mvn clean package" and "mvn clean install" different?

此外,如果仅在测试中使用同级模块,则可以将范围定义为 test 依赖项。

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