-
1. Re: Using template files from Jar
alwad Feb 16, 2015 1:16 PM (in response to alwad)Same behaviour with 2.14
-
2. Re: Using template files from Jar
gastaldi Feb 16, 2015 1:26 PM (in response to alwad)1 of 1 people found this helpfulHi Jerome,
Add your resources in the same folder structure as the Java class that will consume it and use getClass().getResource() to retrieve a URL instance out of it:
URL url = getClass().getResource("voila.txt");
Resource<?> resource = resourceFactory.create(url);
Tip: Using FileResource directly in your addon is a bad practice, as it relies on the physical file. The best would be to use Resource<?>. This makes your code unaware of the resource location.
Best Regards,
George Gastaldi
-
3. Re: Using template files from Jar
alwad Feb 17, 2015 5:36 AM (in response to gastaldi)Hello George,
Thank you
Indeed there is no need to use File or FileResource even if its a file after all (I'm a disciple of intuitive programming ).
That leads to a problem (project root appended before an ok full file path). (should I create a TestCase ?)
Unfortunately,
Resource<URL> templateResource = rfactory.create(getClass().getResource("voila.txt"));
brings null in templateResource although this syntax is used in the angular-js addon for example (FreemarkerClientTest.java)
For future reference, a working code is :
Resource<URL> templateResource = rfactory.create(getClass().getClassLoader().getResource("voila.txt")); Template template = tfactory.create(templateResource, FreemarkerTemplate.class); I'm using Java 7.0.40 on w7 64 and it works on my machine, today
As I plan to have a quantity of templates I prefer to put them in resources, separated from java sources.
I'll file an improvement request about that because what's the point of having the maven tree if a part of it is not used by default.
Cheers,
Jerome