简体   繁体   English

如果junit测试失败,如何停止Maven构建?

[英]How to stop a maven build, if a junit test fails?

I have set up a maven project, which is create a JAR File as artefact. 我已经建立了一个maven项目,该项目将artefact创建一个JAR文件。 Now I have created some JUnit tests and would like to stop the maven build, if one of these junit test fails. 现在,我创建了一些JUnit测试,并且如果其中一个junit测试失败,则想停止Maven构建。 What steps are necessary, to do this? 为此需要采取哪些步骤? Now I get a JAR File, although one or more JUnit test fails. 现在,我得到了一个JAR文件,尽管一个或多个JUnit测试失败。

I have created my JUnit test in the folder "src/main/resources" und here is the build snippet of my pom.xml: 我在文件夹“ src / main / resources”中创建了JUnit测试,这里是pom.xml的构建片段:

    <plugins>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.0.2</version>
            <configuration>
                <optimize>true</optimize>
                <fork>true</fork>
                <source>${source.jdk}</source>
                <target>${target.jdk}</target>                  
            </configuration>
        </plugin>   

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>
                        jar-with-dependencies
                    </descriptorRef>
                </descriptorRefs>
                <archive>
                    <addMavenDescriptor>false</addMavenDescriptor>                      
                    <manifestEntries>
                        <Source-JDK>${source.jdk}</Source-JDK>
                        <Target-JDK>${target.jdk}</Target-JDK>                                      
                        <Project-Build-SourceEncoding>${project.build.sourceEncoding}</Project-Build-SourceEncoding>    
                        <Maven-Build-Timestamp>${maven.build.timestamp}</Maven-Build-Timestamp>
                    </manifestEntries>
                    <manifest>  
                        <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                        <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>                       
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <mainClass>${project.groupId}.${project.artifactId}.myMainFile</mainClass>
                    </manifest>
                </archive>
                <appendAssemblyId>false</appendAssemblyId>
                <classifier>jar-with-dependencies</classifier>
            </configuration>
            <executions>
                <execution>
                    <id>assembly-jar-Id</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

When you create a maven project and if you use mvn clean install to build your source it will automatically fail your build saying test failures there is no need any other things. 当您创建一个maven项目时,如果使用mvn clean install来构建源代码,它将自动使您的构建失败,并说测试失败,不需要其他任何事情。

Follow this to learn more. 请按照了解更多信息。

I have created my JUnit test in the folder "src/main/resources" 我已经在“ src / main / resources”文件夹中创建了我的JUnit测试。

That's the wrong location. 那是错误的位置。 Put your unit test into "src/test/java" and Maven will take care of the rest. 将您的单元测试放入“ src / test / java”中,其余的将由Maven负责。

I get that you are looking to 'fail-fast' but As far as my understanding the answer is NO. 我知道您正在寻求“快速失败”,但据我了解,答案是否定的。 On googling, I found a bug-issue raised for SureFire (the maven plugin that runs the junit test cases) 在谷歌搜索时,我发现了一个针对SureFire(运行junit测试用例的maven插件)引发的错误问题。

Allow "fail fast" or stop running on first failure http://jira.codehaus.org/browse/SUREFIRE-580 允许“快速失败”或在第一次失败时停止运行http://jira.codehaus.org/browse/SUREFIRE-580

which still is in Open state. 仍处于打开状态。

You can add this to your command line : mvn install -Dmaven.test.failure.ignore=true or use the below plugin in your pom.xml : 您可以将其添加到命令行中: mvn install -Dmaven.test.failure.ignore=true或在pom.xml中使用以下插件:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>${surefire.plugin.version}</version>
    <configuration>
        <!-- Build, even if tests fail -->
        <testFailureIgnore>true</testFailureIgnore>
    </configuration>
</plugin>

So even if any of your test fails it will ignore and continue your build process without failures. 因此,即使您的任何测试失败,它也将忽略并继续您的构建过程而不会失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM