git - 使用 Play Framework 将 'git describe' 的输出放入模板中?

标签 git playframework

我想在我的 View 中显示“git describe”的输出。我是否需要编写一个插件来更新一个值并在应用程序范围内设置它?或者有更简单的方法吗?

最佳答案

我刚刚阅读了有关 Play 模块的内容,并决定编写一个 (https://github.com/killdashnine/play-git-plugin) 以查看是否可以解决我的问题:

import java.io.BufferedReader;
import java.io.InputStreamReader;

import play.Logger;
import play.Play;
import play.PlayPlugin;

public class GitPlugin extends PlayPlugin {

        private static String GIT_PLUGIN_PREFIX = "GIT plugin: ";

        @Override
        public void onApplicationStart()  {
                Logger.info(GIT_PLUGIN_PREFIX + "executing 'git describe'");
                final StringBuffer gitVersion = new StringBuffer();
                try {
                        final Process p = Runtime.getRuntime().exec("git describe"); 
                        final BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream())); 

                        // wait for process to complete
                        p.waitFor(); 

                        // read the output
                        String line = reader.readLine(); 
                        while(line != null) { 
                                gitVersion.append(line); 
                                line = reader.readLine(); 
                        } 
                }
                catch(Exception e) {
                        Logger.error(GIT_PLUGIN_PREFIX + "unable to execute 'git describe'");
                }

                // set a property for this value
                Play.configuration.setProperty("git.revision", gitVersion.toString());

                Logger.info(GIT_PLUGIN_PREFIX + "revision is " + gitVersion.toString());
        }
}

结果是:

12:14:46,508 INFO  ~ GIT plugin: executing 'git describe'
12:14:46,513 INFO  ~ GIT plugin: revision is V0-beta-7-gac9af80

在我的 Controller 中:

    @Before
    static void addDefaults() {
        renderArgs.put("version", Play.configuration.getProperty("git.revision"));
     }

当然这不是很便携,可以改进。可能的改进是允许通过配置文件中的设置运行自定义命令。

关于git - 使用 Play Framework 将 'git describe' 的输出放入模板中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5337880/

相关文章:

java - 如何在我的 PHP 和 Java/Play Framework 应用程序之间共享一个 Apache 实例?

ssl - play framework 2.3.7 - SSL配置

git - 为什么 git 允许远程标签移动,或者为什么你不能使用 git-tag 进行原子测试和设置

git - 从 Git : "One of setGitDir or setWorkTree must be called" checkout 时出现 Hudson 错误

java - 在play framework java中检索POST请求中发送的请求体字符串

java - amchart - 使用相同 X 轴的多个 Y 轴

scala - 为方法 Apply 提供太多参数

git - 通过非标准 SSH 端口使用 Git

linux - 通过publickey ssh登录失败

git - 本地存储库需要 Git LFS 吗?