jte icon indicating copy to clipboard operation
jte copied to clipboard

Spring boot production does not find templates

Open Fabricio20 opened this issue 1 year ago • 11 comments

Hey team,

I've been trying to get JTE to work with spring boot, using the examples on the website. However during a production deployment the templates are simply not found/spring boot returns a 404.

During development (from IntelliJ/IDE startup) it works just fine, once the project is compiled into a jar (bootJar target or bootBuildImage) however it just doesn't find the templates.

I've messed around with the options below:

development-mode: false
use-precompiled-templates: true

Including a bunch of attempts at getting the gradle plugin to do something, the closest I got was to have it acutally bundle the .class files into the bootJar, however when that happens it fails to find the JTE "Content" class (even if the dependency is in the libs directory on the final jar).

This is how my gradle build script looks now (relevant parts):

plugins {
    id 'java'
    id 'io.freefair.lombok' version '8.6'
    id 'org.springframework.boot' version '3.4.3'
    id 'io.spring.dependency-management' version '1.1.7'
    id 'gg.jte.gradle' version '3.1.16'
}

bootJar {
    dependsOn(tasks.precompileJte)
    from(fileTree("jte-classes") {
        include("**/*.class")
    })
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'gg.jte:jte:3.1.16'
    implementation 'gg.jte:jte-spring-boot-starter-3:3.1.16'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}

jte {
    precompile()
}

With all that, how are we supposed to actually get this to work? I feel like i've exhausted all my options here besides not using the spring plugin / jte-spring-boot-starter but that doesn't feel right.

To clarify, it works perfectly fine in development mode/on the IDE, but fails in production (bootJar). There are no errors on production (it just results in a 404). With precompilation (as above/gradle) it results in a ClassNotFound for the gg.jte.Content class (even if said class is in spring boot's libs directory).

Fabricio20 avatar Mar 20 '25 17:03 Fabricio20

I've finally figured out something that works, here are the relevant changes:

jte {
    precompile()
}

dependencies {
    implementation 'gg.jte:jte:3.1.16'
    implementation 'gg.jte:jte-spring-boot-starter-3:3.1.16'
}

bootJar {
    dependsOn(tasks.precompileJte)
    with(bootInf {
        from(fileTree("jte-classes") {
            include("**/*.class")
        }).into("classes")
    })
}

The main change is adding the with section to the bootJar target as well as the into directive, this causes gradle to move the class files into the correct locations so that it gets picked up in spring.

This is with the following spring options (using precompiled templates):

gg:
  jte:
    development-mode: false
    use-precompiled-templates: true

While this setup works, looking at the generated bootJar layout, it looks incorrect.

BOOT-INF/
  classes/
    gg/ <-- This is where they should be, and in this setup they are.
  lib/
org/
classes/ <-- This should not be here, it's just the precompiled jte class files

I think either a documentation update or (more likely) changes to the gradle plugin might be worth looking into, this was extremely frustrating to figure out. It's also still not clear to me if I should be using generate() or precompile() target on the jte gradle configuration, given that the bootJar task is the same on development and production and depending on precompileJte while having generate() causes the precompile task to be SKIPPED.

It was thanks to github code search that I came across this fix, on this repository: https://github.com/fiwka/sosilol/blob/8a44ea99efe67e9c2bc16eacfd15eee1140b8078/build.gradle.kts#L79

Fabricio20 avatar Mar 21 '25 17:03 Fabricio20

I need to try, but can't you add the generated/precompiled classes to main resource file set and they end up in the correct place in the jar file?

EDIT: Or could you just set the output diretory for the gradle jte precompilation task to write the files into build/classes?

atomfrede avatar Apr 14 '25 07:04 atomfrede

I had the similar issue. As recommended by @atomfrede the simplest solution is to configure the Maven plugin and set the targetDirectory as shown below

<targetDirectory>${project.build.outputDirectory}</targetDirectory>

I can confirm this to be working. In my opinion this should be mentioned in the documentation. It took a quite some time to figure out given the fact that Spring Boot autoconfiguration configures everything for you.

Another option is to define your own bean for TemplateEngine and specify the path to generated classes directory as shown below. Note that the generated classes location should be on the classpath

@Bean
public TemplateEngine templateEngine() {
  final ClassPathResource cpr = new ClassPathResource("/"); //replace '/' with targetDirectory configured in the Maven plugin
  return TemplateEngine.createPrecompiled(cpr.getFile().topath(), ContentType.Html);
}

I would also recommend to only conditionally enable this when the usePrecompiledTemplates configuration property is set to true.

shirishpandharikar avatar Jul 17 '25 15:07 shirishpandharikar

I had same problem since I exported jar file.

I had the similar issue. As recommended by @atomfrede the simplest solution is to configure the Maven plugin and set the targetDirectory as shown below

This is a big hint.

Problems

Some pages returned always 404 error even though the controller could be accessed without any error. It was happened only after Jar file is executed directly so it takes time to debug.

Before

I searched jar file and noticed there are only libraries even I use JTE gradle plugin and JTE springboot integration.

 jar -tvf ./build/libs/auth-1.0-SNAPSHOT.jar | grep jte

 81041 Fri Feb 01 00:00:00 JST 1980 BOOT-INF/lib/jte-3.2.1.jar
  3964 Fri Feb 01 00:00:00 JST 1980 BOOT-INF/lib/jte-extension-api-3.2.1.jar
 20257 Fri Feb 01 00:00:00 JST 1980 BOOT-INF/lib/jte-kotlin-3.2.1.jar
 52419 Fri Feb 01 00:00:00 JST 1980 BOOT-INF/lib/jte-runtime-3.2.1.jar
 13202 Fri Feb 01 00:00:00 JST 1980 BOOT-INF/lib/jte-spring-boot-starter-3-3.2.1.jar

Solution

I added /build/generated-sources/jte directory as class source.

#build.gradle.kts

sourceSets.main {
    // Check if JTE generates to 'generated-sources' or 'generated-resources'
    // JTE typically generates Java source files, so we add it to the 'java' source set.
    java.srcDir(layout.buildDirectory.dir("generated-sources/jte"))
}

Please following official document, I just wanted direct solution because current my build setting already jte classes was built.

After

We can see BOOT-INF/classes/gg/jte/**.class are loaded

jar -tvf ./build/libs/auth-1.0-SNAPSHOT.jar | grep jte

 81041 Fri Feb 01 00:00:00 JST 1980 BOOT-INF/lib/jte-3.2.1.jar
  3964 Fri Feb 01 00:00:00 JST 1980 BOOT-INF/lib/jte-extension-api-3.2.1.jar
 20257 Fri Feb 01 00:00:00 JST 1980 BOOT-INF/lib/jte-kotlin-3.2.1.jar
 52419 Fri Feb 01 00:00:00 JST 1980 BOOT-INF/lib/jte-runtime-3.2.1.jar
 13202 Fri Feb 01 00:00:00 JST 1980 BOOT-INF/lib/jte-spring-boot-starter-3-3.2.1.jar
     0 Fri Feb 01 00:00:00 JST 1980 BOOT-INF/classes/gg/jte/
     0 Fri Feb 01 00:00:00 JST 1980 BOOT-INF/classes/gg/jte/generated/
     0 Fri Feb 01 00:00:00 JST 1980 BOOT-INF/classes/gg/jte/generated/precompiled/
  2095 Fri Feb 01 00:00:00 JST 1980 BOOT-INF/classes/gg/jte/generated/precompiled/JteemptyGenerated$Companion.class
  2018 Fri Feb 01 00:00:00 JST 1980 BOOT-INF/classes/gg/jte/generated/precompiled/JteemptyGenerated.class
     0 Fri Feb 01 00:00:00 JST 1980 BOOT-INF/classes/gg/jte/generated/precompiled/account/
     0 Fri Feb 01 00:00:00 JST 1980 BOOT-INF/classes/gg/jte/generated/precompiled/error/
     0 Fri Feb 01 00:00:00 JST 1980 BOOT-INF/classes/gg/jte/generated/precompiled/layout/
     0 Fri Feb 01 00:00:00 JST 1980 BOOT-INF/classes/gg/jte/generated/precompiled/share/
  6271 Fri Feb 01 00:00:00 JST 1980 BOOT-INF/classes/gg/jte/generated/precompiled/account/JteloginGenerated$Companion$render$1.class
  3547 Fri Feb 01 00:00:00 JST 1980 BOOT-INF/classes/gg/jte/generated/precompiled/account/JteloginGenerated$Companion.class
  2495 Fri Feb 01 00:00:00 JST 1980 BOOT-INF/classes/gg/jte/generated/precompiled/account/JteloginGenerated.class

As @shirishpandharikar said,

I can confirm this to be working. In my opinion this should be mentioned in the documentation. It took a quite some time to figure out given the fact that Spring Boot autoconfiguration configures everything for you.

It seems the document currently says about it

#https://jte.gg/pre-compiling/
        <targetDirectory>${project.build.directory}/jte-classes</targetDirectory>
#https://jte.gg/spring-boot-starter-3/

Production[¶](https://jte.gg/spring-boot-starter-3/#production)

To use [precompiled Templates](https://jte.gg/pre-compiling/) in production, for use with a JRE environment, you need to configure the Maven/Gradle Plugin to precompile your templates:

GregYeo avatar Nov 15 '25 22:11 GregYeo

The document says this setting for gradle

tasks.jar{
    dependsOn(tasks.precompileJte)
    from(fileTree("jte-classes")) {
        include("**/*.class")
        include("**/*.bin")
    }
}

But if you are using bootJar command to export jar

tasks.bootJar{
    dependsOn(tasks.precompileJte)
    from(fileTree("jte-classes")) {
        include("**/*.class")
        include("**/*.bin")
    }
}

You should add dependency on bootJar task. It would be confusing spring user.

Then you will get these jar

 jar -tvf ./build/libs/auth-1.0-SNAPSHOT.jar | grep jte 
     0 Fri Feb 01 00:00:00 JST 1980 gg/jte/
 81041 Fri Feb 01 00:00:00 JST 1980 BOOT-INF/lib/jte-3.2.1.jar
  3964 Fri Feb 01 00:00:00 JST 1980 BOOT-INF/lib/jte-extension-api-3.2.1.jar
 20257 Fri Feb 01 00:00:00 JST 1980 BOOT-INF/lib/jte-kotlin-3.2.1.jar
 52419 Fri Feb 01 00:00:00 JST 1980 BOOT-INF/lib/jte-runtime-3.2.1.jar
 13202 Fri Feb 01 00:00:00 JST 1980 BOOT-INF/lib/jte-spring-boot-starter-3-3.2.1.jar
     0 Fri Feb 01 00:00:00 JST 1980 gg/jte/generated/
     0 Fri Feb 01 00:00:00 JST 1980 gg/jte/generated/precompiled/
  2078 Fri Feb 01 00:00:00 JST 1980 gg/jte/generated/precompiled/JteemptyGenerated$Companion.class
  2004 Fri Feb 01 00:00:00 JST 1980 gg/jte/generated/precompiled/JteemptyGenerated.class
     0 Fri Feb 01 00:00:00 JST 1980 gg/jte/generated/precompiled/account/
     0 Fri Feb 01 00:00:00 JST 1980 gg/jte/generated/precompiled/error/
     0 Fri Feb 01 00:00:00 JST 1980 gg/jte/generated/precompiled/layout/
     0 Fri Feb 01 00:00:00 JST 1980 gg/jte/generated/precompiled/share/
  6254 Fri Feb 01 00:00:00 JST 1980 gg/jte/generated/precompiled/account/JteloginGenerated$Companion$render$1.class
  3530 Fri Feb 01 00:00:00 JST 1980 gg/jte/generated/precompiled/account/JteloginGenerated$Companion.class
  2481 Fri Feb 01 00:00:00 JST 1980 gg/jte/generated/precompiled/account/JteloginGenerated.class
  3617 Fri Feb 01 00:00:00 JST 1980 gg/jte/generated/precompiled/account/JtesignupGenerated$Companion$render$1.class
  3092 Fri Feb 01 00:00:00 JST 1980 gg/jte/generated/precompiled/account/JtesignupGenerated$Companion.class
  2213 Fri Feb 01 00:00:00 JST 1980 gg/jte/generated/precompiled/account/JtesignupGenerated.class
     0 Fri Feb 01 00:00:00 JST 1980 gg/jte/generated/precompiled/account/result/
  2827 Fri Feb 01 00:00:00 JST 1980 gg/jte/generated/precompiled/error/Jteerr_400Generated$Companion$render$1.class
  4231 Fri Feb 01 00:00:00 JST 1980 gg/jte/generated/precompiled/error/Jteerr_400Generated$Companion.class
  2538 Fri Feb 01 00:00:00 JST 1980 gg/jte/generated/precompiled/error/Jteerr_400Generated.class
  1930 Fri Feb 01 00:00:00 JST 1980 gg/jte/generated/precompiled/error/Jteerr_404Generated$Companion$render$1.class
  3540 Fri Feb 01 00:00:00 JST 1980 gg/jte/generated/precompiled/error/Jteerr_404Generated$Companion.class
  2278 Fri Feb 01 00:00:00 JST 1980 gg/jte/generated/precompiled/error/Jteerr_404Generated.class
  2828 Fri Feb 01 00:00:00 JST 1980 gg/jte/generated/precompiled/error/Jteerr_500Generated$Companion$render$1.class
  4231 Fri Feb 01 00:00:00 JST 1980 gg/jte/generated/precompiled/error/Jteerr_500Generated$Companion.class
  2537 Fri Feb 01 00:00:00 JST 1980 gg/jte/generated/precompiled/error/Jteerr_500Generated.class
  1980 Fri Feb 01 00:00:00 JST 1980 gg/jte/generated/precompiled/error/Jteerr_defaultGenerated$Companion$render$1.class
  4188 Fri Feb 01 00:00:00 JST 1980 gg/jte/generated/precompiled/error/Jteerr_defaultGenerated$Companion.class
  2477 Fri Feb 01 00:00:00 JST 1980 gg/jte/generated/precompiled/error/Jteerr_defaultGenerated.class
  4003 Fri Feb 01 00:00:00 JST 1980 gg/jte/generated/precompiled/layout/JtemainGenerated$Companion.class
  2312 Fri Feb 01 00:00:00 JST 1980 gg/jte/generated/precompiled/layout/JtemainGenerated.class
     0 Fri Feb 01 00:00:00 JST 1980 gg/jte/generated/precompiled/layout/fragments/
  3775 Fri Feb 01 00:00:00 JST 1980 gg/jte/generated/precompiled/share/Jteshare_link_loginGenerated$Companion$render$1.class
  3597 Fri Feb 01 00:00:00 JST 1980 gg/jte/generated/precompiled/share/Jteshare_link_loginGenerated$Companion.class

GregYeo avatar Nov 15 '25 22:11 GregYeo

Can someone provide a valid configuration which works with spring-boot and gradle setup? The documentation is misleading completely, would appreciate

Imaisen17 avatar Dec 24 '25 14:12 Imaisen17

I stumbled on this problem when I tried to publish my Docker container with Spring Boot layered-jar. I used precompilation because my templates import classes from my packages. The documentation is indeed not clear at all about how to handle bootJar, and the plugin did not pre-configure the tasks. After several days of struggling, I managed an acceptable solution for my use case. The solution is inspired by the maven plugin described here.

jte {
    precompile()
    targetDirectory = layout.buildDirectory.dir("generated-templates").get().asFile.toPath()
}

tasks.register('copyPrecompiledJteTemplates', Copy) {
    dependsOn precompileJte
    from fileTree(jte.targetDirectory) {
        include '**/*.class'
    }
    into layout.buildDirectory.dir("classes/java/main").get().asFile
}
// For jar and build
jar.dependsOn copyPrecompiledJteTemplates

// For bootJar and bootRun
resolveMainClassName.dependsOn copyPrecompiledJteTemplates

// For test
compileTestJava.dependsOn copyPrecompiledJteTemplates
  • Precompiled templates are put inside build folder, which is generally not included in Git, so dev will not inadvertently commit the precompiled templates.
  • Use default output package for precompiled templates, so no need to customize TemplateEngine bean. Put TemplateEngine.createPrecompiled(ContentType.Html) as a @Bean in @Configuration class is enough.
  • Precompiled templates .class are copied to the runtime classpath of main source set for both IDE and bootRun to pick up.
  • Only .class precompiled templates are included in the .jar file. No .java files, and no duplicate entries.

But :

  • Requires any classes that are imported into templates to be public.
  • Resources of templates are not handled yet. However, it should be possible to copy them in a similar way.

tranhan5010 avatar Apr 24 '26 15:04 tranhan5010

Have you tried using the generate task instead of precompile?

jte {
    generate()
}

I don't know how to set up a Spring project like these, so this is just something to try.

edward3h avatar Apr 25 '26 20:04 edward3h

I tried following the doc for generate but it failed since the templates require imports of classes from our package. The proposed solution in documentation for precompile did work for Spring Boot's BootJar, but not ideally due to the duplication of precompiled class files.

tranhan5010 avatar Apr 26 '26 19:04 tranhan5010

And this problem is still unsolved even now, Oct 2026. JTE works fine during development, but when it comes to PRODUCTION phase, everything feels new again. I have been trying to deploy my jte based app, but it always crash with missing classes. Anyone with real solution?

spring boot 4.1.0; build.gradle.kts; application.properties

Gsquare47 avatar Oct 11 '26 19:10 Gsquare47

@Gsquare47 can you share an example project that demonstrates the problem?

edward3h avatar Oct 11 '26 19:10 edward3h