简体   繁体   中英

Play! Framework - build.sbt with Build.scala

I'm trying to separate various settings from the build definition in my Play! 2.1 application.

I defined some settings in build.sbt as follows:

name := "My Project"
version := 1.0

How can I reuse these values in Build.scala?

object ApplicationBuild extends Build {

  val main = 
    // Doesn't compile since name and version are SettingKeys, not Strings
    play.Project(name, version).settings(
      // ...
    )

}

Thanks a lot!

You can use <<= instead of := if you need to access keys (like <+= instead of += ). In this case we want to pull the version and name from the global scope.

val main = play.Project(appName, appVersion, appDependencies).settings(
  version <<= (version in Global)  { v => v} ,
  name <<= (name in Global) { n => n } 
)

Although since this is the default scope we can omit the scope in this case.

val main = play.Project(appName, appVersion, appDependencies).settings(
  version <<= (version)  { v => v} ,
  name <<= (name) { n => n } 
)

An even shorter version of this is simply

val main = play.Project(appName, appVersion, appDependencies).settings(
  version <<= version ,
  name <<= name
)

Note your build.sbt file must go in the root directorying and not the project/ directory.

The Build.scala cannot access the value that you define in build.sbt, but it works the other way around. Whatever you are trying to do here is practically just trying to hack SBT 0.12.

What I would recommend is for you to define the shared settings in a .scala file, ie. Settings.scala. And then you can refer to these shared settings from both Build.scala and build.sbt.

Settings.scala

object Settings {
  appName:= "My Project"
  appVersion:= 1.0
}

Build.scala

object ApplicationBuild extends Build {
  val main = play.Project(appName, appVersion, appDependencies)...
}

build.sbt

name := Settings.appName
version := Settings.appVersion

Of course the story will be different if you use Play 2.2 with SBT 0.13. :)

Let me know if this makes sense and feel cleaner for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM