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

Groovy class loaders are not independent between modules #183

Closed
TobiX opened this issue Dec 7, 2020 · 4 comments
Closed

Groovy class loaders are not independent between modules #183

TobiX opened this issue Dec 7, 2020 · 4 comments
Assignees
Labels
Milestone

Comments

@TobiX
Copy link
Contributor

TobiX commented Dec 7, 2020

I'm currently not sure if this is a bug or just a "This is how Java works, deal with it" problem, so feel free to close this if this "works as intended".

When running scripts in multiple independent maven modules under the same parent, the class loaders are not independent. I have attached a ZIP with a minimal reproducer (gmavenplus-test.zip):

parent
├── a
└── b
  • a has a dependency on log4j
  • b has a dependency on junit

All modules run a script which simply does Class.forName() for both, a class from log4j and a class from junit. The result is what you expect for whatever module runs first, but classes are found from both modules in whichever module is run last.

This surprised me a bit. I was under the impression that the "temporary" class loader which this plugin uses is destroyed after each run (or at least after each maven module)

@keeganwitt
Copy link
Member

I haven't seen the behavior explicitly documented. This is all I could find: http://takari.io/book/91-maven-classloading.html. I'm doing some experiments to understand better.
I thought the way it worked was that each goal had it's own classpath (though I'm not sure what it did for submodules). But possibly I was mistaken and it's an artifact of the parent classloader I was choosing.

@keeganwitt
Copy link
Member

By the way, this doesn't happen when using includeClasspath of PROJECT_ONLY. This does require you to have the dependency in the project, instead of in the plugin, but it is a clue as to what's going on.

At the moment, I believe the parent classloader is the classloader from the Plexus ClassRealm. It appears this parent is reused for both goal executions, and that's why it seems to be additive (whichever one runs second has both depedencies), even if I create another classloader within the mojo's execute(). Still experimenting...

@keeganwitt keeganwitt self-assigned this Dec 13, 2020
@keeganwitt keeganwitt added this to the 1.12.0 milestone Dec 13, 2020
keeganwitt added a commit that referenced this issue Dec 13, 2020
Fix multi-module classpath issues (closes #183)
@TobiX
Copy link
Contributor Author

TobiX commented Dec 13, 2020

Thank you very much for the fix 👍🏽 I'll probably be able to test it after the holidays.

@keeganwitt
Copy link
Member

Thanks for reporting this (and for providing the test case)! I just made a release with this fix (1.12.0).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants