So I'm trying to write a Stash post receive hook plugin that eventually writes some pertinent information to a Google spreadsheet. To do this, I have to pull the private key for Oauth2 authentication from a .p12 file. I made a couple test projects that were just plain Java command line apps and I would just out the .p12 file in the root and call it from there. I made another one where I would call it from the resources folder and that worked fine too.
I ported that code over, got all my dependencies settled up, and ran it. I naively dropped the .p12 file in the root and obviously got a file not found exception. Looked at the target directory, saw it never brought that over, readjusted and put it in the resources folder to see if that would get carried over. Another file not found exception and still not in the target directory. I tried putting it in a couple different other places just to see if it would be brought over. Nothing so far has worked.
What am I doing wrong here?
I guess I could put the file right on my Stash instance and call it from there, but I would rather have it self contained right in the plugin.
I've searched around a lot but didn't find anything really relevant. Anyone have any suggestions?
Community moderators have prevented the ability to post new answers.
Hi Alex,
The easiest thing to do is to load your file from within your plugin's jar file rather than loading it from the file system directly. If you put the file in the 'src/main/resources' directory of your source tree, it will get embedded in the jar file by default. Any path structure below the 'resources' directory will be maintained when the file is embedded.
So, for example, if I put a file in 'src/main/resources/authentication/google.p12', I can stream the contents of the file using the following code snippet:
import java.io.IOException; import java.io.InputStream; import org.apache.commons.io.IOUtils; InputStream is = null; try { is = this.getClass().getClassLoader().getResourceAsStream("authentication/google.p12"); byte[] filedata = IOUtils.toByteArray(is); // TODO: Do what is required with the file's contents } catch (IOException ioe) { // TODO: Handle exception } finally { if (is != null) { IOUtils.closeQuietly(is); } }
Hi, Joseph. I am looking for an example to create a page in Confluence though Confluence REST API. I read your example about how to update page, it is very helpful. But I want to create a page. Can you give me some examples? Thank you very much.
I figured out now. But still thank you for your update example. It's very helpful. orz
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Ah! My issue was that I just had the .p12 file directly in the resources directory.
So previously I just had
stream = this.getClass().getClassLoader().getResourceAsStream("google.p12");
As soon as I put it in its own directory, it worked perfectly.
Thank you very much!
This may be a dumb question but why does it have to be in its own folder? I would think just having it in the root resources directory would be fine?
Thank you again.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.