Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spring boot production does not find templates #438

Open
Fabricio20 opened this issue Mar 20, 2025 · 1 comment
Open

Spring boot production does not find templates #438

Fabricio20 opened this issue Mar 20, 2025 · 1 comment

Comments

@Fabricio20
Copy link

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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant