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

如何通过Maven命令行同时运行Java与Groovy测试?

Maven命令行无法执行Groovy/Spock测试的解决方法

问题描述

克隆Spring Boot项目后,执行mvn test时仅运行了无实际逻辑的Java测试类AppTests.java。Groovy测试文件已被gmavenplus-plugin成功编译,但maven-surefire-plugin未执行这些测试。IntelliJ中可单独运行Groovy测试,需要实现命令行运行所有测试。

解决步骤

1. 配置maven-surefire-plugin扫描所有测试类

默认情况下,maven-surefire-plugin可能仅扫描Java测试源编译后的class文件,需显式配置规则让它识别Groovy编译后的测试类:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.22.2</version>
    <configuration>
        <argLine>-Dplatform.environment=ci</argLine>
        <!-- 适配Java和Groovy/Spock的测试类命名风格 -->
        <includes>
            <include>**/*Test.class</include>
            <include>**/*Spec.class</include> <!-- 若使用Spock的Spec命名风格则添加 -->
        </includes>
    </configuration>
</plugin>

2. 明确GMavenPlus插件的测试源目录

显式指定Groovy测试目录,避免Maven忽略该目录:

<plugin>
    <groupId>org.codehaus.gmavenplus</groupId>
    <artifactId>gmavenplus-plugin</artifactId>
    <version>3.0.0</version>
    <executions>
        <execution>
            <goals>
                <goal>addTestSources</goal>
                <goal>compileTests</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <testSources>
            <testSource>
                <directory>${project.basedir}/src/test/groovy</directory>
                <includes>
                    <include>**/*.groovy</include>
                </includes>
            </testSource>
        </testSources>
    </configuration>
</plugin>

3. 执行测试命令

修改配置后,执行以下命令清理并重新运行所有测试:

mvn clean test

问题相关信息

关键执行日志

[INFO] --- gmavenplus-plugin:3.0.0:addTestSources (default) @ project ---

[INFO] --- swagger-codegen-maven-plugin:3.0.35:generate (generate-orchestrator) @ project ---
...
[INFO] --- maven-resources-plugin:3.2.0:resources (default-resources) @ project ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] Copying 1 resource
[INFO] Copying 1 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.10.1:compile (default-compile) @ project ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 32 source files to ...project/target/classes
[INFO] --- maven-resources-plugin:3.2.0:testResources (default-testResources) @ project ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] skip non existing resourceDirectory ...project/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.10.1:testCompile (default-testCompile) @ project ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to ...project/target/test-classes
[INFO] 
[INFO] --- gmavenplus-plugin:3.0.0:compileTests (default) @ project ---
[INFO] Using isolated classloader, without GMavenPlus classpath.
[INFO] Using Groovy 3.0.13 to perform compileTests.
[INFO] Parallel parsing disabled.
[INFO] Compiled 8 files.
[INFO] 
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ project ---
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running TestSuite
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.258 s - in TestSuite
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

测试目录结构

src/test/
├── groovy
│   └── com
│           └── project
│                 ├── ASampleTest.groovy
│                 ├── smoke
│                 │   ├── BlaTest.groovy
│                 │   └── utils
│                 │       └── Provider.groovy
│                 └── unit
│                     ├── BlaTest2.groovy
│                     └── BlaTest.groovy
├── java
│   └── com
│             └── project
│                 └── AppTests.java

Java测试类

@SpringBootTest
class AppTests {

    @Test
    void contextLoads() {
    }

}

原始pom.xml配置

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com</groupId>
    <artifactId>project</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>project</name>

    <properties>
        <java.version>17</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>
        <dependency>
            <groupId>io.swagger.core.v3</groupId>
            <artifactId>swagger-annotations</artifactId>
            <version>2.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.spockframework</groupId>
            <artifactId>spock-core</artifactId>
            <version>2.2-M3-groovy-3.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.spockframework</groupId>
            <artifactId>spock-spring</artifactId>
            <version>2.2-M3-groovy-3.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy-all</artifactId>
            <version>3.0.13</version>
            <type>pom</type>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>io.swagger.codegen.v3</groupId>
                <artifactId>swagger-codegen-maven-plugin</artifactId>
                <version>3.0.35</version>
                <executions>
                    <execution>
                        <id>generate-orchestrator</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <inputSpec>${project.basedir}/src/main/resources/openapi/geo_search_v4.yaml</inputSpec>
                            <language>java</language>
                            <modelPackage>com.sabre.zjwpit.geo.api</modelPackage>
                            <generateModelTests>false</generateModelTests>
                            <generateApis>false</generateApis>
                            <generateApiTests>false</generateApiTests>
                            <library>resttemplate</library>
                            <configOptions>
                                <dateLibrary>java8-localdatetime</dateLibrary>
                                <hideGenerationTimestamp>true</hideGenerationTimestamp>
                            </configOptions>
                        </configuration>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.yaml</groupId>
                        <artifactId>snakeyaml</artifactId>
                        <version>1.30</version>
                    </dependency>
                </dependencies>
            </plugin>
            <plugin>
                <groupId>org.codehaus.gmavenplus</groupId>
                <artifactId>gmavenplus-plugin</artifactId>
                <version>3.0.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>addTestSources</goal>
                            <goal>compileTests</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <argLine>-Dplatform.environment=ci</argLine>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 20:59:51