-
Notifications
You must be signed in to change notification settings - Fork 11
Custom Guice modules in plugins are not registered in the plexus container, documentation is wrong #35
Comments
What bytecode is your output? Maven 3.9.1 uses Sisu 0.3.5 that supports only up to Java 11 bytecode, anything above that will be ignored by Sisu. |
If all good (re bytecode) you still should not mix Mojo annotations/API and JSR330. Fix for your issue should be:
|
Thanks for the tips and the quick reply ! 👍 |
And one more hint: ift you do not depend on Guice, just by using this single Module class, then instead of module just create:
|
Thanks again, this can be very useful! |
@cstamas one last question before I switch the subject, custom modules annotated with |
Yes, they must have default ctor |
I have the same issue with the bean injected by the SpringModule class of spring-guice project. Here is how I autocreate the module:
The beans does not get injected as Guice component in my Mojo. Even when I try to inject a spring managed bean myself in a custome Module like below, it does not work.
The normal component with ctor is injected, but not the spring managed instance. However if I try to instantiate the spring container in a Guice component itself, get the bean and use it, it works fine.
Is there a difference between Sisu and Guice that can cause this issue ? |
@cstamas I have downgraded to JDK 11, removed the ctor and used inject in the field itself. |
Am not a spring guy, but best would be if you create a reproducer in some shared repo, so i can look. Nothing guaranteed |
Are you doing this in a plugin or extension? IIRC Maven doesn't expose the Guice API to plugins, so you'll need to stick with the javax.inject API using approaches like a writing a |
@cstamas I would like to debug to see what happens but I couldn't do it with I also tried to do some logging, but it also fails to print any output during modules initialisation. Is there a way to debug the initialisation of the modules in sisu ? |
Just for your information, It worked fine. I managed to bind the bean coming from the spring library. But I just cant make it work when I package them all in a plugin and run inside maven. |
Hi - I suspect it's because Maven doesn't expose the Guice API (ie.
com.google.inject) from maven-core to plugins.
This means your plugin loads com.google.inject classes from the plugin's
class-loader rather than using the classes from maven-core, so you end up
with different Module classes - one loaded by Maven itself and the other
loaded by your plugin. Sisu sees the same com.google.inject classes as
maven-core, which is why it won't automatically pick up your module
(because it uses the different com.google.inject classes loaded by the
plugin.)
This also explains why your example works outside of Maven, but not when
it's wrapped in a plugin.
You should still be able to re-use the SpringModule, but you'll need to
expose the bindings a different way - I'll try to find time over the
weekend to write up an example.
… Message ID: ***@***.***>
|
@mcculls I tried to see if anything changes with an extention instead of a plugin, but it failed with same result. |
@mcculls ok! sorry! I just understood what you mean :) My POJOs that I inject in my MOJO are actually not binded by my custome modules! Now I am wondering what am I doing wrong, because my custome modules dont' work at all, Which is weird is that, I see MySpringModule as a component in the plexus container when I lookup with a LifeCycleParticipant |
No worries - I wondered if it might be using the default constructors. I'll need to confirm, but I think the issue is that Maven doesn't currently expose the Guice module API - so while you can use modules in your plugin, and query them via the lookup API, classes like This means that Sisu, which lives inside core, cannot cast your module instance to its One approach would be to create a Guice injector inside your plugin and have JSR330 annotated |
@mcculls Ok! Thanks! it is much clearer now :) Also just to be sure to understand: |
Right now I believe that's the case for Maven, because IIRC
com.google.inject is not exported from core. Other apps that migrated from
Plexus (like Nexus) did expose it, and that's where that advice came from.
|
@mcculls Thanks a lot for the clarification.
Is there any work going on to export this api in future versions of maven ?
Also, I think this tutorial is a bit misleading.
https://github.com/eclipse/sisu.plexus/wiki/Plexus-to-JSR330#custom-bindings
Because there are lots of SO threads opened by people who fell for the same.
If you can confirm that custom bindings don't work in plugins, this ticket would be a great helper for all.
…On Mon, May 29, 2023, 4:43 PM Stuart McCulloch ***@***.***> wrote:
Right now I believe that's the case for Maven, because IIRC
com.google.inject is not exported from core. Other apps that migrated from
Plexus (like Nexus) did expose it, and that's where that advice came from.
—
Reply to this email directly, view it on GitHub
<#35 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AJIV32OWZKWX2Q5TVZVPEVTXISYYLANCNFSM6AAAAAAXZ7HWJY>
.
You are receiving this because you modified the open/close state.Message
ID: ***@***.***>
|
@mcculls I tried to use modules in an extension but couldt make it work either.
BTW the |
I will create another issue for the extention and keep this issue for waiting your confirmation on the usage of custom bindings in plugins |
I too would like to be able to use custom bindings in plugins. The documentation is either misleading or plain wrong. I have managed to debug a plugin execution, and I can't see any code anywhere that ever would register a custom UPDATE: after some more debugging I can see that the |
@dsyer thanks for the analysis, I wanted to debug as well but didnt have time. |
I think opening custom modules for plugins using the available sisu.inject mechanisms would be very dangerous. There's a reason the class loader boundaries are so strict - it's what keeps Maven plugins vaguely sane and working in spite of big changes in the Maven project infrastructure. So I think we have to ask ourselves, what is it we actually need here? I think for me it is 2 things: 1) make my The best I could come up with was to include Guice on the plugin classpath and create an @Named
class InjectorProvider implements Provider<Injector> {
private final Injector injector;
@Inject
InjectorProvider(MavenSession session) {
injector = Guice.createInjector(new MyModule());
// ... do stuff with MavenSession etc.
}
public Injector get() {
return injector;
}
} Then inject that @Named
class ServiceProvider implements Provider<Service> {
private final Injector injector;
@Inject
ServiceProvider(Injector injector) {
this.injector = injector;
}
public Service get() {
return injector.getInstance(Service.class);
}
} This makes it explicit that you don't expect all the bindings in your library to end up in the plugin Plexus container, which I think is probably a good thing. It's a bit unfortunate that you have to write boilerplate code to expose the components that you need in your |
I agree with that. What about an extention ? is it acceptable to delegate the responsability of not breaking everything in plexus to a developper of a maven extention and allow him/she to add custom bindings? if so we can as well allow plugins use existing bindings from external libraries. |
I don’t know if an extension helps. The class loader boundary is constructed differently I think, but the isolation is just as strict. Happy to be wrong. Anyway, I have a proof of concept for a library that creates a new injector like in the code above. I’ll play around with it some more and see if it’s worth publishing. |
if you accept help I would love to contribute ^_^ |
I put the code here: https://github.com/scratches/plugin-demo/. |
Ok I see that you used a plugin as a dependency of the first plugin to generate automatically the providers, I was trying to do the same during launch break, but you were faster :D |
I think this issue should be fixed as a documentation issue at the very least, so I would leave it open. Maybe change the summary? |
Since @cstamas had initially proposed to use the However I still prefer using an extention and use the Spring-Guice project, So I tried to create a configurable extension. Also, @mcculls , I would like to contribute to fix the documentation. |
@HomeOfTheWizard thanks for all your work on this - yes, moving the doc to a github page is fine with me |
Hello,
Not sure what I am doing wrong but I cannot load a custome Guice/Sisu module to inject cutom bindings as explained in the documentation
https://github.com/eclipse/sisu.plexus/wiki/Plexus-to-JSR330#custom-bindings
I am using maven 3.9.1
and developping with OpenJDK 17.
Here is my pom.xml
Here is my Mojo:
Here is my Module with custom bindings:
Can you please help me sort this out ?
The text was updated successfully, but these errors were encountered: