apache / apache/maven-surefire

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

Open
#3,345 4 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
461
Forks
588
Avg merge
1d 8h
Merged PRs (30d)
19

Description

## 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//` instead of `target/classes/`
- `target/test-classes//` 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 detection** — `findModuleDescriptor()` looks for `module-info.class` directly in `target/classes/` but misses the nested `target/classes//module-info.class`
2. **Test class scanning** — `DirectoryScanner` scans `target/test-classes/` and finds `/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)

```xml

org.apache.maven.plugins
maven-surefire-plugin
3.5.5

${project.build.outputDirectory}/modfib
${project.build.testOutputDirectory}/modfib
true
--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

```

**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

- Repository: https://github.com/aschemaven/java9-jigsaw-examples/tree/feature/surefire-whitebox/jigsaw-examples/example_test/m4
- First commit: workaround with surefire 3.5.5
- Second commit: zero-config with a locally patched surefire 3.6.0-SNAPSHOT (proof-of-concept, all 838 surefire-common tests passing)

## Related issues

- #3090 (SUREFIRE-1755) — Put dependencies from module-info in test sources on module path
- #3088 (SUREFIRE-1765) — target/test-classes should not be added to classpath when tests run on modulepath using patch-module
- #3040 (SUREFIRE-1811) — Add resources to test module

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the working m4 example with Maven 4 and compare the workaround against the locally patched Surefire 3.6.0-SNAPSHOT behavior. Read findModuleDescriptor(), DirectoryScanner, and the handling of module-info-patch.args; verify that nested module output, test scanning, and compiler-generated exports work without explicit configuration, then run the surefire-common tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
build-system, testing
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.