我想在我看來顯示'git describe'的輸出。我是否需要編寫一個更新值並將其設置爲應用程序範圍的插件?還是有更簡單的方法來做到這一點?使用play框架在模板中輸出'git describe'?
4
A
回答
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
如果你不運行從混帳回購協議,你可以不喜歡我你的代碼,我有一個產生戰爭的文件,並在這個劇本我會做一個構建腳本:
cat > {apppath}/conf/application_version.properties << EOF
application.version=`git describe`
application.buildtime=`date`
EOF
...
而且在@OnApplicationStart i類添加屬性
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))
}
}
}
而從控制器
object ApplicationVersion extends Controller {
def version = {
Json("{iamVersion: '"+configuration.getProperty("application.version")+"', buildTime: '"+configuration.getProperty("application.buildtime")+"'}")
}
}
相關問題
- 1. 如何在Play中使用Groovy!框架,模板之外?
- 2. Scala Play框架模板複雜對象
- 3. 在Play框架中引用模板語法系統
- 4. 使用Play框架
- 5. 如何在Play框架中創建列表2 scala模板
- 6. Play框架的模板引擎中是否存在「instanceof」功能?
- 7. 如何在Play中強制使用Logger.debug輸出!框架specs2測試?
- 8. 如何在Play框架中使用SORM框架?
- 9. 使用其他模板的Play模板
- 10. 在Play框架中使用FBConnect模塊獲取URISyntaxException
- 11. 在帶有Websocket的Play框架中使用Try/Catch時出錯
- 12. Play框架模板中的不良語法
- 13. Scala模板與Play框架中找不到的類
- 14. Play 2.0框架模板引擎中的計算
- 15. 在Play框架中的RJS
- 16. Play框架中的模型子包
- 17. Play框架模型中的數組[Byte]
- 18. CORS Play框架
- 19. 如何在Play框架中禁用H2
- 20. 如何擴展Play!框架模板標籤
- 21. 無法從Play框架模板訪問嵌套的對象
- 22. 如何將'git describe'的輸出轉換成Jenkins構建名稱
- 23. 如何在Play框架的模板引擎中迭代Java對象的屬性
- 24. Play框架2.3.2:如何在scala模板中渲染列表或地圖
- 25. renderArgs在模板中爲空!框架
- 26. 更新@OneToMany在Play框架
- 27. 過程輸出中框架
- 28. 使用Play框架EC2連接到RDS
- 29. 如何使用Play框架下載commons.lang?
- 30. 如何使用JasperReports與Play框架