[MCOMPILER-478] testCompile with specific module-info.java partially copy target/classes to target/test-classes
Rocher Suchard opened MCOMPILER-478 and commented
Hello,
I am trying to understand how I can fix this particular bug that occurs while compiling test sources with src/test/java/module-info.java (or src/test/jpms/module-info.java in my case because this would make Eclipse fails, due to having two module-info.java).
The module-info that I put in src/test/jpms is more or less the same as {}src/main/jpms{}, but with some test modules (like {}org.opentest4j{}, {}org.junit.jupiter.api{}, and {}org.assertj.core{}). This is pretty much what is explained here at the end of page: https://maven.apache.org/surefire/maven-surefire-plugin/examples/jpms.html#.
When I try to run tests, maven-surefire-plugin fails: the JVM is not happy about a package not found.
1. Created at 2021-12-26T23:43:40.644
Corrupted STDOUT by directly writing to native stream in forked JVM 1. Stream 'Error occurred during initialization of boot layer'.
1. Created at 2021-12-26T23:43:40.647
Corrupted STDOUT by directly writing to native stream in forked JVM 1. Stream 'java.lang.module.FindException: Error reading module:'.
1. Created at 2021-12-26T23:43:40.647
Corrupted STDOUT by directly writing to native stream in forked JVM 1. Stream '...\target\test-classes'.
1. Created at 2021-12-26T23:43:40.648
Corrupted STDOUT by directly writing to native stream in forked JVM 1. Stream 'Caused by: java.lang.module.InvalidModuleDescriptorException:'.
1. Created at 2021-12-26T23:43:40.649
Corrupted STDOUT by directly writing to native stream in forked JVM 1. Stream 'Package org.acme.functions not found in module'.
At first, I thought it was something related to maven-surefire-plugin and I wanted to create a SUREFIRE JIRA issue, but I discovered that something - I presume maven-compiler-plugin - is copying used classes from target/classes to target/test-classes during test compilation.
In the case of my interface, it is not used and I presume the "something" that I speak about do partial copy of used files during compilation.
If we edit the src\main\java\org\acme\impl\AImpl.java to use the interface (should be commented in the attached ZIP):
public class AImpl implements A {
@Override public void doSomethingCool() {
org.acme.functions.BooleanConsumer bc = v -> {};
throw new UnsupportedOperationException("no doing something cool");
}
}
The class file is copied and surefire does not fail.
The attached ZIP file exhibit the problem :
If we simply compile with tests: {}mvn install{}: maven-surefire-plugin will fails to execute test because of missing package. I could probably remove the offending package from test/module-info but I don't think I should tinker to much : the test/module-info should really "extends" the main/module-info and only add packages/modules.
If we skip test : {}mvn install -DskipTests{}: maven-jar-plugin will fails to execute to process due to a jar error {}(jar: Package org.acme.functions missing from ModulePackages class file attribute{}){}.{}
A solution to both issues would be to create dedicated tests project, with its own module-info.java and another module name (eg: org.acme.tests) but that would no longer be a (modular) white box testing.
Note : the attached project requires Maven 3.8.4 (there is a maven wrapper inside) and Java 17. I don't know if this fail with Java 11.
Affects: 3.8.1
Attachments:
- surefire-empty-packages.zip (15.46 kB)