SBT including the version number in a program

前端 未结 4 1152
春和景丽
春和景丽 2020-12-13 19:36

I want a program I\'m building to be able to report its own version at runtime (e.g. scala myprog.jar --version). Traditionally in a maven project, I\'d use res

4条回答
  •  庸人自扰
    2020-12-13 20:26

    I ended up making the build system (I use a Makefile on top of sbt) prepare a src/main/resources/version.txt file for Scala to read.

    In the Makefile:

    $(RESOURCES_VERSION): build.sbt
        grep "^version := " $< | cut -f2 -d\" > $@
    

    In Scala:

    val version: String = {
      val src = Source.fromURL( getClass.getResource("/version.txt") )
      src.getLines.next   // just take the first line 
    }
    

    This works for me.

    It's curious that such a needed feature (I would think) is not easily available in Scala. A very simple sbt plugin just for this would be welcome.

提交回复
热议问题