Gradle Ratpack&x2B;百里香叶+;shadowJar-解析模板时出错;“家”;,模板可能不存在或不可访问
我正在开发ratpack.io web应用程序,并使用gradle作为构建工具。模板是从Gradle Ratpack&x2B;百里香叶+;shadowJar-解析模板时出错;“家”;,模板可能不存在或不可访问,gradle,groovy,thymeleaf,ratpack,shadowjar,Gradle,Groovy,Thymeleaf,Ratpack,Shadowjar,我正在开发ratpack.io web应用程序,并使用gradle作为构建工具。模板是从src/main/thymeleaf目录中的模板文件呈现的,该目录在运行时运行良好(只需使用gradle run) 我在创建uber jar时遇到了一些问题,其中不包括模板文件。当我打开输出jar文件时,我看到thymeleaf目录是空的 我知道shadow jar过程的一部分是将所有依赖项合并到一个jar中,但我不确定我需要做什么,包括模板文件。我尝试创建特殊的规则来包含html文件,但最终只得到jar中的
src/main/thymeleaf
目录中的模板文件呈现的,该目录在运行时运行良好(只需使用gradle run
)
我在创建uber jar时遇到了一些问题,其中不包括模板文件。当我打开输出jar文件时,我看到thymeleaf
目录是空的
我知道shadow jar过程的一部分是将所有依赖项合并到一个jar中,但我不确定我需要做什么,包括模板文件。我尝试创建特殊的规则来包含html文件,但最终只得到jar中的html文件,甚至没有来自thymeleaf目录的文件
- 我需要配置什么来获取这个uber jar中包含的模板文件李>
- 如果我真的得到了jar的template dir中包含的文件,那么我是否需要更新模板解析器来从jar中提取文件,而不是当前的工作目录李>
- 您有什么理由将模板文件保存在
中吗?默认情况下,Thymeleaf模板应存储在src/main/thymeleaf
目录中src/ratpack/Thymeleaf
类定义了一个文件夹名称,所有模板都存储在该文件夹中。默认值是templaemodule
,当您创建一个shadowJar时,您应该在JAR归档文件中找到一个文件夹thymeleaf
。shadowJar将thymeleaf
复制到此目的地,没有任何问题 默认情况下,基于Java的Ratpack项目不知道src/ratpack/thymeleaf
,但您可以通过在src/Ratpack
中创建名为src/Ratpack
的空文件并配置.Ratpack
来轻松配置它(下面有更详细的示例) 下面是一个简单的例子: 格雷德尔先生 src/main/java/app/RatpackApp.javaserver->server.findBaseDir()
在这里您可以找到完整的示例-我刚刚尝试使用groovy模板,得到了相同的结果,它们没有自动包含在uber jar中。我一定做了一些非常幼稚的事情,因为我不是唯一一个想在CLI构建和uberjar中都使用模板的人。我在“src/main/thymeleaf”中有这些模板,因为我看到的一个例子()就是这样放的。我认为这是正确的,因为模板解析程序正确地找到了它们。我是ratpack的新手,需要设置base dir,所以我将其设置为“src/main”以匹配示例。包括您在内的所有其他在线示例都是groovy示例,其中没有指定base dir,因此我不知道应该将其设置为什么。感谢@voodoogiant指出ratpack java。我已经更新了示例以涵盖ratpack java而不是ratpack groovy。Github示例应用程序也已更新。$ ls ./src/main/thymeleaf home.html
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" }
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")))) ); } }
<!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>
ratpack-thymeleaf-example [master●●] % unzip -l build/libs/ratpack-thymeleaf-example-all.jar | grep home 232 06-24-2018 10:12 thymeleaf/home.html