[英]How to deal with generated sources in 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
是一個模塊,它根據 swagger 規范生成工件 ( my-app-clients
)。 換句話說, 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
但是當我第一次嘗試執行時(假設我清空了 local.m2 存儲庫)
mvn clean verify -Pgenerate-clients,run-integration-tests
我遇到錯誤:無法解決項目 edu.yuriiknowsjava:api-integration-tests:0.0.1.0-SNAPSHOT 的依賴關系:無法在...中找到 edu.yuriiknowsjava:my-app-clients:0.0.1.0-SNAPSHOT 。
所以我的問題是有沒有辦法告訴 maven 類似: “嘿 maven,你不用擔心my-app-clients
工件,它會在執行-Pgenerate-clients
profile 后生成”
我找到了解決這個問題的一個骯臟的解決方法——我添加了dummy-client
模塊,它用於占位符的目的。
問題如下: api-integration-tests
模塊需要my-app-clients
作為依賴項,但我們的多模塊項目中沒有這樣的工件。 my-app-clients
工件將在執行clients
模塊后生成。
因此,我創建了一個虛擬子模塊,其工件 ID my-app-clients
以保持 maven 反應堆的快樂。
/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將按照以下順序建設項目:
免責聲明:我認為這不是一個好的解決方案,我不建議將它用於實際項目。 但我認為很高興知道存在這樣的選擇。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.