更新时间:2022-06-12 17:17:57
听起来你没有理解 在 Maven 中构建生命周期.如果您运行 mvn install
所有生命周期阶段(包括 install
阶段本身)在安装阶段之前运行.这意味着运行以下阶段:
It sounds like you didn't understand the concept of the build life-cycle in Maven. If you run mvn install
all life-cycle phases (including the install
phase itself) run before the install phase. This means running the following phases:
换句话说,包括 test
和 integration-test
生命周期阶段.因此,如果没有任何补充信息,就不可能随心所欲地更改行为.
which means in other words the test
as well as integration-test
life-cycle phases are included. So without any supplemental information it's not possible to change the behaviour as you wish it.
可以通过在 Maven 中使用配置文件来实现:
It could be achieved by using a profile in Maven:
<project>
[...]
<profiles>
<profile>
<id>no-unit-tests</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
[...]
</project>
所以你的第一个要求:
mvn install
,我希望编译所有测试,但我不想执行任何测试.mvn install
, I want all tests to compile, but I do not want to execute any.可以通过以下方式实现:
can be achieved by using the following:
mvn -Pno-unit-test test
mvn test
,我希望所有测试都能编译,但只执行单元测试.mvn test
, I want all tests to compile, but execute only unit tests.这可以通过简单的调用来实现:
This can simply achieved by using the plain call:
mvn test
因为未运行集成测试阶段(请参阅构建生命周期).
cause the integration tests phase is not run (see the build life cycle).
mvn integration-test
,我想编译并执行所有测试.mvn integration-test
, I want to compile and execute all tests.这意味着运行默认值,包括运行 test
阶段,该阶段将运行单元测试 (maven-surefire-plugin) 并进一步运行由 maven-failsafe-plugin 处理的集成测试.但是你应该知道,如果你想调用集成测试,你应该使用以下命令:
This means running the default which includes running the test
phase which will run the unit tests (maven-surefire-plugin) and furthermore running the integration test which are handled by the maven-failsafe-plugin. But you should be aware that if you like to call the integration tests you should using the following command:
mvn verify
相反,因为您错过了之前调用中的 post-integration-test
阶段.
instead, cause you missed the post-integration-test
phase in your previous call.
除了上述之外,您应该遵循单元和集成测试的命名约定,其中 单元测试 应该如下命名:
Apart from the above you should follow the naming conventions for unit and integration tests where unit tests should be named like the following:
<includes>
<include>**/*Test*.java</include>
<include>**/*Test.java</include>
<include>**/*TestCase.java</include>
</includes>
和集成测试命名如下:
<includes>
<include>**/IT*.java</include>
<include>**/*IT.java</include>
<include>**/*ITCase.java</include>
</includes>
我希望你已经像下面这样配置了 maven-failsafe-plugin,这是将 maven-failsafe-plugin 绑定到正确的生命周期阶段所需的:
I hope you have configured the maven-failsafe-plugin like the following which is needed to bound the maven-failsafe-plugin to the correct life-cycle-phases:
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.15</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
[...]
</project>
正如您所做的那样,但您应该知道 include
标记适用于源代码 (.java) 而不是编译名称 (.class).我不会使用 Category 注释,只是简单地使用命名约定使 pom 更简单和更短.
as you correctly did, but you should be aware that the include
tags work on the source code (.java) and not on the compiled names (.class). I wouldn't use the Category annotation, just simply using the naming conventions makes the pom simpler and shorter.