renannprado / quarkus-jte-extension

Integration library to use jte with Quarkus.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

java.lang.ClassNotFoundException is thrown if you use a "custom class" inside of the jte template

user667 opened this issue · comments

I have checked out your quarkus extension but I cannot get it to work. JteTemplateEngineFactory:14 requires the two properties JTE_QUARKUS_CLASS_PATH and JTE_QUARKUS_SOURCE_DIR to be set or any call to the renderer will fail. There's no documentation as to how to set these. How are they supposed to be used?

@user667 can you please share a project which I could use to reproduce the issue? I've just tried the example again and it works just fine, you actually don't need to set those variables, they are used by the library itself.

Are you using the below command to run your application?

./mvnw compile quarkus:dev

@renannprado, thx! Yes I tried running the application with

mvn compile quarkus:dev

Apart from the tests that do not run (I've read the note in your README about this), I still get the same error:

java.lang.IllegalStateException: JTE_QUARKUS_CLASS_PATH not found, template engine cannot be created

I've created a reproducer project https://github.com/user667/quarkus-jte

@user667 I've investigated a bit and it seems there's a bug in the library.

Basically it seems that the lib is using the wrong ClassLoader when creating the jte class, so it's not possible to use classes from your application inside of the template 🤦 . My bad.

java.lang.ClassNotFoundException: org.acme.Pojo

That's why using a String inside of the template is working just fine, but when you use a custom class, it fails.

But I've fixed your project to an extend, see the below diff. The most important thing is at the end, I've moved the page.jte file to the correct folder.

What you could do now is basically not use any custom classes inside of your template and then you'd be fine.

diff --git a/pom.xml b/pom.xml
index 865b8e2..ee8686d 100644
--- a/pom.xml
+++ b/pom.xml
@@ -1,7 +1,7 @@
 <?xml version="1.0"?>
 <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
-         xmlns="http://maven.apache.org/POM/4.0.0"
-         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
+    xmlns="http://maven.apache.org/POM/4.0.0"
+    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
     <modelVersion>4.0.0</modelVersion>
     <groupId>ch.elmundi</groupId>
     <artifactId>quarkus-jte</artifactId>
@@ -54,6 +54,10 @@
             <version>1.12.1</version>
         </dependency>
         -->
+         <dependency>
+            <groupId>io.quarkus</groupId>
+            <artifactId>quarkus-arc</artifactId>
+        </dependency>
         <dependency>
             <groupId>dev.renann</groupId>
             <artifactId>quarkus-jte</artifactId>
@@ -113,8 +117,19 @@
                 </executions>
             </plugin>
             <plugin>
-                <groupId>io.quarkus</groupId>
+                <groupId>io.quarkus.platform</groupId>
                 <artifactId>quarkus-maven-plugin</artifactId>
+                <version>${quarkus.version}</version>
+                <extensions>true</extensions>
+                <executions>
+                    <execution>
+                        <goals>
+                            <goal>build</goal>
+                            <goal>generate-code</goal>
+                            <goal>generate-code-tests</goal>
+                        </goals>
+                    </execution>
+                </executions>
             </plugin>
             <plugin>
                 <groupId>org.apache.maven.plugins</groupId>
diff --git a/src/main/java/ch/elmundi/playground/jte/JtePage.java b/src/main/java/ch/elmundi/playground/jte/JtePage.java
index 9ef5938..227eae8 100644
--- a/src/main/java/ch/elmundi/playground/jte/JtePage.java
+++ b/src/main/java/ch/elmundi/playground/jte/JtePage.java
@@ -24,6 +24,6 @@ public class JtePage {
     @GET
     @Produces(MediaType.TEXT_HTML)
     public String hello() {
-        return templateRenderer.render("templates/page.jte", Collections.singletonMap("page", page));
+        return templateRenderer.render("page.jte", Collections.singletonMap("page", page));
     }
 }
diff --git a/src/main/resources/templates/page.jte b/src/main/jte/page.jte
similarity index 100%
rename from src/main/resources/templates/page.jte
rename to src/main/jte/page.jte