How to access SBT settings from Java code in PlayFramework?

孤人 提交于 2019-12-23 12:23:59

问题


I have a PlayFramework application, and there is a name and version in the build.sbt.

If I run the play console, I can access this information by typing name or version.

How can I get this information from Java code inside the application? I can't seem to find any useful methods in Play.application() and I have not been able to find this in the documentation.


回答1:


For this you can use the SBT plugin BuildInfo:

Add to project/plugins.sbt:

addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.2.5")

And to build.sbt:

buildInfoSettings

sourceGenerators in Compile <+= buildInfo

buildInfoKeys := Seq[BuildInfoKey](name, version, scalaVersion, sbtVersion)

buildInfoPackage := "hello"

Now you can access the build information with static methods:

package controllers;

import play.*;
import play.mvc.*;

import views.html.*;

public class Application extends Controller {

    public static Result index() {
        return ok(hello.BuildInfo.name());//the name of the application as output
    }

}


来源:https://stackoverflow.com/questions/19776828/how-to-access-sbt-settings-from-java-code-in-playframework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!