Summary
When using Maven 4's Module Source Hierarchy (modular source declarations in the POM) and module-info-patch.maven for whitebox testing of Java modules, surefire requires manual workarounds (classesDirectory, testClassesDirectory, useModulePath, argLine) that should not be necessary.
Problem
Maven 4 with maven-compiler-plugin 4.0.0-beta-4 compiles modular sources to a nested layout:
target/classes/<module-name>/ instead of target/classes/
target/test-classes/<module-name>/ instead of target/test-classes/
Additionally, maven-compiler-plugin generates target/test-classes/META-INF/maven/module-info-patch.args from module-info-patch.maven, containing --add-exports and --add-opens directives for test execution.
Surefire 3.5.5 / 3.6.0-SNAPSHOT does not handle either:
- Module detection —
findModuleDescriptor() looks for module-info.class directly in target/classes/ but misses the nested target/classes/<module-name>/module-info.class
- Test class scanning —
DirectoryScanner scans target/test-classes/ and finds <module-name>/pkg/Test.class, interpreting the module name as a package prefix
- module-info-patch.args — surefire auto-generates
--add-opens and --add-reads but does not read the compiler-generated args file containing --add-exports directives from module-info-patch.maven
Current workaround (surefire 3.5.5)
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.5.5</version>
<configuration>
<classesDirectory>${project.build.outputDirectory}/modfib</classesDirectory>
<testClassesDirectory>${project.build.testOutputDirectory}/modfib</testClassesDirectory>
<useModulePath>true</useModulePath>
<argLine>--add-reads modfib=ALL-UNNAMED --add-exports modfib/pkgfib.internal=ALL-UNNAMED
--add-opens modfib/pkgfib=ALL-UNNAMED --add-opens modfib/pkgfib.internal=ALL-UNNAMED</argLine>
</configuration>
</plugin>
Note: This workaround hardcodes a single module name in classesDirectory, testClassesDirectory, and argLine. It does not scale to projects with multiple Java modules — each would need its own configuration, which is not possible with a single surefire execution.
Expected behavior
With Maven 4 Module Source Hierarchy and a module-info-patch.maven file, mvn test should work without any explicit surefire configuration beyond specifying the plugin version.
Working example
Related issues
Summary
When using Maven 4's Module Source Hierarchy (modular source declarations in the POM) and
module-info-patch.mavenfor whitebox testing of Java modules, surefire requires manual workarounds (classesDirectory,testClassesDirectory,useModulePath,argLine) that should not be necessary.Problem
Maven 4 with maven-compiler-plugin 4.0.0-beta-4 compiles modular sources to a nested layout:
target/classes/<module-name>/instead oftarget/classes/target/test-classes/<module-name>/instead oftarget/test-classes/Additionally, maven-compiler-plugin generates
target/test-classes/META-INF/maven/module-info-patch.argsfrommodule-info-patch.maven, containing--add-exportsand--add-opensdirectives for test execution.Surefire 3.5.5 / 3.6.0-SNAPSHOT does not handle either:
findModuleDescriptor()looks formodule-info.classdirectly intarget/classes/but misses the nestedtarget/classes/<module-name>/module-info.classDirectoryScannerscanstarget/test-classes/and finds<module-name>/pkg/Test.class, interpreting the module name as a package prefix--add-opensand--add-readsbut does not read the compiler-generated args file containing--add-exportsdirectives frommodule-info-patch.mavenCurrent workaround (surefire 3.5.5)
Note: This workaround hardcodes a single module name in
classesDirectory,testClassesDirectory, andargLine. It does not scale to projects with multiple Java modules — each would need its own configuration, which is not possible with a single surefire execution.Expected behavior
With Maven 4 Module Source Hierarchy and a
module-info-patch.mavenfile,mvn testshould work without any explicit surefire configuration beyond specifying the plugin version.Working example
Related issues