如何在我的 Maven Spring Boot 多模块项目的测试中引用兄弟模块?
Posted
技术标签:
【中文标题】如何在我的 Maven Spring Boot 多模块项目的测试中引用兄弟模块?【英文标题】:How to reference sibling module in a test for my maven spring boot multi-module project? 【发布时间】:2020-03-20 23:02:05 【问题描述】:如何在 Maven 中包含兄弟模块作为我的 Spring Boot 测试的测试依赖项?
我有一个小的多模块 maven 项目,其中包含一个 Spring Boot Web 应用程序 (querying-api )、一个公共库 (domain-definitions) 和一个初始化脚本(数据库加载器)。
我正在我的 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]
测试引用通用域定义库没有问题...
父 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】:这可能是因为在本地存储库中找不到库 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>
【讨论】:
糟糕,如果我的帖子不清楚,很抱歉,domain-definitions
库工作正常。而且我可以将它作为依赖项包含在我的应用程序querying-api
中,而无需先运行mvn clean install
。但由于某种原因,java 脚本database-loader
无法作为测试依赖项导入。我不知道为什么我可以毫无问题地导入domain-definitions
,但不能通过mvn clean package
导入database-loader
。以上是关于如何在我的 Maven Spring Boot 多模块项目的测试中引用兄弟模块?的主要内容,如果未能解决你的问题,请参考以下文章
如何正确设置 Maven Spring Boot 多模块项目?
如何将maven多模块spring boot部署到docker容器
如何在 OpenShift 上部署多模块 maven spring boot 应用程序
如何将 Spring Boot 项目集成到已经存在的多模块 Maven 项目中?