java - 如何处理maven中生成的源
问题描述
我有一个多模块项目
/my-project
/api
/service
/clients
/api-integration-tests
以下是来自父 pom 的配置文件:
<profiles>
<profile>
<id>build-app</id>
<modules>
<module>api</module>
<module>service</module>
</modules>
<profile>
<profile>
<id>generate-clients</id>
<modules>
<module>clients</module>
</modules>
<profile>
<profile>
<id>run-integration-tests</id>
<modules>
<module>api-integration-tests</module>
</modules>
<profile>
</profiles>
clients
my-app-clients
是一个基于 swagger 规范生成工件 () 的模块。换句话说,clients
模块将my-app-clients
生成的依赖项放在 maven 存储库中。
api-integration-tests
需要my-app-clients
作为依赖项:
<dependencies>
<dependency>
<groupId>edu.yuriiknowsjava</groupId>
<artifactId>my-app-clients</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
现在要运行集成测试,我必须执行 3 个命令:
# Build my app
mvn clean install -Pbuild-app
# Generate an artifact `my-app-clients`
mvn clean install -Pgenerate-clients
# Run tests
mvn clean verify -Prun-integration-tests
我想将其简化为 2 个命令
# Build my app
mvn clean install -Pbuild-app
# Generate `my-app-clients` artifact and run tests
mvn clean verify -Pgenerate-clients,run-integration-tests
但是当我第一次尝试执行时(假设我已经清空了本地 .m2 存储库)
mvn clean verify -Pgenerate-clients,run-integration-tests
我遇到一个错误: 无法解决项目 edu.yuriiknowsjava:api-integration-tests:0.0.1.0-SNAPSHOT:Failure to find edu.yuriiknowsjava:my-app-clients:0.0.1.0-SNAPSHOT in ... 的依赖关系。
所以我的问题是有没有办法告诉maven这样的东西:“嘿,maven,你不用担心my-app-clients
工件,它会在执行-Pgenerate-clients
配置文件后生成”
解决方案
我找到了解决此问题的肮脏解决方法-我添加dummy-client
了用于占位符目的的模块。
问题如下:api-integration-tests
模块需要my-app-clients
作为依赖项,但我们的多模块项目中没有这样的工件。执行模块my-app-clients
后将生成工件clients
。
所以我创建了一个具有工件 id 的虚拟子模块,my-app-clients
以保持 maven reactor 快乐。
/my-project
/api
/service
/clients
/api-integration-tests
/dummy-client # DUMMY!
这是 dummy 的 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">
<parent>
<artifactId>my-project</artifactId>
<groupId>edu.yuriiknowsjava</groupId>
<version>0.0.1.0-SNAPSHOT</version>
</parent>
<packaging>pom</packaging>
<modelVersion>4.0.0</modelVersion>
<!-- This is the key element. We want to trick maven into thinking that this dummy is the missing dependency maven reactor wasn't able to resolve! -->
<artifactId>my-app-clients</artifactId>
<!-- We want to put explicit dependency, so that maven build clients and then - this dummy -->
<modules>
<module>../clients</module>
</modules>
<!-- This is a dummy to mock "my-app-clients" artifact to make maven reactor happy -->
<build>
<plugins>
<plugin>
<!-- We don't won't to install this dummy into the local repo -->
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
</project>
然后我不得不修改run-integration-tests
配置文件以包含我的虚拟模块。
<profile>
<id>run-integration-tests</id>
<modules>
<module>dummy-client</module>
<module>api-integration-tests</module>
</modules>
<profile>
现在,当我执行
mvn clean verify -Pgenerate-clients,run-integration-tests
Maven reactor 将按以下顺序构建项目:
- 我的项目(pom)
- 客户(罐子)
- my-app-clients (pom) # 这是一个假人,这一步不发布任何东西
- api-集成测试
请参阅有关 maven reactor 构建顺序的官方文档。
免责声明:我认为这不是一个好的解决方案,我不建议将它用于实际项目。但我认为很高兴知道存在这样的选择。
推荐阅读
- android - 如何使用 Xamarin 播放暂停音频
- javascript - 集中无线电组 Vuetify
- python - 用html文档中的元素替换多个字符串
- server - Payara server 5 asadmin 命令无法读取用户输入
- excel - Python中的广义减少梯度非线性求解器
- javascript - 在使用 while 循环创建的表单中重命名输入字段 ID
- python - 如何使用 PyODBC 从 MS Access 填充表单
- rust - 为什么我不能对引用的 Vec 使用可变引用两次,如果它的生命周期与其内容相同?
- java - 我的 Card 类有什么问题,我不希望它是抽象的
- c# - 在 Windows 7 上检索 Windows Defender 扫描状态