diff --git a/MessageHub/src/main/java/com/xiaowang/messagehub/MessageHubApplication.java b/MessageHub/src/main/java/com/xiaowang/messagehub/MessageHubApplication.java index 7e909ae0..e0ae8853 100644 --- a/MessageHub/src/main/java/com/xiaowang/messagehub/MessageHubApplication.java +++ b/MessageHub/src/main/java/com/xiaowang/messagehub/MessageHubApplication.java @@ -17,18 +17,21 @@ import java.nio.file.Paths; @SpringBootApplication public class MessageHubApplication { + private static final String APP_RUNTIME_DIR = "YumiNail"; + private static final String MODULE_RUNTIME_DIR = "MessageHub"; + public static void main(String[] args) { configureNacosRuntimePaths(); SpringApplication.run(MessageHubApplication.class, args); } private static void configureNacosRuntimePaths() { - configurePathIfAbsent("JM.LOG.PATH", resolveRuntimePath("logs", true)); - configurePathIfAbsent("JM.SNAPSHOT.PATH", resolveRuntimePath("nacos-snapshot", false)); + configurePathIfAbsent("JM.LOG.PATH", "logs", true); + configurePathIfAbsent("JM.SNAPSHOT.PATH", "nacos-snapshot", false); } private static String resolveRuntimePath(String child, boolean includeNacosDir) { - Path path = Paths.get(System.getProperty("user.dir"), child); + Path path = resolveBaseRuntimePath().resolve(child); try { Files.createDirectories(path); if (includeNacosDir) { @@ -40,10 +43,22 @@ public class MessageHubApplication { return path.toAbsolutePath().toString(); } - private static void configurePathIfAbsent(String key, String value) { + private static Path resolveBaseRuntimePath() { + String localAppData = System.getenv("LOCALAPPDATA"); + if (hasText(localAppData)) { + return Paths.get(localAppData, APP_RUNTIME_DIR, MODULE_RUNTIME_DIR); + } + return Paths.get(System.getProperty("user.home"), ".yuminail", MODULE_RUNTIME_DIR.toLowerCase()); + } + + private static void configurePathIfAbsent(String key, String child, boolean includeNacosDir) { String currentValue = System.getProperty(key); - if (currentValue == null || currentValue.trim().isEmpty()) { - System.setProperty(key, value); + if (!hasText(currentValue)) { + System.setProperty(key, resolveRuntimePath(child, includeNacosDir)); } } + + private static boolean hasText(String value) { + return value != null && !value.trim().isEmpty(); + } } diff --git a/docs/changes/2026-03-24-MessageHub运行时目录移出项目.md b/docs/changes/2026-03-24-MessageHub运行时目录移出项目.md new file mode 100644 index 00000000..979f57d5 --- /dev/null +++ b/docs/changes/2026-03-24-MessageHub运行时目录移出项目.md @@ -0,0 +1,30 @@ +# 2026-03-24 MessageHub运行时目录移出项目 + +## 本次处理 + +- 修复 `MessageHub` 启动时将 `logs`、`nacos-snapshot` 直接创建到项目根目录的问题。 +- 保留 `JM.LOG.PATH`、`JM.SNAPSHOT.PATH` 的外部覆盖能力;仅在未显式配置时才自动计算并创建默认目录。 +- 默认运行时目录调整为: + - Windows:`%LOCALAPPDATA%\\YumiNail\\MessageHub\\logs` + - Windows:`%LOCALAPPDATA%\\YumiNail\\MessageHub\\nacos-snapshot` + - 兜底:`${user.home}\\.yuminail\\messagehub\\logs` + - 兜底:`${user.home}\\.yuminail\\messagehub\\nacos-snapshot` + +## 修改文件 + +- `MessageHub/src/main/java/com/xiaowang/messagehub/MessageHubApplication.java` + +## 原因说明 + +- 原实现使用 `System.getProperty("user.dir")` 作为运行时目录根路径,导致每次从项目目录启动时都会在工作区生成运行时文件夹。 +- 原实现即使外部已经传入 `JM.LOG.PATH` 或 `JM.SNAPSHOT.PATH`,也会先执行默认目录创建,仍然会污染项目目录。 + +## 结果 + +- 后续启动 `MessageHub` 时,不会再默认在项目根目录生成 `logs`、`nacos-snapshot`。 +- 如有特殊部署需求,仍可继续通过 JVM/System Property 显式指定运行时路径。 + +## 额外说明 + +- 本轮未改动业务逻辑、数据库、Nacos 配置及其他模块。 +- 本轮无需同步更新整体架构分析文档。