As part of my current project I’ve created a custom class loader. Part of the unit tests for the custom loader involves using some JAR files to demonstrate the proper behavior of the loader.
I’d like to build the test JAR files from Java sources ahead of running the actual unit tests. Further, the test JAR files cannot be on the class path when the unit tests are run, since I want to dynamically load them during the test execution.
Is there a standard pattern for accomplishing this sort of “build some JARs on the side before the test phase but leave them out of the class path” requirement? I can’t believe I’m the first person to try doing this with Maven 2, but I can’t seem to hit on the right POM structure and dependencies. Usually I end up with some of the test jars not being built ahead of the test phase, but I’ve also had problems with inconsistent order-of-build causing the build to work properly on one machine, but fail to build some of the test jars on another.
The simplest thing to do is to set up another project to package the classes for your test jar, then set that as a normal test-scoped dependency.
If you don’t want/aren’t able to do that, you can use the assembly plugin to create a jar in the
process-test-classesphase (i.e. after the tests have been compiled but before the tests are executed). The configuration below will invoke the assembly plugin to create a jar calledclassloader-test-depsin that phase in the target directory. Your tests can then use that jar as needed.The assembly plugin uses an assembly descriptor (in src/main/assembly, called test-assembly.xml) that packages the contents of target/test-classes. I’ve set up a filter to include the contents of com.test package and its children. This assumes you have some package name convention you can apply for the contents of the jar.
The assembly plugin will by default attach the jar as an additional artifact, by specifying
attachas false, it will not be installed/deployed.This is the content of test-assembly.xml