Access Resources bundled within Plugin

Hello!

I want to access some images (as Files, not Icons) and one json file which I bundle with my plugin.
I have the structure

src/
     main/
          java/
          res/


In the debug builds the acces works fine via:

 
final PluginId pluginId = PluginId.getId("<id>");
final
IdeaPluginDescriptor pluginDescriptor = PluginManager.getPlugin(pluginId);
if
(pluginDescriptor == null) {
    return null;
}
final ClassLoader classLoader = pluginDescriptor.getPluginClassLoader();
final
URL resource = classLoader.getResource(filePath);


But after I create a deploy build and install it, that is not working anymore. What am I doing wrong?
I even compared the structure of the generated plugin zips. In the debug build the content of my plugin is extracted to a "classes" folder, on the other hand in the deployment build, it is still a jar.

Thanks a lot!

Marc

1 comment
Comment actions Permalink

Yeah. I too got bitten by it. The debug environment unpacks the jar while the deployed environment does not. So the resources are not available in deployed version same as in debug. I would prefer that the debug version mimicked the deployed one, otherwise it makes it harder to debug.

Here is what I use to access a font resource. I create a file in the temp directory by copying the resource stream. For everything else that can be represented as a string it is easier to just grab the text of the resource with Resources.toString(resourceFile, Charsets.UTF_8)

Can't vouch that there is no better way. I am not a Java Guru. It does solve the problem for binary files.

    protected String createCustomFontUrl() {         try {             InputStream fontStream = getClass().getResourceAsStream("/com/vladsch/idea/multimarkdown/taskitems.ttf");             File tempFontFile = new File(tempDirPath + "multimarkdown_taskitems.ttf");             logger.info("creating temp font file: " + tempFontFile.getAbsolutePath());             FileOutputStream fileOutputStream = new FileOutputStream(tempFontFile);             byte[] buffer = new byte[32768];             int length;             while ((length = fontStream.read(buffer)) > 0) {                 fileOutputStream.write(buffer, 0, length);             }             fileOutputStream.close();             fontStream.close();             return tempFontFile.toURI().toURL().toExternalForm();         } catch (FileNotFoundException e) {             e.printStackTrace();         } catch (IOException e) {             e.printStackTrace();         }         return null;     }

0

Please sign in to leave a comment.