Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SUREFIRE-2190] Fix module dependencies for compile only dependencies #668

Merged
merged 2 commits into from Aug 23, 2023

Conversation

hgschmie
Copy link
Contributor

Include all modules on the module path to the root modules. This fixes
test code that uses dependencies that are declared as optional
(require static) for the main artifact to access these dependencies.

maven surefire plugin struggles with optional dependencies for the main artifact and test code.

Check out the test repo at https://github.com/hgschmie/msurefire2190

This repo contains three artifacts with identical code:

  • thing1 builds a main artifact without JPMS
  • thing2 builds a main artifact with a strong (requires) dependency on jakarta.annotation.
  • thing3 builds a main artifact with a compile-only (requires static) dependency on jakarta annotations.

The code and its test classes are otherwise identical.

Running mvn -DskipTests clean install builds all three modules and the test code.

Running mvn surefire:test passes the tests in the first two modules but fails in the third.

The surefire plugin, when it executes tests using JPMS adds all
referenced modules to the module path (in this case the module under
test itself and the jakarta.annotations-api jar). It then adds the
main module using --add-modules and patches this module with the
test classes (so that the test classes execute as part of the module).

In case of a requires relationship, the JVM will find the required
JPMS module and add it as accessible. This is why the "thing2" tests
pass.

In case of a requires static relationship, the JVM will not add the
module transitively as it is considered a compilation-only
dependency. For the code under test to be able to access the classes
from jakarta.annotation, they must be declared as a module. However,
the test code only adds the module under test with --add-modules.


The fix for this is pretty simple but must be done in the surefire
plugin. Instead of adding --add-modules <... module id under test..>,
it needs to add --add-modules ALL-MODULE-PATH. Which is
correct, because the code is not looking for compile time only
dependencies but actual runtime dependencies where the code under
execution may also need to access optional runtime dependencies (see
https://nipafx.dev/java-modules-optional-dependencies/ for a slightly
longer explanation).

Following this checklist to help us incorporate your
contribution quickly and easily:

  • Make sure there is a JIRA issue filed
    for the change (usually before you start working on it). Trivial changes like typos do not
    require a JIRA issue. Your pull request should address just this issue, without
    pulling in other changes.
  • Each commit in the pull request should have a meaningful subject line and body.
  • Format the pull request title like [SUREFIRE-XXX] - Fixes bug in ApproximateQuantiles,
    where you replace SUREFIRE-XXX with the appropriate JIRA issue. Best practice
    is to use the JIRA issue title in the pull request title and in the first line of the
    commit message.
  • Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • Run mvn clean install to make sure basic checks pass. A more thorough check will
    be performed on your pull request automatically.
  • You have run the integration tests successfully (mvn -Prun-its clean install).

To make clear that you license your contribution under
the Apache License Version 2.0, January 2004
you have to acknowledge this by using the following check-box.

Include all modules on the module path to the root modules. This fixes
test code that uses dependencies that are declared as optional
(`require static`) for the main artifact to access these dependencies.
@olamy
Copy link
Member

olamy commented Aug 20, 2023

this looks to be a sensible enough change to have an IT test. Can you extract one from your test project?

@hgschmie
Copy link
Contributor Author

yes. let me add this to the pr

@hgschmie hgschmie merged commit 5aea5e2 into apache:master Aug 23, 2023
13 checks passed
@hgschmie hgschmie deleted the surefire-2190 branch August 23, 2023 16:41
@olamy olamy added the bug label Aug 24, 2023
olamy added a commit to jetty/jetty.project that referenced this pull request Nov 9, 2023
due to apache/maven-surefire#668

[ERROR] java.lang.module.FindException: Module jakarta.interceptor not found, required by jakarta.transaction

Signed-off-by: Olivier Lamy <olamy@apache.org>
@olamy
Copy link
Member

olamy commented Nov 9, 2023

this has broken Jetty 12 build

[INFO] -------------------------------------------------------
13:33:32 000][info][gc] Using G1
[ERROR] Error occurred during initialization of boot layer
[ERROR] java.lang.module.FindException: Module jakarta.interceptor not found, required by jakarta.transaction

joakime added a commit to jetty/jetty.project that referenced this pull request Nov 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
2 participants