I'm using the gmail API. When I war package my application, my client_secrets.json and credentials file are not inside. How do I ensure that they are available for use?
-
Don't package them into the executable; provide them at runtime instead. – chrylis -cautiouslyoptimistic- Nov 08 '15 at 11:21
-
@chrylis do you mean hosting the client_secrets.json outside the war file? – tery Nov 08 '15 at 11:25
-
Either that or not using JSON and building the credentials object from environment variables or properties. Spring Boot configuration properties make this simple. – chrylis -cautiouslyoptimistic- Nov 08 '15 at 11:28
-
@chrylis If i'm running the war from tomcat on ubuntu, which directory can I point it to? (I'm not sure which directories the tomcat user is allowed access to..) – tery Nov 08 '15 at 11:45
-
You should be able to set properties for your application in your Tomcat configuration. – chrylis -cautiouslyoptimistic- Nov 08 '15 at 15:06
1 Answers
In case you are using Maven or Gradle, you need to put them into /src/main/resources/client_secrets.json
. (not into the root folder of your project!)
Then they are 'inside' the jar.
Then you can load an pass it to GoogleClientSecrets
like this:
InputStream in = XXX.class.getResourceAsStream("/client_secrets.json");
GoogleClientSecrets clientSecrets =
GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
XXX
you should replace with your class name. Google is using the same in their quickstart. See: https://developers.google.com/gmail/api/quickstart/java Step 3.
Update regarding your comment:
Everything which you place inside /src/main/resources
is inside the "root" of your war-File. That means they are in the root of your classpath. All this stuff you can access over the classloader (not FileInputStream) like this:
XXX.class.getResourceAsStream("/folder/filename");
The FileInputStream
which you mentioned you use normally only to access files outside the war
Your suggestion with InputStream in = new FileInputStream("src/main/resources/credentials/gmail/client_secret.json");
Does only work inside your IDE because there the folder src/main/resources
really exists. When you package it as an war
, as i described above, the content of src/main/resources
is moved into the root of your war
.

- 11,040
- 17
- 69
- 104
-
thanks! resources does package the static file. what if I want to deploy with war? does "src/main/resources/credentials/gmail/client_secret.json" still work? – tery Nov 08 '15 at 11:02
-
Yes in `war`-package mode `/src/main/resources` are also considered. Otherwise all your logging configuration and stuff like that would be inside the war file. Is that was you asked? – d0x Nov 08 '15 at 11:19
-
not exactly...how do I now reference those resources? InputStream in = new FileInputStream("src/main/resources/credentials/gmail/client_secret.json"); doesn't work any more. – tery Nov 08 '15 at 11:22
-
-
makes sense, substituting stuff before I try deployment again...what do I change DATA_STORE_DIR = new File("src/main/resources/credentials/gmail"); to? – tery Nov 08 '15 at 11:37
-
Nope, `XXX.class.getResourceAsStream("/credentials/gmail/client_secret.json");` should work inside your IDE and on production. – d0x Nov 08 '15 at 11:45
-
oh, that works, thanks! for the second variable, DATA_STORE_DIR, what do I change it to? – tery Nov 08 '15 at 11:53
-
I am trying private static java.io.File DATA_STORE_DIR = new File(GmailQuickstart.class.getResource("/credentials/gmail/").toURI()); but it gives an unhandled exception, can't put a throws on the class... – tery Nov 08 '15 at 11:59
-
Stuff like this you can initialize in static constructors: http://stackoverflow.com/questions/335311/static-initializer-in-java But thats not a good style of programming – d0x Nov 08 '15 at 12:03
-
-
yep thanks! it works on my local machine, but still fails on my deployed instance. I can't figure out what happened...setting logging.path = classpath:/log still doesn't output a log file :/ – tery Nov 08 '15 at 12:33
-
You can open another question for that. Give some information about your enviroment (which spring version, server, ..., log configuration) – d0x Nov 08 '15 at 12:40