1

I have set up a maven project, which is create a JAR File as artefact. Now I have created some JUnit tests and would like to stop the maven build, if one of these junit test fails. What steps are necessary, to do this? Now I get a JAR File, although one or more JUnit test fails.

I have created my JUnit test in the folder "src/main/resources" und here is the build snippet of my 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>

4 Answers4

3

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.

Follow this to learn more.

Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
  • As an additional info - if that does not happen to you most probably maven does not see and run your test classes. See http://stackoverflow.com/questions/6178583/maven-does-not-find-junit-tests-to-run – Deltharis Aug 11 '14 at 10:02
0

I have created my JUnit test in the folder "src/main/resources"

That's the wrong location. Put your unit test into "src/test/java" and Maven will take care of the rest.

Ray
  • 3,084
  • 2
  • 19
  • 27
  • sorry, that was my mistake. I put the junit test in src/test/java and not in src/main/resources –  Aug 11 '14 at 10:35
-3

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)

Allow "fail fast" or stop running on first failure http://jira.codehaus.org/browse/SUREFIRE-580

which still is in Open state.

mtk
  • 13,221
  • 16
  • 72
  • 112
-3

You can add this to your command line : mvn install -Dmaven.test.failure.ignore=true or use the below plugin in your 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.

Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
arjun99
  • 358
  • 1
  • 8