[forge-users] Using template files from Jar

George Gastaldi ggastald at redhat.com
Mon Feb 16 13:19:34 EST 2015


Hi 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

On 02/16/2015 04:07 PM, forge-users at lists.jboss.org wrote:
> Hi,
>
> I am trying to use a template I put in src/main/resources
>
> BTW,  First, it was not included in the addon Jar built :/
> I had to modify the pom like this
>
> <build>
> *<resources>*
> *    <resource >*
> *       <directory>${basedir}/src/main/resources</directory>*
> *     </resource>*
> *  </resources>*
>    <finalName>ProjectName</finalName>
>
> to have it included.
>
>
> Now, I am trying to use it but how do I get a FileResource from the name of a file in the jar ?
> I tried several options like
>
> But I get an error message saying the file does not exist as the path included is wrong
> BUT the file object within the FileResource is ok
>
> The wrong path is prefixed with the project root !
> So it looks like
>
> java.lang.IllegalArgumentException: Template does not exist: d:\projects\myproject\*file:\C:\Users\Me\.forge*\addons\\fr-company-addons-ADDONS213b-1-0-1-SNAPSHOT\ADDONS213b-1.0.1-SNAPSHOT-forge-addon.jar!\voila.txt
>
> In the debugger this is the value displayed for the fileresource I create (its toString I presume) although the toString of its file attribute is correct.
>
> |  |  | URL voilaURL = getClass().getClassLoader().getResource("voila.txt");
> File f = new File(voilaURL.getFile()) ;
> FileResource<?> resource = ressfactory.create( f).reify(FileResource.class); // KO
> FileResource resource =  rfactory.create(new File(voilaURL.toURI()) ).reify(FileResource.class); // KO |
>
> I'm using version2.13.1 Final
>
> Any idea ?
>
> Thanks
>
> Posted by forums
> Original post: https://developer.jboss.org/message/918940#918940
>
> _______________________________________________
> forge-users mailing list
> forge-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/forge-users



More information about the forge-users mailing list