簡體   English   中英

如何處理maven中的生成源

[英]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將按照以下順序建設項目:

  1. 我的項目(pom)
  2. 客戶端(罐)
  3. my-app-clients (pom) # 這是一個虛擬的,這一步不發布任何東西
  4. API 集成測試

請參閱有關 maven 反應器構建順序的官方文檔

免責聲明:我認為這不是一個好的解決方案,我不建議將它用於實際項目。 但我認為很高興知道存在這樣的選擇。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM