From 92b7de6befad658f45c3f4dc5024b077a6c67cad Mon Sep 17 00:00:00 2001 From: liuweiqing Date: Wed, 15 Nov 2023 10:21:05 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=83=AD=E9=87=8D=E8=BD=BD?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 未修改前idea可以热重载,但是命令行不行,因为在开发环境外,例如打包后的应用中,资源文件(如模板文件)被打包进了 JAR 文件,因此不能使用常规的文件系统路径来访问它们,所以Templates.java的path获取方式有问题,同时还有一个问题,就是应该替换/target/symphony/目录而不是/target/classes/ --- src/main/java/org/b3log/symphony/util/Templates.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/b3log/symphony/util/Templates.java b/src/main/java/org/b3log/symphony/util/Templates.java index 66753b2d3..1b7e32116 100644 --- a/src/main/java/org/b3log/symphony/util/Templates.java +++ b/src/main/java/org/b3log/symphony/util/Templates.java @@ -56,18 +56,26 @@ public final class Templates { TEMPLATE_CFG = new Configuration(FREEMARKER_VER); TEMPLATE_CFG.setDefaultEncoding("UTF-8"); try { - String path = Templates.class.getResource("/").getPath(); + String path = Templates.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath(); + LOGGER.info("Code source path: " + path); + LOGGER.info("Initial resource path: " + path); // 添加调试信息 if (StringUtils.contains(path, "/target/classes/") || StringUtils.contains(path, "/target/test-classes/")) { // 开发时使用源码目录 path = StringUtils.replace(path, "/target/classes/", "/src/main/resources/"); path = StringUtils.replace(path, "/target/test-classes/", "/src/main/resources/"); + LOGGER.info("Adjusted resource path for development environment: " + path); // 添加调试信息 + } + if (StringUtils.contains(path, "/target/symphony/")) { + // 开发时使用源码目录 + path = StringUtils.replace(path, "/target/symphony/", "/src/main/resources/"); + LOGGER.info("Adjusted resource path for development environment: " + path); // 添加调试信息 } path += "skins"; TEMPLATE_CFG.setDirectoryForTemplateLoading(new File(path)); LOGGER.log(Level.INFO, "Loaded template from directory [" + path + "]"); } catch (final Exception e) { TEMPLATE_CFG.setClassForTemplateLoading(Templates.class, "/skins"); - LOGGER.log(Level.INFO, "Loaded template from classpath"); + LOGGER.error("Failed to load template from directory, loading from classpath", e); // 记录错误信息 } TEMPLATE_CFG.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER); TEMPLATE_CFG.setLogTemplateExceptions(false);