Can one service instance be injected into multiple plugins to avoid to generating duplicated Object

I am developing the service which can download custom rule from web.
Multiple plugins which my team is developing will rely on it.
So when the service is injected into the plugins, the service instance will be created in each plugins.
It leads the custom rule will be downloaded multiple times.

My question is: Can one service instance be injected into multiple plugins?

Hi,

There is a special convention that will make all classes located into package org.sonar.plugins.<pluginKey>.api visible to all other plugins.

So you can package your shared service in a single plugin, in the API package, and inject it into all other plugins.

Sorry, I replied late.
So should I build service jar to a plugin?
I should use <packaging>sonar-plugin</packaging> instead of <packaging>jar</packaging> in pom.xml, right?

Yes, create a plugin containing the shared behavior.

Thanks I will try it soon.

Thanks. It works when my package name is org.sonar.plugins.<pluginKey>.

Another question is about the guide: https://docs.sonarqube.org/display/DEV/API+Basics
Is the description wrong? because I tried org.mycompany.<pluginKey>, it didn’t work.

Caused by: java.lang.ClassNotFoundException: net.xxx.sonar.g11n.api.Spelling (pluginKey is “g11n”)
It worked after I change net.xxx.sonar.g11n to org.sonar.plugins.g11n

Exposing APIs to Other Plugins

The common use case is to write a language plugin that will allow some other plugins to contribute additional rules (see for example how it is done in the Java plugin). The main plugin will expose some APIs that will be implemented/used by the “rule” plugins.

Plugins are loaded in isolated classloaders. It means a plugin can’t access another plugin’s classes. There is an exception for package names ending with <pluginKey>.api . For example all classes in a plugin with the key myplugin that are located in org.mycompany.myplugin.api are visible to other plugins.

Documentation is wrong, I will fix it. Thanks for reporting it.