Skip to content

Support Maven 4 Module Source Hierarchy and module-info-patch.args for modular whitebox testing #3345

@ascheman

Description

@ascheman

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:

  1. Module detectionfindModuleDescriptor() looks for module-info.class directly in target/classes/ but misses the nested target/classes/<module-name>/module-info.class
  2. Test class scanningDirectoryScanner scans target/test-classes/ and finds <module-name>/pkg/Test.class, interpreting the module name as a package prefix
  3. 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions