3

How can we create a Maven test jar with Java 11 modules?

The only way I found is to add a module-info.java file to test/java and change the module name (e.g. append ".test"). Then provide the class in a separate package (e.g. append ".test") and export that package:

module my.module.test {
    requires my.module;

    exports my.module.test;
}

Otherwise the classes are not visible or I get split package issues.

But this isn't really the purpose of the test-jar goal and it limits access to "my.module".

What is the proper way to use test-jar with Java 11 modules? Or should it be avoided?

Puce
  • 37,247
  • 13
  • 80
  • 152
  • Hmm, maybe th "easy way" doesn't really work with Java 11 anymore: https://maven.apache.org/plugins/maven-jar-plugin/examples/create-test-jar.html# – Puce Oct 07 '19 at 09:48
  • A test-jar goal is intended to populate test(classes) to other modules which usually shouldn't be done that way ...furthermore the question is: Do you want to test the module boundaries or would you like to unit test your code inside the module? If the second one you don't need to use the module-info in src/test/java ...otherwise it's the only way ...(I would see that as an integration tests which should be located in a different module)... – khmarbaise Oct 07 '19 at 12:22
  • the test-jar contains test utilities for classes in the primary artifact, e.g. utility methods to create test data based on model classes defined in the primary artifact or Hamcrest BaseMatcher for custom exception classes defined in the primary artifact. But there are sometimes also simple "inside" module tests (not used externally). – Puce Oct 07 '19 at 15:45
  • If so then you should make a separate module and make the test utilities classes into src/main/java and make your tests for them into src/test/java and use that dependency via test scope (this can be a module on it's own). This is much more clearer and easier than using `test-jar` configuration+classifier (where used) ...If your tests only internals no need for module-info otherwise make it and integration tests in a complete separate standalone module. Separation of concern. – khmarbaise Oct 07 '19 at 17:49

0 Answers0