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

如何创建自定义Maven任务以执行指定代码及特定测试类

要实现mvn sanity这类自定义命令来运行指定目录下的测试,最常用且简便的方案是结合Maven Profiles和命令别名(或直接使用Profile激活命令),下面是具体的实现步骤:

方案一:使用Maven Profiles + 直接命令(基础版)

这种方式无需额外脚本或插件,通过配置Profile来指定测试类的范围,然后用命令激活Profile即可。

1. 在pom.xml中配置测试Profiles

在项目的pom.xml中添加以下Profile配置,分别指定sanity、integration、regression三个目录的测试类范围:

<profiles>
    <!-- Sanity测试Profile -->
    <profile>
        <id>sanity</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>3.2.5</version> <!-- 建议使用最新稳定版 -->
                    <configuration>
                        <!-- 匹配sanity目录下所有以Test.java结尾的测试类 -->
                        <!-- 如果你的测试类命名规则不同(比如*Tests.java),请调整这里的匹配规则 -->
                        <includes>
                            <include>**/spec/sanity/**/*Test.java</include>
                        </includes>
                    </configuration>
                    <executions>
                        <execution>
                            <id>run-sanity-tests</id>
                            <phase>test</phase>
                            <goals>
                                <goal>test</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>

    <!-- Integration测试Profile -->
    <profile>
        <id>integration</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>3.2.5</version>
                    <configuration>
                        <includes>
                            <include>**/spec/integration/**/*Test.java</include>
                        </includes>
                    </configuration>
                    <executions>
                        <execution>
                            <id>run-integration-tests</id>
                            <phase>test</phase>
                            <goals>
                                <goal>test</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>

    <!-- Regression测试Profile -->
    <profile>
        <id>regression</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>3.2.5</version>
                    <configuration>
                        <includes>
                            <include>**/spec/regression/**/*Test.java</include>
                        </includes>
                    </configuration>
                    <executions>
                        <execution>
                            <id>run-regression-tests</id>
                            <phase>test</phase>
                            <goals>
                                <goal>test</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

2. 运行指定测试

配置完成后,你可以通过以下命令运行对应目录的测试:

# 运行sanity测试
mvn test -Psanity

# 运行integration测试
mvn test -Pintegration

# 运行regression测试
mvn test -Pregression
方案二:配置命令别名实现mvn sanity(进阶版)

如果想要更简洁的mvn sanity命令,而不是mvn test -Psanity,可以通过Maven的settings.xml配置命令别名:

1. 修改Maven的settings.xml

找到你的Maven配置文件(通常在~/.m2/settings.xml,Windows下为C:\Users\<你的用户名>\.m2\settings.xml),添加以下别名配置:

<settings>
    <!-- 其他配置保持不变 -->
    <aliases>
        <alias>sanity</alias>
        <alias-value>test -Psanity</alias-value>
        
        <alias>integration</alias>
        <alias-value>test -Pintegration</alias-value>
        
        <alias>regression</alias>
        <alias-value>test -Pregression</alias-value>
    </aliases>
</settings>

2. 直接运行自定义命令

现在你就可以用简洁的命令运行对应测试了:

mvn sanity
mvn integration
mvn regression
额外说明:集成测试的特殊处理

如果你的integration目录下是集成测试(而非单元测试),建议使用maven-failsafe-plugin而非maven-surefire-plugin——Failsafe插件专门用于集成测试,支持测试失败后继续执行等特性。配置示例如下:

<profile>
    <id>integration</id>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>3.2.5</version>
                <configuration>
                    <!-- 集成测试类通常命名为*IT.java,可根据你的规则调整 -->
                    <includes>
                        <include>**/spec/integration/**/*IT.java</include>
                    </includes>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</profile>

对应的运行命令为:

mvn verify -Pintegration

或者配置别名后使用mvn integration。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 03:50:50