You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Maven Profile拆分UI/API依赖后构建报错问题咨询

Maven Profile拆分UI/API测试依赖遇到的问题及解决

问题描述

我维护一个同时运行UI和API测试的Maven项目,原POM里依赖太多难维护,就用Maven Profile做拆分:UI和API两个Profile分别放对应依赖,公共依赖放根dependencies里。但执行mvn clean install -P UI时,API测试类报'package io.restassured.http does not exist'(rest-assured在API Profile里)。想知道这种模块化方式可行吗?还是我配置错了?

附原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>
<groupId>org.example</groupId>
<artifactId>TestProfile</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.19.1</version>
            <configuration>
                <testFailureIgnore>true</testFailureIgnore>
            </configuration>
        </plugin>
    </plugins>
</build>

<profiles>
    <profile>
        <id>UI</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>

        <dependencies>

            <dependency>
                <groupId>org.seleniumhq.selenium</groupId>
                <artifactId>selenium-java</artifactId>
                <version>4.18.1</version>
            </dependency>

            <!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-compiler-plugin -->
            <dependency>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.12.1</version>
            </dependency>

            <!-- https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager -->
            <dependency>
                <groupId>io.github.bonigarcia</groupId>
                <artifactId>webdrivermanager</artifactId>
                <version>5.7.0</version>
            </dependency>

        </dependencies>
    </profile>

    <profile>
        <id>API</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <dependencies>

            <!-- https://mvnrepository.com/artifact/io.rest-assured/rest-assured -->
            <dependency>
                <groupId>io.rest-assured</groupId>
                <artifactId>rest-assured</artifactId>
                <version>5.4.0</version>
            </dependency>

            <!-- https://mvnrepository.com/artifact/org.json/json -->
            <dependency>
                <groupId>org.json</groupId>
                <artifactId>json</artifactId>
                <version>20240303</version>
            </dependency>

            <dependency>
                <groupId>com.googlecode.json-simple</groupId>
                <artifactId>json-simple</artifactId>
                <version>1.1.1</version>
            </dependency>

            <!-- https://mvnrepository.com/artifact/org.hamcrest/hamcrest -->
            <dependency>
                <groupId>org.hamcrest</groupId>
                <artifactId>hamcrest</artifactId>
                <version>2.2</version>
                <scope>test</scope>
            </dependency>

        </dependencies>
    </profile>

</profiles>

<dependencies>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>7.9.0</version>
        <scope>test</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-compiler-plugin -->
    <dependency>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.12.1</version>
    </dependency>
</dependencies>

<properties>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>

问题原因分析

  1. Profile激活逻辑错误:你给UI和API两个Profile都加了<activeByDefault>true</activeByDefault>,默认两个都会激活。但当你指定-P UI时,Maven会禁用所有默认激活的Profile,只启用你指定的UI Profile,导致API依赖完全不引入,而API测试类还在源码目录里,编译自然找不到包。
  2. 测试类未隔离:所有测试类(UI和API)都在同一个目录下,不管激活哪个Profile,Maven都会尝试编译所有类,缺少依赖的类必然报错。
  3. 冗余配置错误:你把maven-compiler-plugin当成依赖引入了,这是Maven插件,应该放在<build><plugins>里,而非<dependencies>,UI Profile和根依赖里的这个配置都是错的。

解决方案

方案1:修正Profile+隔离测试类(单模块场景)

  • 去掉两个Profile的<activeByDefault>true</activeByDefault>,或只保留一个默认激活(比如UI)。运行API测试时用-P API指定。
  • 把UI和API测试类分目录存放:
    • src/test/java/ui/:UI测试类
    • src/test/java/api/:API测试类
  • 在每个Profile里配置surefire插件,只运行对应目录的测试:
<profile>
    <id>UI</id>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19.1</version>
                <configuration>
                    <includes>
                        <include>ui/**/*.java</include>
                    </includes>
                    <testFailureIgnore>true</testFailureIgnore>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <!-- UI依赖... -->
</profile>
<profile>
    <id>API</id>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19.1</version>
                <configuration>
                    <includes>
                        <include>api/**/*.java</include>
                    </includes>
                    <testFailureIgnore>true</testFailureIgnore>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <!-- API依赖... -->
</profile>

方案2:拆分多模块项目(更推荐,长期维护友好)

如果项目规模不小,拆成多模块更清晰:

  • 父模块:管理公共依赖(比如TestNG)
  • ui-test子模块:只放UI测试代码+UI相关依赖
  • api-test子模块:只放API测试代码+API相关依赖
  • 可以通过父模块的Profile控制构建哪个子模块,或直接单独构建:mvn clean install -pl ui-test

立即修复的小问题

删除所有<dependencies>中的maven-compiler-plugin依赖,插件配置只保留在<build><plugins>里。

内容的提问来源于stack exchange,提问作者user23656021

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 11:04:51