2

现在我知道如果我使用

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <excludes>
            <exclude>**/Benchmark*.java</exclude>
        </excludes>
    </configuration>
</plugin>

我将能够跳过我不想运行的测试。但是,我想要一个特定的配置文件,如果我使用该特定配置文件构建,上述排除的测试将运行。我试过了

<profiles>
    <profile>
        <id>benchmark</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <configuration>
                        <includes>
                            <include>**/Test*.java</include>
                            <include>**/*Test.java</include>
                            <include>**/*TestCase.java</include>
                        </includes>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

与上述一起,但似乎它不起作用。

4

2 回答 2

1

您可以使用属性定义测试源目录,并使用自定义配置文件覆盖它,如下所示:

<properties>
    <test.dir>src/test/java</test.dir>
</properties>

<profiles>
    <profile>
        <id>benchmark</id>
        <properties>
            <test.dir>src/benchmark-tests/java</test.dir>
        </properties>
    </profile>
</profiles>

<build>
    <testSourceDirectory>${test.dir}</testSourceDirectory>
</build>

这样,执行mvn test将在src/test/java中运行所有测试,并mvn test -Pbenchmarksrc/benchmark-tests/java中运行测试。

控制台输出:

mvn clean test
-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running br.com.instaweb.sample.SampleUnitTest
sample unit test was run
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.034 sec

和:

mvn clean test -Pbenchmark
-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running br.com.stackoverflow.BenchmarkTest
benchmark test was run
于 2013-06-14T04:15:05.690 回答
1

Maven 将您的配置合并在一起,而不是像您期望的那样覆盖,因此您的配置文件处于活动状态的最终配置如下所示:

<configuration>
    <includes>
        <include>**/Test*.java</include>
        <include>**/*Test.java</include>
        <include>**/*TestCase.java</include>
    </includes>
    <excludes>
        <exclude>**/Benchmark*.java</exclude>
    </excludes>
</configuration>

不需要所有这些 default <include>反正他们已经在那里了。只需<excludes/>告诉 Maven 您想停止执行您在基本 POM 中指定的排除项。即,在您的个人资料中,只需说:

<configuration>
    <excludes/>
</configuration>
于 2013-06-14T04:20:24.973 回答