scala reference sbt project version - scala

I have a scala play application; I am trying to expose health check for our service. Part of the health check I would like to capture the project artifact version.
Can I know how I can reference project artifact version from health check controller in play application. We are making use of sbt for the build.
sample sbt file
import root.sbt.Keys._
import com.typesafe.sbt.SbtNativePackager._
import NativePackagerKeys._
import play.PlayScala
name := "artifact-name"
version := "0.5"
scalaVersion := "2.11.1"
javacOptions ++= Seq("-source", "1.7", "-target", "1.7")
scalacOptions += "-target:jvm-1.7"
lazy val root = (project in file(".")).enablePlugins(PlayScala)

The xsbt-reflect plugin gives you access to the SBT variables at runtime.
All you need to do inside of your health route is return
Reflect.version

Related

How to add Google ML Kit dependency to SBT project?

When adding the Google ML Kit dependency to our SBT project, it successfully compiles, yet it cannot resolve imports to the com.google.mlkit package.
Here is a sample build.sbt file, using barcode-scanning as an example:
ThisBuild / version := "0.1.0-SNAPSHOT"
ThisBuild / scalaVersion := "2.13.8"
lazy val root = (project in file("."))
.settings(
name := "test-google-mlkit"
)
resolvers += "Google Maven" at "https://maven.google.com/"
libraryDependencies += "com.google.mlkit" % "barcode-scanning" % "17.0.2" // % "runtime"
This successfully compiles, but we cannot resolve an import to com.google.mlkit, with or without the "runtime" flag (as indicated on the maven repo).
object mlkit is not a member of package com.google
Is it possible to use Google's ML Kit in a Scala project? What are we missing? Any help is appreciated.
To solve the import problem just change the resolvers to:
resolvers += "google repository" at "https://dl.google.com/android/maven2"

Unable to stub controller components

The method stubControllerComponents in package play.api.test appears to use same package and object name as a separate dependency which is causing a conflict when I attempt to use stubControllerComponents :
play.api.test.Helpers.stubControllerComponents is not found in below code:
import java.io.File
import play.api.test
import play.api.mvc._
import javax.inject._
import play.api.Environment
import play.api.mvc.{AbstractController, ControllerComponents}
class CountController #Inject() (cc: ControllerComponents,
env: Environment) extends AbstractController(cc) {
def getter() = Option(env.classLoader.getResourceAsStream("file.csv"))
}
play.api.Environment(play.api.test.Helpers.stubControllerComponents, Environment.simple())
This Helpers contains the method I require stubControllerComponents :
But this version of the class is being imported with import play.api.test :
Play link for stubbing : https://www.playframework.com/documentation/2.6.x/Highlights26#StubControllerComponents
build.sbt:
name := "ddd"
version := "1.0"
lazy val `ddd` = (project in file(".")).enablePlugins(PlayScala)
resolvers += "scalaz-bintray" at "https://dl.bintray.com/scalaz/releases"
resolvers += "Akka Snapshot Repository" at "https://repo.akka.io/snapshots/"
scalaVersion := "2.12.2"
libraryDependencies ++= Seq( jdbc , ehcache , ws , guice , specs2 % Test)
unmanagedResourceDirectories in Test <+= baseDirectory ( _ /"target/web/public/test" )
Do I need to exclude portions of a dependency, in this case filters-helpers in order to make stubControllerComponents available ?
Update:
play.api.test.Helpers.stubControllerComponents not found:
Update2:
You seem to be using a scratch file. AFAICS, there is no way to also include dependencies from your Test scope into the classpath of your worksheet.
A workaround would be to (temporarily) add the play-test artefact to your libraryDependencies. Or just create a proper test file, which has access to the Test libraries normally.
play.api.test.Helpers.stubControllerComponents is provided by play-test
dependency
libraryDependencies += "com.typesafe.play" %% "play-test" % PlayVersion.current % "test",
which is indirectly imported by Play's sbt plugin specified at project/plugins.sbt
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.8.1")
after explicitly enablePlugins(PlayScala) within project's build.sbt.
Note how play-test is out-of-the-box scoped to test configuration hence it is provided only on the test classpath. If you wish to reference stubControllerComponents from within IntelliJ Scala Worksheet, then make sure to create the worksheet inside test/ directory and not inside app/ directory. This will make Scala Worksheet use test classpath.

Play framework's sbt import maven module error

I am using Playframework 2.5.4 to serve as REST API endpoint in default sbt build, compile & run with activator.
This play module imports a core module in local maven (3.9), where types were defined. Such as:
package com.myCompany
case class User(
id: Option[UUID] = None,
username: String
)
Then play controller package will import com.myCompany.User.
Issue is, when activator compiles, it gives unknown parameter name: username
If I placed case class in play's models package, it compiles without error.
Below is my build.sbt
name := """api"""
version := "1.0-SNAPSHOT"
lazy val root = (project in file(".")).enablePlugins(PlayScala)
scalaVersion := "2.11.8"
libraryDependencies ++= Seq(
jdbc,
cache,
ws,
filters,
"com.myCompany" % "core" % "1.0-SNAPSHOT"
)
routesGenerator := InjectedRoutesGenerator
resolvers ++= Seq(
"scalaz-bintray" at "http://dl.bintray.com/scalaz/releases",
Resolver.mavenLocal
// "Local Maven Repository" at "file:///home/pocheng/.m2/repository"
)
I suspect build.sbt file is having issue. Would appreciate any pointer. Thanks.

How to run playframework sample in Intelllij 13?

I am using Intellij 13 Ultimate and want to create a Play Framework sample. But I am unable to build this project because it always throws this error:
object index is not a member of package views.html
Ok(views.html.index("Your new application is ready."))
I have tried this on both Mac and Windows platforms, but the error is always the same.
How can I solve this?
The generation works just fine and all the paths are correctly added to the build. However the routes are not (yet) compiled by the plugins (scala+playframework), thus the reverse routing classes generated by play are not available for intellij.
You will have the same problem with the templates.
If you run a play compile (or a sbt compile), the classes will be generated and intellij should be able to pick them up and compile your project.
I found a trick here.
If you generate the play-scala project using activator command line activator [new my_first_project play-scala]. You'll get the following sbt build file.
name := """my_first_project"""
version := "1.0-SNAPSHOT"
lazy val root = (project in file(".")).enablePlugins(PlayScala)
scalaVersion := "2.11.7"
libraryDependencies ++= Seq(
jdbc,
cache,
ws,
"org.scalatestplus.play" %% "scalatestplus-play" % "1.5.1" % Test
)
resolvers += "scalaz-bintray" at "http://dl.bintray.com/scalaz/releases"
But IF you create a project from intellj using NewProject->Scala-Play 2.x, you will get the following sbt.
name := "my_second_project"
version := "1.0"
lazy val `my_second_project` = (project in file(".")).enablePlugins(PlayScala)
scalaVersion := "2.11.7"
libraryDependencies ++= Seq( jdbc , cache , ws , specs2 % Test )
unmanagedResourceDirectories in Test <+= baseDirectory ( _ /"target/web/public/test" )
resolvers += "scalaz-bintray" at "https://dl.bintray.com/scalaz/releases"
fork in run := false
after combine them. Ignore the name. And I set for in run to false
name := "my_second_project"
version := "1.0"
lazy val `my_second_project` = (project in file(".")).enablePlugins(PlayScala)
scalaVersion := "2.11.7"
libraryDependencies ++= Seq( jdbc , cache , ws , specs2 % Test )
unmanagedResourceDirectories in Test <+= baseDirectory ( _ /"target/web/public/test" )
resolvers += "scalaz-bintray" at "https://dl.bintray.com/scalaz/releases"
fork in run := false
After doing that. Open the activator-generated project with Intellj, configure a run with Play 2.x run. Everything goes well.
By the way, if you open the activator-generated project before you change the sbt file. You might need to rm -r .idea
Hope that helps.
Running "play idea" in the new project root fixed it for me. The project should compile and run after reloading.
I only have this problem with projects created through Idea's New Project menu.
I have the same problem: in Idea 13 there are highlight errors there, but in Idea 12 everything is ok. The reason is that there is no Scala plugin for Idea 13, so for now it's impossible to get it to work in Idea 13.
The solution is to install Idea 12, go to Preferences -> Plugins, type "Scala" in find box, and install Scala plugin.

scala 2.10Mx with playframework 2

How to use scala 2.10Mx with play 2.x.x?
I tried adding scalaVersion := "2.10.0-M3" to project/Build.scala but had no effect.
Here's my project/Build.scala:
import sbt._
import Keys._
import PlayProject._
object ApplicationBuild extends Build {
val appName = "dashboard-server"
val appVersion = "1.0-SNAPSHOT"
resolvers += "Local Ivy Repository" at "file://"+Path.userHome.absolutePath+"/.ivy2/cache"
scalaVersion := "2.10.0-M3"
val appDependencies = Seq(
"mysql" % "mysql-connector-java" % "5.1.10"
)
val main = PlayProject(appName, appVersion, appDependencies, mainLang = SCALA).settings(
// Add your own project settings here
)
}
I'm using sbt 0.11.3
There is a ticket in the play bugtracker: https://play.lighthouseapp.com/projects/82401/tickets/650-support-for-scala-210-m6
The answer is you currently cannot use play 2 with scala 2.10 because of akka.
play 2.0.x doesn't work with Scala 2.10.
the Play 2.1 branch does, but as of the time i write this (25 Oct 2012), you need to build the development branch from source, and the development branches are still under active development. TLDR: not yet suitable for production apps, give it a couple months
Well, actually you can do it. I described details in my answer here.
But in few words you need to specify few things in Build.scala.
Switching version by setting scalaVersion to 2.10.1 didn't help me because SBT still goes to repository and gets pieces of 2.10.0. So I told SBT to use local copy of Scala by setting following variables
scalaVersion := "2.10.1-local",
autoScalaLibrary := false,
scalaHome := Some(file("/Program Files (x86)/scala/"))