Building and Deploying Scala Apps
If you've only ever tooled around with Scala in an IDE, here's a quick and easy way to see your app out in the Linux/Mac/Windows worlds.
Join the DZone community and get the full member experience.
Join For Freehey! looks like you're familiar with scala. and i believe you've even developed some apps locally using it. one of coding's main missions is to help people to solve their problems. but how can your apps do this if they are running locally on your laptop? in this post, i want to show the easiest way to deploy a scala application.
how to build a scala project
what do i mean by saying “build” a scala project? well, it means that the project, running locally from the ide, should be “transformed” into something that can be launched from any other environment (linux / windows / mac).
let’s consider a sample scala project. it should be something really simple, so i decided to create a trivial akka-http app. here is its structure:
i used green arrows in order to highlight the files that are important in the context of this article. by walking through the files, we can see what they contain and their purpose. by doing this, i want to work from a particular example to more general one.
almost all scala projects start from a build.sbt file. as a rule, it contains meta information about the project: name, version, dependencies…
name := "sbt-build-example"
version := "1.0"
scalaversion := "2.12.2"
librarydependencies ++= seq(
"com.typesafe.akka" %% "akka-http" % "10.0.9"
)
take a look at the dependencies in the code above. to be more precise, look at the single dependency. for sure, your project contains more than one. but for this demonstration, it’s enough.
then we have a
main
class where all “business logic” happens:
package com.example
import akka.actor.actorsystem
import akka.http.scaladsl.http
import akka.http.scaladsl.http.serverbinding
import akka.http.scaladsl.server.directives._
import akka.stream.actormaterializer
import scala.concurrent.future
object main extends app {
implicit val system = actorsystem()
implicit val ec = system.dispatcher
implicit val materializer = actormaterializer()
val routes = pathprefix("ping") {
get {
complete("pong")
}
}
val bindingfuture: future[serverbinding] = http().bindandhandle(routes, "127.0.0.1", 5000)
}
so the demo app has only one endpoint, which returns “pong” when you make a get request to 127.0.0.1/ping
you can check it if you want.
so, how to build this app? how to make it deployable? in order to perform this, we need to add the sbt-native-packager plugin to the project. you can do it by adding a single line of code to the plugins.sbt file.
addsbtplugin("com.typesafe.sbt" % "sbt-native-packager" % "1.2.0")
then you need to add the following line of code to the build.sbt file as well:
enableplugins(javaapppackaging)
voila!
open terminal/your console, navigate to the root folder of the project, and execute the following command:
sbt stage
this command generates a new folder in your project directory: target/universal/stage/
there are two folders in the stage directory: bin and lib . the first one contains launchers (linux/mac/windows). the second one contains all dependencies and a jar with the application classes.
what’s next? as you may guess, the content of the stage directory can be moved to your server and launched there by executing the following command in the console:
./bin/app-name
notice that this command must be run from the stage folder. by the way, app-name may vary. it depends on the project name (package name) that you specified in the build.sbt file.
summary
as you can see, sbt-native-packager gives a pretty straightforward way to build a scala project. what is really cool is that it provides more packaging options for scala apps. in this article, i described only about 5% of the functionality that this plugin can perform. therefore, i’m going to write a couple of new tutorials about sbt-native-packager .
Published at DZone with permission of Alexey Zvolinskiy, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments