我正在开发ratpack.io Web应用程序并使用gradle作为构建工具。模板是从 src/main/thymeleaf 目录中的模板文件渲染的,它在运行时运行良好(只需使用 gradle run)。
$ ls ./src/main/thymeleaf
home.html
我在创建 uber jar 时遇到了问题,其中不包含模板文件。当我破解输出 jar 文件时,我看到 thymeleaf 目录是空的。
我知道 Shadow-jar 过程的一部分是将所有依赖项合并到一个 jar 中,但我不确定我需要做什么来包含模板文件。我尝试创建特殊规则来包含 html 文件,但最终我只得到 jar 中的 html 文件,甚至没有得到 thymeleaf 目录中的文件。
- 我需要配置什么才能获取此 uber jar 中包含的模板文件?
- 如果我确实获取了 jar 模板目录中包含的文件,我是否需要更新模板解析器以从 jar 与当前工作目录中提取文件?
最佳答案
您将模板文件保留在 src/main/thymeleaf
中是否有任何原因?默认情况下,Thymeleaf 模板应存储在 src/ratpack/thymeleaf 目录中。
ThymeleafModule
类定义存储所有模板的文件夹名称。默认值为 thymeleaf,当您创建 ShadowJar 时,您应该在 JAR 存档中找到一个文件夹 thymeleaf。 ShadowJar 将 src/ratpack/thymeleaf 复制到此目的地,没有任何问题。
基于 Java 的 Ratpack 项目默认不知道 src/ratpack
,但您可以通过在 src 中创建一个名为
并配置 .ratpack
的空文件来轻松配置它/ratpackserver -> server.findBaseDir()
(下面有更详细的示例)。
这是一个简单的例子:
build.gradle
buildscript {
repositories {
jcenter()
}
dependencies {
classpath "io.ratpack:ratpack-gradle:1.5.4"
classpath "com.github.jengelman.gradle.plugins:shadow:1.2.4"
}
}
apply plugin: "io.ratpack.ratpack-java"
apply plugin: "com.github.johnrengelman.shadow"
apply plugin: "idea"
apply plugin: "eclipse"
mainClassName = 'app.RatpackApp'
repositories {
jcenter()
}
dependencies {
// Default SLF4J binding. Note that this is a blocking implementation.
// See here for a non blocking appender http://logging.apache.org/log4j/2.x/manual/async.html
runtime 'org.slf4j:slf4j-simple:1.7.25'
compile ratpack.dependency('thymeleaf')
compile ratpack.dependency('guice')
testCompile "org.spockframework:spock-core:1.0-groovy-2.4"
}
src/main/java/app/RatpackApp.java
package app;
import ratpack.guice.Guice;
import ratpack.server.BaseDir;
import ratpack.server.RatpackServer;
import ratpack.thymeleaf.ThymeleafModule;
import java.util.HashMap;
import static ratpack.thymeleaf.Template.thymeleafTemplate;
public final class RatpackApp {
public static void main(String[] args) throws Exception {
RatpackServer.start(server ->
server.serverConfig(config -> config.findBaseDir())
.registry(Guice.registry(bindings -> bindings.module(ThymeleafModule.class)))
.handlers(chain -> chain.get(ctx -> ctx.render(thymeleafTemplate(new HashMap<String, Object>() {{
put("title", "Hello, Ratpack!");
put("header", "Hello, Ratpack!");
put("text", "This template got rendered using Thymeleaf");
}}, "home"))))
);
}
}
src/ratpack/thymeleaf/home.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
<title th:text="${title}" />
</head>
<body>
<h1 th:text="${header}"></h1>
<p th:text="${text}" />
</body>
</html>
Remember to create an empty file
.ratpack
insrc/ratpack
so Ratpack can discover this location as a files base dir.
现在,在使用 gradle ShadowJar
创建最终 JAR 后,我可以看到模板文件被正确复制:
ratpack-thymeleaf-example [master●●] % unzip -l build/libs/ratpack-thymeleaf-example-all.jar | grep home
232 06-24-2018 10:12 thymeleaf/home.html
在这里您可以找到完整的示例 - https://github.com/wololock/ratpack-thymeleaf-example
关于gradle - Ratpack + Thymeleaf + ShadowJar - 解析模板时出错 "home",模板可能不存在或无法访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51006738/