tsurumure 1 сар өмнө
parent
commit
4d6e05ab9b

+ 1 - 0
Dockerfile

@@ -2,3 +2,4 @@ FROM openjdk:17-oracle
 
 RUN mkdir -p /app/build && chmod +x /app/build
 ADD ./target/backendsys.jar /app/build/backendsys.jar
+ADD ./version.txt /app/build/version.txt

+ 12 - 6
src/main/java/com/backendsys/modules/PublicController.java

@@ -27,14 +27,20 @@ public class PublicController {
     @GetMapping("/api/version")
     public String getVersion() {
 
-        Integer level = 1;
         String activeProfile = env.getActiveProfiles()[0];
-        if ("local".equals(activeProfile)) level = 2;
+        if ("local".equals(activeProfile)) {
+            // jar 包所在目录 + 文件名
+            String path = FileUtil.getParent(
+                    this.getClass().getProtectionDomain().getCodeSource().getLocation().getFile(), 2) + "./version.txt";
+            return FileUtil.readUtf8String(path).trim();
+        } else {
+            // 自动部署的目录
+            String versionFile = "/app/build/version.txt"; // 上一级目录
+            return FileUtil.exist(versionFile)
+                    ? FileUtil.readUtf8String(versionFile).trim()
+                    : "0.0.0";
+        }
 
-        // jar 包所在目录 + 文件名
-        String path = FileUtil.getParent(
-                this.getClass().getProtectionDomain().getCodeSource().getLocation().getFile(), level) + "./version.txt";
-        return FileUtil.readUtf8String(path).trim();
     }
 
 }