我想在我的视图中显示'git describe'的输出。我是否需要编写一个更新值并将其设置为应用程序范围的插件?或者有更简单的方法吗?
答案 0 :(得分:3)
我刚读过有关游戏模块的内容,并决定写一个(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
在我的控制器中:
@Before
static void addDefaults() {
renderArgs.put("version", Play.configuration.getProperty("git.revision"));
}
当然这不是很便携,可以改进。可能的改进是允许通过配置文件中的设置运行自定义命令。
答案 1 :(得分:1)
如果你没有从git repo运行代码,你可以像我一样,我有一个生成war文件的构建脚本,在这个脚本中我会这样做:
cat > {apppath}/conf/application_version.properties << EOF
application.version=`git describe`
application.buildtime=`date`
EOF
...
在@OnApplicationStart类中添加属性
private def readApplicationVersion() {
Logger.info("Bootstrap.readApplicationVersion file")
Play.id match {
case "" | "test" => Play.configuration.put("application.version", "TEST-MODE"); Play.configuration.put("application.buildtime", "YEAH BABY YEAH REALTIME")
case _ => addFileProp(VirtualFile.open(Play.applicationPath).child("conf/application_version.properties").inputstream())
}
}
private def addFileProp(input: InputStream) {
input match {
case null => Logger.error("can't find config file, Play id: " + Play.id + ". Will exit now.")
case _ => val extendCconfiguration = IO.readUtf8Properties(input);
for (key <- extendCconfiguration.keys) {
Play.configuration.put(key, extendCconfiguration.get(key))
}
}
}
来自Controller
object ApplicationVersion extends Controller {
def version = {
Json("{iamVersion: '"+configuration.getProperty("application.version")+"', buildTime: '"+configuration.getProperty("application.buildtime")+"'}")
}
}