首页 > 解决方案 > 如何处理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>

clientsmy-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配置文件后生成”

标签: javamaven

解决方案


我找到了解决此问题的肮脏解决方法-我添加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 将按以下顺序构建项目:

  1. 我的项目(pom)
  2. 客户(罐子)
  3. my-app-clients (pom) # 这是一个假人,这一步不发布任何东西
  4. api-集成测试

请参阅有关 maven reactor 构建顺序的官方文档

免责声明:我认为这不是一个好的解决方案,我不建议将它用于实际项目。但我认为很高兴知道存在这样的选择。


推荐阅读