Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven Thread Safe Plugin Pitfalls

I'm working on building my first maven plugin and am targeting maven 3. I am trying to understand how the instantiation strategy plays into the thread safety. If I keep the default "per-lookup" strategy, what does that mean? To me that means each execution of the plugin will instantiate a new mojo making it virtually impossible to write a non-thread safe plugin.

If I select the "singleton" strategy, I can see how thread safety issues would crop up since the same mojo could be in use at the same time within different threads causing the usual thread safety issues (i.e class level variables) to come into play.

In summary, does the per-lookup instantiation strategy work like I am thinking? What are some general rules of thumb/guidelines to make sure maven 3 plugins are actually thread safe (and not just marked as thread safe in the @Mojo annotation)?

like image 557
jasonf Avatar asked Aug 28 '26 05:08

jasonf


1 Answers

You are correct, per-lookup means that a new instance is created for every lookup from plexus. Each execution results in a lookup, so yes, for every execution a new instance is created.

It is thus the safest way to prevent multithreading issues.

However, you could still create multithreading issues by using non-final static fields.

So, use per-lookup and don't use static variables, and you are safe.

like image 164
blackbuild Avatar answered Aug 30 '26 07:08

blackbuild



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!