Putting output of 'git describe' in template using play framework?
I'd like to show th开发者_如何转开发e output of 'git describe' in my view. Do I need to write a plug-in that updates a value and sets it application wide? Or is there an easier way to do this?
I've just read about play modules and decided to write one (https://github.com/killdashnine/play-git-plugin) to see if I could solve my problem:
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());
}
}
Which results in:
12:14:46,508 INFO ~ GIT plugin: executing 'git describe'
12:14:46,513 INFO ~ GIT plugin: revision is V0-beta-7-gac9af80
In my controller:
@Before
static void addDefaults() {
renderArgs.put("version", Play.configuration.getProperty("git.revision"));
}
Of course this is not very portable and could be improved. Possible improvement would be to allow a custom command to be run via a settings in your configuration file.
If you don't run your code from a git repo you could do like me, i have a build script that produces a war file and in this script i'll do:
cat > {apppath}/conf/application_version.properties << EOF
application.version=`git describe`
application.buildtime=`date`
EOF
...
And in @OnApplicationStart class i add the properties
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))
}
}
}
And from the Controller
object ApplicationVersion extends Controller {
def version = {
Json("{iamVersion: '"+configuration.getProperty("application.version")+"', buildTime: '"+configuration.getProperty("application.buildtime")+"'}")
}
}
精彩评论