Sérgio Sousa
Sérgio Sousa

Reputation: 81

Quarkus Qute template location from another maven module

Having a maven module that declares a Qute template and REST endpoint to render it, I wanted to include this module on another maven project. The problem is, at it seems, the destination module does not compile since it does not have / find the template in it's resources/templates location (the template is included in the jar of the included module).

Is there any way to instruct Qute (at build time) to read templates from other locations or disable this build check (since the template is in the classpath at the correct location?

The only way I can make it work roght now is to copy my template to the destination project at resources/templates but it doesn't seem the right solution.

Thanks in advance

Upvotes: 2

Views: 721

Answers (2)

Martin Kouba
Martin Kouba

Reputation: 1526

Yes, by default only the templates located in src/main/resources/templates are validated and can be injected.

You can parse any template content manually via Engine.parse() or even add a custom template locator via io.quarkus.qute.EngineBuilder.addLocator(), e.g. something like:

import io.quarkus.qute.EngineBuilder;

class MyEngineConfig {

   void configureEngine(@Observes EngineBuilder builder) {
     builder.addLocator(path -> Optional.of(new TemplateLocation() {

        @Override
        public Reader read() {
            return new BufferedReader(new InputStreamReader(FlowChartResource.class.getResourceAsStream(path)));
        }

        @Override
        public Optional<Variant> getVariant() {
            return Optional.empty();
        }

    }));
   }
}

The disadvantage is that Quarkus can't validate/inject such templates.

See also related issues https://github.com/quarkusio/quarkus/issues/12084 and https://github.com/quarkusio/quarkus/issues/10376.

Upvotes: 1

S&#233;rgio Sousa
S&#233;rgio Sousa

Reputation: 81

Ok, I found another solution, using "low-level" engine instance and parsing the template "manualy":

engine.parse(new BufferedReader(new InputStreamReader(FlowChartResource.class.getResourceAsStream([PATH to My Template]))).lines().collect(Collectors.joining("\n")))

Upvotes: 0

Related Questions