需求是可以查看当前web应用对应的是哪一个源码版本,何时编译的,以肯定某些错误是由于没有部署新版本致使的,仍是存在未解决bug。前端
对sbt和scala不熟,因此这个方案可能很笨拙,但能解决问题。基本思路是在编译以前,把构建时间和GIT版本写入一个文本文件中,运行时由后台java代码读取,发送给前端。java
sbt文件能够是.sbt后缀,也能够是.scala后缀,若是是.scala后缀,则限制不多,能够随意作处理,但通常使用的是.sbt后缀文件。.sbt文件中只能写函数,因此把构建信息写入文件的操做都放在函数中。git
在.sbt文件中添加如下内容:web
import java.text.SimpleDateFormat import java.util.Date ... //函数NowDate取当前时间也就是构建时间 def NowDate(): String = { val now: Date = new Date() val dateFormat: SimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") val date = dateFormat.format(now) return date } //取GIT的header并写入public文件夹下的文本文件,同时也把构建时间写入另外一个文本文件 def createGitHeaderFile(dir:String):sbt.File={ val propFile = sourceDir("/public/data/gitv.txt") try{ val h =Process("git rev-parse HEAD").lines.head IO.write(propFile, h) }catch{ case ex:Exception => { ex.printStackTrace() IO.write(propFile, "ERROR_GET_GIT_HEADER_FAIL") } } val btFile = sourceDir("/public/data/buildtime.txt") val nowText = NowDate() IO.write(btFile, nowText) return file(s"${file(".").getAbsolutePath}/$dir") } //这样写其实只是为了把git版本header写入文件public/data/gitv.txt //这里指定额外的源文件路径,能够调用函数,因此利用这个机会调用上面定义的函数,把构建信息写入 unmanagedSourceDirectories in Compile ++= Seq( createGitHeaderFile("/scala") )
在web项目中的java文件读取构建信息,而后能够进行处理或者发送给前端函数
istr = env.resourceAsStream("public/data/gitv.txt"); if (istr.nonEmpty()){ InputStream i=istr.get(); BufferedReader reader = new BufferedReader(new InputStreamReader(i)); try { String s = reader.readLine(); if (s!=null){ gitHeader=s; } } catch (IOException e) { e.printStackTrace(); System.out.println("cannot read public/data/gitv.txt"); } } istr = env.resourceAsStream("public/data/buildtime.txt"); if (istr.nonEmpty()){ InputStream i=istr.get(); BufferedReader reader = new BufferedReader(new InputStreamReader(i)); try { String s = reader.readLine(); if (s!=null){ this.buildTime=s; } } catch (IOException e) { e.printStackTrace(); System.out.println("cannot read public/data/buildtime.txt"); } }