diff --git a/build.gradle b/build.gradle index 900e9f1d..6907a252 100644 --- a/build.gradle +++ b/build.gradle @@ -1,3 +1,5 @@ +import org.apache.tools.ant.filters.ReplaceTokens + plugins { id 'java' id 'org.springframework.boot' version '3.1.5' @@ -6,6 +8,7 @@ plugins { id 'org.springdoc.openapi-gradle-plugin' version '1.8.0' id 'org.hidetake.swagger.generator' version '2.19.2' // id 'org.graalvm.buildtools.native' version '0.9.28' + id 'org.ajoberstar.grgit' version '5.2.1' } group 'plus.maa' @@ -126,4 +129,20 @@ rootProject.afterEvaluate(){ forkedSpringBootRun.configure { doNotTrackState("See https://github.com/springdoc/springdoc-openapi-gradle-plugin/issues/102") } -} \ No newline at end of file +} + +// 配置资源文件拷贝任务 +processResources { + // 获取当前 commit + def commit = grgit?.head() + // 替换配置文件中的占位符 + filesMatching(['**/*.properties', '**/*.yml', '**/*.yaml']) { file -> + { + file.filter(ReplaceTokens, tokens: [ + // commit 不存在时,填充空字符串 + 'commitAbbreviatedId': commit?.abbreviatedId ?: '', + 'commitDateTime' : commit?.dateTime?.toString() ?: '' + ]) + } + } +} diff --git a/src/main/java/plus/maa/backend/config/external/Commit.java b/src/main/java/plus/maa/backend/config/external/Commit.java new file mode 100644 index 00000000..7f0a24a0 --- /dev/null +++ b/src/main/java/plus/maa/backend/config/external/Commit.java @@ -0,0 +1,11 @@ +package plus.maa.backend.config.external; + +import lombok.Data; + +import java.time.ZonedDateTime; + +@Data +public class Commit { + private String abbreviatedId; + private ZonedDateTime dateTime; +} diff --git a/src/main/java/plus/maa/backend/config/external/Info.java b/src/main/java/plus/maa/backend/config/external/Info.java index eca8e46e..92b20132 100644 --- a/src/main/java/plus/maa/backend/config/external/Info.java +++ b/src/main/java/plus/maa/backend/config/external/Info.java @@ -1,5 +1,7 @@ package plus.maa.backend.config.external; +import org.springframework.boot.context.properties.NestedConfigurationProperty; + @lombok.Data public class Info { private String title; @@ -7,4 +9,6 @@ public class Info { private String version; private String domain; private String frontendDomain; + @NestedConfigurationProperty + private Commit commit; } \ No newline at end of file diff --git a/src/main/java/plus/maa/backend/controller/SystemController.java b/src/main/java/plus/maa/backend/controller/SystemController.java index 65827d92..d4243dc8 100644 --- a/src/main/java/plus/maa/backend/controller/SystemController.java +++ b/src/main/java/plus/maa/backend/controller/SystemController.java @@ -38,6 +38,7 @@ public MaaResult getSystemVersion() { systemInfo.setTitle(info.getTitle()); systemInfo.setDescription(info.getDescription()); systemInfo.setVersion(info.getVersion()); + systemInfo.setCommit(info.getCommit()); return MaaResult.success(systemInfo); } diff --git a/src/main/java/plus/maa/backend/controller/response/MaaSystemInfo.java b/src/main/java/plus/maa/backend/controller/response/MaaSystemInfo.java index 8f152138..c3496ba8 100644 --- a/src/main/java/plus/maa/backend/controller/response/MaaSystemInfo.java +++ b/src/main/java/plus/maa/backend/controller/response/MaaSystemInfo.java @@ -1,6 +1,7 @@ package plus.maa.backend.controller.response; import lombok.Data; +import plus.maa.backend.config.external.Commit; /** * @author AnselYuki @@ -10,4 +11,5 @@ public class MaaSystemInfo { private String title; private String description; private String version; + private Commit commit; } diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 4ced4110..466d3e7d 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -40,4 +40,11 @@ logging: logback: rollingpolicy: max-history: 14 - clean-history-on-start: true \ No newline at end of file + clean-history-on-start: true + +maa-copilot: + info: + # 这几个参数应在编译时生成,不宜修改 + commit: + abbreviated-id: @commitAbbreviatedId@ + date-time: @commitDateTime@ \ No newline at end of file