Different Maven and Gradle execution of Java 9+ Modular Junit 5 tests

reading at https://heise.de/-4093423 about eclipse photon, I stumpled upon this chapter:


Mit dem enthaltenen JUnit 5.1 lässt sich nun auch Java-Code testen, der sich – wie bei dem in der Java-Welt weitverbreiteten Build-Tool Maven üblich – zusammen mit dem Testcode im selben Projekt befindet, auch wenn dieses ein modulares Java-Projekt ist. Haupt- und Testcode lassen sich im selben Projekt nun so trennen, dass im Hauptcode nicht versehentlich Bibliotheken verwendet werden, die eigentlich nur zur Nutzung beim Testen gedacht waren. Mit Maven funktioniert das bereits automatisch, bei Gradle oder ohne Build-Tool sind dazu noch manuelle Schritte notwendig.

en_US (translated by online-translator.com / Prompt):

Now with the contained JUnit 5.1 Java code can be also tested which is – like with the Build tool widespread in the Java world Maven commonly – together with the test code in the same project, even if this is a modular Java project. Main code and test code can be separated in the same project now in such a way that in the main code mistakenly libraries are not used who were intended, actually, only to the use with the testing. With Maven this already automatically functions, with Gradle or without Build tool still manual steps are necessary in addition.

I admit, that I’m not yet familiar with modular java components and just started to use Gradle 4.6+ for Junit 5+, but perhaps s.o. can explain, which case (example code are very appreciated) is covered by maven but not (yet) possible to be build by gradle?!

I would be pleased to see that this “missing link” is captured as a proper feature request on gradle if there is a gap “we” can’t deal with as claimed on this article cited above.


While Maven just works, with Gradle you have to set various module command line arguments

Need to add this boilerplate code for compileTestJava and test tasks:

compileTestJava {
    inputs.property("moduleName", moduleName)
    doFirst {
        options.compilerArgs = [
            '--module-path', classpath.asPath,
            '--add-modules', 'org.junit.jupiter.api',
            '--add-reads', "$moduleName=org.junit.jupiter.api",
           '--patch-module', "$moduleName=" + files(sourceSets.test.java.srcDirs).asPath,
        classpath = files()

test {

    doFirst {
        jvmArgs = [
            '--module-path', classpath.asPath