Adding Gson to a Jira Plugin: ClassDefNotFound issue

I’m having trouble adding Gson to my Jira plugin. Every time I try to use it, I get a ClassDefNotFound error. It’s really frustrating!

I’ve already tried these things:

  • Adding the Maven plugin with compile scope
  • Using provided scope for the Maven plugin
  • Putting com.google.gson.*;version="0" in Import-Package
  • Adding com.google.gson.* to Import-Package
  • Using an older Gson version that matches the provided dependencies

But nothing seems to work. I’m out of ideas. Has anyone faced this problem before? How did you solve it? Any tips or tricks would be super helpful. I’ve been stuck on this for hours and I’m not sure what else to try.

Thanks in advance for any help!

hey mate, had same issue few weeks back. wat helped me was using shade plugin to repackage gson into my plugin. just add maven-shade-plugin to ur pom.xml and configure it to relocate gson packages. this way u avoid conflicts with jira’s internal gson. gl with ur plugin!

I’ve encountered similar issues when working with Jira plugins. One solution that worked for me was to use the OSGi bundle instead of the regular Gson jar. Here’s what I did:

  1. Remove the Gson dependency from your pom.xml file.
  2. Download the Gson OSGi bundle from Maven Central.
  3. Place the bundle in the ‘lib’ directory of your plugin.
  4. Update your atlassian-plugin.xml to include the bundle.

This approach ensures that Gson is properly packaged and available within the OSGi environment of Jira. It resolved the ClassDefNotFound error for me and allowed smooth integration of Gson in my plugin.

If you’re still having issues, double-check your plugin’s manifest to ensure all necessary packages are correctly imported. Sometimes, the devil is in the details with OSGi and class loading in Jira plugins.

Having dealt with similar Jira plugin issues, I’d suggest looking into your plugin’s classloader. Often, the ClassDefNotFound error stems from Jira’s complex classloading hierarchy. Try explicitly declaring Gson as a plugin module in your atlassian-plugin.xml file. This way, you’re telling Jira to load Gson within your plugin’s context.

Also, ensure you’re not conflicting with Jira’s internal Gson version. Sometimes, using a specific version that’s known to work with your Jira version can solve the issue. If all else fails, consider bundling Gson within your plugin jar, but be cautious as this can lead to version conflicts if not managed properly.

Lastly, double-check your plugin’s manifest for any missing package imports. Sometimes, adding DynamicImport-Package: * can help, though it’s not always the best practice for long-term maintenance.