Java 当JUnit失败时,如何停止Maven?
我正在从Maven运行Junit测试。ant脚本Java 当JUnit失败时,如何停止Maven?,java,maven-2,ant,build,junit,Java,Maven 2,Ant,Build,Junit,我正在从Maven运行Junit测试。ant脚本 <junit failureproperty="failproperty" errorproperty="errorproperty"> <classpath refid="classpath" /> <test name="${unit-test-suite}" />
<junit failureproperty="failproperty" errorproperty="errorproperty">
<classpath refid="classpath" />
<test name="${unit-test-suite}" />
<formatter type="brief" usefile="false" />
</junit>
<echo> ----------> ${failproperty} </echo>
<echo> ----------> ${errorproperty} </echo>
<fail message="something wrong" if="${failureproperty}"/>
<fail message="something wrong" if="${errorproperty}"/>
Maven 2目前不支持此“fail fast”功能,请参阅(并可能投票支持)。您确定正确设置了属性吗 请注意,if和除非属性采用属性名,而不是表达式。例如,尝试使用:
而不是
在这个简单的示例中,
标记起作用。尝试mvn包
,然后mvn包-Dfailproperty=true
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ant-test</groupId>
<artifactId>ant-test</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>ant-test</id>
<phase>package</phase>
<configuration>
<tasks>
<junit failureproperty="fail">
<classpath>
<path refid="maven.plugin.classpath"/>
<path refid="maven.test.classpath"/>
</classpath>
<formatter type="plain" />
<batchtest>
<fileset dir="src/test/java"/>
</batchtest>
</junit>
<fail if="fail"/>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant-junit</artifactId>
<version>1.7.1</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>
</project>
如果我像您一样传入参数,那么是的,它可以工作,但是如果我从maven运行,即使遇到JUnit错误,也不会设置fail属性。在你的项目中尝试一下。查看更新的项目
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ant-test</groupId>
<artifactId>ant-test</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>ant-test</id>
<phase>package</phase>
<configuration>
<tasks>
<junit failureproperty="fail">
<classpath>
<path refid="maven.plugin.classpath"/>
<path refid="maven.test.classpath"/>
</classpath>
<formatter type="plain" />
<batchtest>
<fileset dir="src/test/java"/>
</batchtest>
</junit>
<fail if="fail"/>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant-junit</artifactId>
<version>1.7.1</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>
</project>
import junit.framework.Assert;
import org.junit.Test;
public class FakeTestCase {
@Test
public void testThis() {
Assert.fail("Failure");
}
}