3 Replies Latest reply on Feb 17, 2015 5:36 AM by alwad

    Using template files from Jar

    alwad

      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

        • 1. Re: Using template files from Jar
          alwad

          Same behaviour with 2.14

          • 2. Re: Using template files from Jar
            gastaldi

            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

            1 of 1 people found this helpful
            • 3. Re: Using template files from Jar
              alwad

              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