【问题标题】:Cannot run multiple executions in maven surefire?无法在 Maven surefire 中运行多个执行?
【发布时间】:2015-10-01 10:09:03
【问题描述】:

我想运行名称以 ResourceTest.java 结尾的测试类,所以我定义了以下执行。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <excludes>
            <exclude>**/*.java</exclude>
        </excludes>
    </configuration>
    <version>2.12.2</version>
    <executions>
        <execution>
            <id>resource-tests</id>
            <phase>resource-tests</phase>
            <goals>
                <goal>resource-tests</goal>
            </goals>
            <configuration>
                <includes>**/*ResourceTest.java</includes>
                <!-- <exludes>**/*.java</exludes> -->
            </configuration>
        </execution>
    </executions>
</plugin>

但我不确定如何运行它,我已经搜索了很多,但我错过了一些东西。

我尝试了surefire:test,它跳过了上面配置中定义的所有测试用例。所以,我尝试了surefire:resource-tests,maven 说没有没有定义目标。

我正在使用 eclipse 通过传递这些参数来运行我的 maven 构建。如何通过执行 ID 运行?

当我在 pom 中定义了多个执行时,如何在使用 surefire:test 运行时选择特定执行?

我错过了什么?任何帮助将不胜感激。

【问题讨论】:

    标签: java maven maven-surefire-plugin


    【解决方案1】:

    您当前的配置存在几个问题:

    • 您强制在resource-tests 阶段执行maven-surefire-plugin,但在此阶段does not exist。您应该删除该声明以保留默认的插件绑定阶段,即test
    • 您正在调用目标 resource-testsmaven-surefire-plugin does not define such a goal
    • &lt;includes&gt; 元素定义不明确。它下面应该有一个&lt;include&gt; 标签。
    • 您正在从插件配置中排除所有 Java 文件,因此不会运行任何测试
    • 插件的配置应该在&lt;configuration&gt;元素下完成,而不是针对每个&lt;executions&gt;
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.12.2</version>
        <configuration>
            <includes>
                <include>**/*ResourceTest.java</include>
            </includes>
        </configuration>
    </plugin>
    

    当您有多个执行并且想要“选择”其中一个时,您可以使用配置文件:

    <profiles>
        <profile>
            <id>resource-tests</id>
            <properties>
                <test-classes>**/*ResourceTest.java</test-classes>
            </properties>
        </profile>
        <profile>
            <id>task-tests</id>
            <properties>
                <test-classes>**/*TaskTest.java</test-classes>
            </properties>
        </profile>
    </profiles>
    

    使用以下插件配置:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.12.2</version>
        <configuration>
            <includes>
                <include>${test-classes}</include>
            </includes>
        </configuration>
    </plugin>
    

    有了这样的配置:

    • 当您运行mvn clean test -Presource-tests 时,只会测试匹配**/*ResourceTest.java 的类
    • 当您运行mvn clean test -Ptask-tests 时,只会测试匹配**/*TaskTest.java 的类

    【讨论】:

    • 现在我可以用 surefire:test 运行它了,对吗?当我这样运行时,我看到 **/*ResourceTest.java 属性的 String cannot be cast to util.List 错误。还有,当我指定多个执行时,如何选择特定的执行?
    • @pinkpanther 你可以简单地运行mvn clean test。由于这个插件默认绑定到test阶段,所以会被执行。
    • @pinkpanther 是的,我后来看到了这个并编辑了我的帖子。
    • 谢谢,当我定义了多个执行时,我还能告诉我如何选择特定的执行吗?例如,使用 id=task-tests?
    • 我更新了我的问题,我正在尝试定义多个执行,因此我提出了这个问题。
    猜你喜欢
    • 2011-04-05
    • 2020-11-17
    • 2018-11-12
    • 2021-04-22
    • 2016-04-26
    • 2019-04-18
    • 2021-12-18
    • 1970-01-01
    相关资源
    最近更新 更多