Scala

Spring Boot and Scala with sbt as the build tool

Earlier I had blogged about using Scala with Spring Boot and how the combination just works. There was one issue with the previous approach though – the only way to run the earlier configuration was to build the project into a jar file and run the jar file.
 
 
 
 
 
 
 
 

./gradlew build
java -jar build/libs/spring-boot-scala-web-0.1.0.jar

Spring boot comes with a gradle based plugin which should have allowed the project to run with a “gradle bootRun” command, this unfortunately gives an error for scala based projects.

A good workaround is to use sbt for building and running Spring-boot based projects. The catch though is that with gradle and maven, the versions of the dependencies would have been managed through a parent pom, now these have to be explicitly specified. This is how a sample sbt build file with the dependencies spelled out looks:

name := "spring-boot-scala-web"

version := "1.0"

scalaVersion := "2.10.4"

sbtVersion := "0.13.1"

seq(webSettings : _*)

libraryDependencies ++= Seq(
  "org.springframework.boot" % "spring-boot-starter-web" % "1.0.2.RELEASE",
  "org.springframework.boot" % "spring-boot-starter-data-jpa" % "1.0.2.RELEASE",
  "org.webjars" % "bootstrap" % "3.1.1",
  "org.webjars" % "jquery" % "2.1.0-2",
  "org.thymeleaf" % "thymeleaf-spring4" % "2.1.2.RELEASE",
  "org.hibernate" % "hibernate-validator" % "5.0.2.Final",
  "nz.net.ultraq.thymeleaf" % "thymeleaf-layout-dialect" % "1.2.1",
  "org.hsqldb" % "hsqldb" % "2.3.1",
  "org.springframework.boot" % "spring-boot-starter-tomcat" % "1.0.2.RELEASE" % "provided",
  "javax.servlet" % "javax.servlet-api" % "3.0.1" % "provided"
)


libraryDependencies ++= Seq(
  "org.apache.tomcat.embed" % "tomcat-embed-core"         % "7.0.53" % "container",
  "org.apache.tomcat.embed" % "tomcat-embed-logging-juli" % "7.0.53" % "container",
  "org.apache.tomcat.embed" % "tomcat-embed-jasper"       % "7.0.53" % "container"
)

Here I am also using xsbt-web-plugin which is plugin for building scala web applications.

xsbt-web-plugin also comes with commands to start-up tomcat or jetty based containers and run the applications within these containers, however I had difficulty in getting these to work.

What worked is the runMain command to start up the Spring-boot main program through sbt:

runMain mvctest.SampleWebApplication

and xsbt-web-plugin allows the project to be packaged as a war file using the “package” command, this war deploys and runs without any issues in a standalone tomcat container.

Here is a github project with these changes: https://github.com/bijukunjummen/spring-boot-scala-web.git

 

Reference: Spring Boot and Scala with sbt as the build tool from our JCG partner Biju Kunjummen at the all and sundry blog.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button