Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven: Excluding tests from build

I have some classes I'm using as tests in my src/test/java folder of my project. When I run maven using the standard maven compile plugin. Those items are compiled into .class files and are included in the jar where the compiled code is packaged.

I've created these tests for myself to run within eclipse, prior to running maven and building my release. They are just sanity tests and should not be included in the build. I'd rather not put them in a seperate project, because, to me, they make sense here. How can I tell maven that I do not want it to compile/include the files in that directory?

I beleive the maven compiler plugin is generating the jar as follows:

<plugin>
 <artifactId>maven-compiler-plugin</artifactId>
 <configuration>
  <source>1.6</source>
  <target>1.6</target>
 </configuration>
</plugin>
like image 220
Brian Avatar asked Feb 05 '13 21:02

Brian


People also ask

How do you skip a unit test?

In Maven, you can define a system property -Dmaven. test. skip=true to skip the entire unit test. By default, when building project, Maven will run the entire unit tests automatically.

How do I skip test cases in Jenkins?

We can simply skip or ignore them. This behavior is specified in Jenkins. For a Maven project we simply set a parameter in the JVM options of our Maven configuration. This will disable or skip all of the tests in this project.

Does Maven build run tests?

Usually, we execute tests during a Maven build using the Maven surefire plugin. This tutorial will explore how to use this plugin to run a single test class or test method.


2 Answers

I understand from your comment on this answer that the "tests" aren't unit tests, but just ordinary classes that you want excluded from the final artifact? As such, your best option is to make use of the <exclude> tag with the maven-jar-plugin as follows:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.4</version>
    <configuration>
        <excludes>
            <exclude>**/yoursortoftestpackage/YourSortOfTestClass*</exclude>
        </excludes>
    </configuration>
</plugin>

Hope that helps!

Cheers,

like image 73
Anders R. Bystrup Avatar answered Sep 19 '22 11:09

Anders R. Bystrup


Use the option of -Dmaven.test.skip will skip both compilation and execution of the tests. ( use -DskipTests just skips the test execution, the tests are still compiled)

The reference link

mvn clean install -Dmaven.test.skip

like image 27
Y.G. Avatar answered Sep 20 '22 11:09

Y.G.