How to pack test classes in a jar without running them?

I struggle to include my test classes in the jar package, but I don't run them. After some googling, I tried mvn package -DskipTests , but my test classes are simply not added to the jar.

Any ideas?

+4
source share
2 answers

if you follow the maven rules then your test classes are under src/test/java . maven never packs the contents of a subdirectory of test sources into an artifact.

you have (at least ...) 3 alternativs:

put tests with "normal" sources

if you REALLY want them to be packaged (why?), then you should put the test classes in src/main/java , where they will be treated as a regular source and their compiled classes packaged in an artifact (usually * .jar)

you face all possible problems. for example, your artifact will have a compilation dependent junit dependency, which may interfere with other modules using your jar.

you may also need to configure maven plugins to run tests to keep abreast of your test classes if you do (that is, if you ever want to run tests as part of your build). for example, reliable and fail-safe plugins.

configure maven jar plugin to build test container

see maven jar plugin documentation. they even have a section called "How to create a jar containing test classes" - the instructions there will result in your tests being packaged in a separate jar file (which is probably much better).

create a jar your way using the build plugin directly

yuo could disable the default execution of the jar plugin and instead add the execution of the plugin assembly to the packaging phase to create a jar. you will need to write assembly assembler for this (not as complicated as the link above does). this will give you complete control over what is included in the can. it’s best to start by copying one of the predefined assemblies

+5
source

If these are tests for the current project, then it should be in src/test/java , and it will not be included in the main bank. This is the most common use case. If you are writing a test library that will be used in other projects, put them in src/main/java and add it as a dependency with the test scope in these projects.

If you want to distribute everything in a "stand-alone package", you can either share this code through the version control repository, or create the original jar with the maven build plugin.

+2
source

Source: https://habr.com/ru/post/1479145/


All Articles