我想在我的 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/