mvn clean
mvn package
mvn clean package
mvn test
<project> [...] <build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.20</version> </plugin> </plugins> </pluginManagement> </build> [...] </project>
<dependencies> [...] <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.9.8</version> <scope>test</scope> </dependency> [...] </dependencies>
<!DOCTYPE suite SYSTEM "http://beust.com/testng/testng-1.0.dtd" > <suite name="testSuite"> <test name="test"> <classes> <class name=「ATest" /> </classes> </test> </suite>
import org.testng.Assert; import org.testng.annotations.Test; public class ATest { @Test public void testIsTrue(){ A a=new A(); Assert.assertTrue(a.returnTrue()); } }
<project> [...] <build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.20</version> <configuration> <suiteXmlFiles> <suiteXmlFile>testng.xml</suiteXmlFile> </suiteXmlFiles> </configuration> </plugin> </plugins> </pluginManagement> </build> […] </project>
mvn package -DskipTests
<project> [...] <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.20</version> <configuration> <skipTests>true</skipTests> </configuration> </plugin> </plugins> </build> [...] </project>
mvn package -Dmaven.test.skip=true
<project> [...] <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.5.1</version> <configuration> <skip>true</skip> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.20</version> <configuration> <skipTests>true</skipTests> </configuration> </plugin> </plugins> </build> [...] </project>
mvn -Dtest=ATest test
」maven-surefire-plugin「插件還支持使用星號匹配測試類名的方式以指定運行特定的測試類,星號表示匹配零個或多個字符。例如只執行」A「開頭的測試類:
mvn -Dtest=A* test
除了使用星號匹配,還能夠使用逗號指定多個測試類:
mvn test -Dtest=ATest,BTest
同時匹配類名以A開頭的測試類和類名爲BTest的測試類:
mvn -Dtest=A*,BTest test
當test命令經過參數匹配不到任何測試類時,項目將會構建失敗。配置參數DfailIfNoTests=false能夠在匹配不到測試類時依舊構建成功:
mvn -Dtest=AATest -DfailIfNoTests=false test
<project> [...] <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.20</version> <configuration> <includes> <include>**/*Tests.java</include> </includes> </configuration> </plugin> </plugins> </build> [...] </project>
<project> [...] <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.20</version> <configuration> <excludes> <exclude>**/CTest.java</exclude> <exclude>**/DTest.java</exclude> </excludes> </configuration> </plugin> </plugins> </build> [...] </project>
參考資料:java
Maven文檔:http://maven.apache.org/guides/apache
maven-surefire-plugin 文檔:http://maven.apache.org/surefire/maven-surefire-plugin/框架
=====================================================================maven