[英]Exclude Tests from Maven build
When I am running maven clean install
it always executes all tests in src/test/java
is there a way to skip all the tests, just to run simple build without any tests.当我运行maven clean install
它总是执行src/test/java
中的所有测试,有没有办法跳过所有测试,只运行简单的构建而不进行任何测试。
I want my tests to be in src/test/java
but I want to tell maven to do not execute them.我希望我的测试在src/test/java
但我想告诉 maven 不要执行它们。 I've been looking for something like that on the internet but I did not manage to find the answer.我一直在互联网上寻找类似的东西,但我没有找到答案。
Is there a way to do this?有没有办法做到这一点?
You can also choose to use 您也可以选择使用
mvn install -Dmaven.test.skip
From Maven website: 来自Maven网站:
If you absolutely must, you can also use the maven.test.skip property to skip compiling the tests. 如果绝对必须,您还可以使用maven.test.skip属性跳过编译测试。 maven.test.skip is honored by Surefire, Failsafe and the Compiler Plugin. maven.test.skip受到Surefire,Failsafe和Compiler Plugin的尊重。
As is says you will not even compile the test sources. 按照说你甚至不会编译测试源。
From http://maven.apache.org/plugins/maven-surefire-plugin/examples/skipping-test.html : 来自http://maven.apache.org/plugins/maven-surefire-plugin/examples/skipping-test.html :
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.2</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>
[...]
</project>
My favorite way to manage when my tests run is to create a Maven variable called skip-tests
and default it to true
. 我运行测试时最喜欢的管理方法是创建一个名为skip-tests
的Maven变量,并将其默认为true
。 Then you can use that variable like so: 然后您可以像这样使用该变量:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.2</version>
<configuration>
<skipTests>${skip-tests}</skipTests>
</configuration>
</plugin>
This way, you can just pass in the variable at build time, -Dskip-tests=false
, when you don't want them to run. 这样,您可以在构建时传递变量, -Dskip-tests=false
,当您不希望它们运行时。 This is most useful when you have integration and unit tests, and would like to run or disable both sets of tests with just one variable. 当您进行集成和单元测试时,这非常有用,并且只想使用一个变量来运行或禁用两组测试。
use this 用这个
mvn clean install -DskipTests mvn clean install -DskipTests
We use Surefire for Unit tests, and Failsafe for Integration tests. 我们使用Surefire进行单元测试,并使用Failsafe进行集成测试。
To skip all tests: 要跳过所有测试:
mvn clean package -DskipTests
To skip just Failsafe tests: 要跳过Failsafe测试:
mvn clean package -DskipIT
To skip just Surefire, you need to explicitly call the integration-test goal of the Failsafe plugin, after compiling the test classes of course: 要仅跳过Surefire,您需要在编译测试类之后显式调用Failsafe插件的集成测试目标:
mvn clean test-compile failsafe:integration-test
All the answers here are correct.这里所有的答案都是正确的。 To be precise,准确地说,
-DskipTests - compiles the test classes, but skips running them, -DskipTests - 编译测试类,但跳过运行它们,
-Dmaven.test.skip=true - skips compiling the test files and also does not run them -Dmaven.test.skip=true - 跳过编译测试文件,也不运行它们
I think simplest would be this : mvn clean package -Dmaven.skip.tests=true
我认为最简单的是: mvn clean package -Dmaven.skip.tests=true
I think this the approach to use as it does not make you change your pom, so does not have to make changes to the project. 我认为这种方法可以使用,因为它不会让你改变你的pom,因此不必对项目进行更改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.