Scalatra Scala Service giter8 Template

Scala Dec 31, 2018

Lets stand up a new Scala service from scratch. For this walk-through, I used Ubuntu 18.10.

Basic requirements:

  • JDK8 sudo apt install openjdk-8-jdk Using version "1.8.0_191"
  • Scala Build Tool https://www.scala-sbt.org/ (I chose to download and manually add to my path - but the SBT docs recommend getting it via apt). Using version 1.2.1

What we will get from this guide:

A basic Scalatra service that renders a basic html page using Twirl for server-side templating (Twirl is from Play 2) – http://scalatra.org/guides/2.6/views/twirl.html It wont have much out of the gate other than the ability to define a route, render some html, and do logging (its default test verifies we can get a 200)

Create a Scalatra project using giter8 Template

Following the tutorial - http://scalatra.org/getting-started/first-project.html

organization [com.example]: org.bitbucket.honstain
name [My Scalatra Web App]: ToyInventoryApp
version [0.1.0-SNAPSHOT]: 
servlet_name [MyScalatraServlet]: ToyInventory
package [com.example.app]: org.bitbucket.honstain.app
scala_version [2.12.6]: 
sbt_version [1.2.1]: 
scalatra_version [2.6.4]: 

Template applied in /home/dev/Desktop/scalatra-blog/./toyinventoryapp

Sanity check  that we can build:

[info] Done updating.
[warn] There may be incompatibilities among your library dependencies.
[warn] Run 'evicted' to see detailed eviction warnings
[info] Loading settings for project toyinventoryapp from build.sbt ...
[info] Set current project to ToyInventoryApp (in build file:/home/dev/Desktop/scalatra-blog/toyinventoryapp/)
[info] sbt server started at local:///home/dev/.sbt/1.0/server/a4b28a03b2c11993f519/sock

I want to use IntelliJ (so I will demonstrate that, but other options are reasonable), so lets load that now:

It can take a bit for IntelliJ to finish indexing. When you're done you should hopefully see the basic project:

As described in the Scalatra guide, you can now start the app, I showed the IntelliJ example, but you should also try using SBT via the command line.

# Basic start
jetty:start

# Run the application with hot reload
~;jetty:stop;jetty:start

At this stage, we should hopefully have a basic Scalatra app running.

I have shared the public repo with this project on bitbucket https://bitbucket.org/honstain/scalatra-example-blog/commits/9fe71828eee4e5942b8f48a8add691a26dfd9333

Tags