Scala on App Service
Scala is an object-oriented programming language that can be compiled to run on the Java Virtual Machine (JVM). Using the Java runtime allows you to integrate with the enormous Java ecosystem and execute Scala programs anywhere the JVM is available. This includes Azure App Service with the Java SE runtime. The Play Framework is a lightweight web application framework for Java and Scala that integrates all components and APIs needed for modern web application development.
Follow the tutorial below to deploy a Play framework Scala app onto Azure App Service.
Prerequisites
To follow the steps in this tutorial you will need the following tools installed locally. (You can use either the Azure CLI or the Maven plugin. You don’t need both to complete this tutorial.)
- Java 11
- sbt v1.3.4 or greater
- Maven (Or install the Azure CLI)
- Azure CLI (Or use Maven)
To check your sbt version, enter the following in a command window:
sbt sbtVersion
Build and Run the Project
This example Play project was created from a seed template. It includes all Play components and an Akka HTTP server. The project is also configured with filters for Cross-Site Request Forgery (CSRF) protection and security headers.
To build and run the project:
-
First clone the Scala on App Service repository with the following command:
git clone https://github.com/Azure-Samples/scala-on-app-service
-
Change directory into the example project directory:
cd scala-on-app-service
-
Build the project by running
sbt run
. The command builds and starts the embedded HTTP server. Since this downloads libraries and dependencies, the amount of time required depends partly on your internet connection speed. -
After the message
Server started, ...
displays, enter the following URL in a browser: http://localhost:9000. The Play application will respond with:Welcome to the Hello World Scala POC Tutorial!
.
Now that the application is working locally, let’s package the application into an executable .jar file that we can deploy onto Azure App Service.
Assemble and Test JAR Locally
Follow these steps to build a .jar file executable for a Java 11 runtime using sbt assembly.
-
From the project root, run:
sbt assembly
This command produces an executable .jar file in the
scala-on-app-service/target/scala-2.14/
directory. -
To test the app locally, run the previously created .jar file:
java -jar target/scala-2.13/scala-play-example-assembly-1.0.jar
The application should now be running at http://localhost:80. (Note that the port is now 80, as this is the default HTTP port expected on App Service when we deploy it in the next section.)
-
Open the application in your browser to ensure it works locally as an executable .jar.
Something interesting to note about creating an executable .jar using sbt assembly
is that it will inject all necessary Scala dependencies according to the assemblyMergeStrategy
defined in build.sbt
. This allows a native Scala app like this one to be executed in a Java-only environment. This also means that your production environment only needs to be running Java 11 and doesn’t need any Scala runtime dependencies since they’ve all been injected into the .jar file.
Deploy to Azure App Service
You can deploy the .jar using either the Azure CLI or the Maven plugin. Follow the instructions below for your preferred tool.
Deploy as JAR using Azure CLI
To deploy with the Azure CLI, run the following command from the project root:
az webapp deploy --type jar --src-path target/scala-2.13/<project-name>-assembly-<version>.jar --name <app-name> --resource-group <resource-group>
Once complete, you should be able to access your Play Framework app at https://<app-name>.azurewebsites.net
Deploy as JAR using Maven
-
To use Maven, you’ll need a pom file. The last line of
scala-on-app-service/build.sbt
handles maven repo creation for publishing. After assembling your .jar file withsbt assembly
, run the following command from the project root to generate a pom file:sbt publish
-
Copy the newly created pom file to the project root:
cp maven-repo/com/example/scala-play-example_2.13/1.0/scala-play-example_2.13-1.0.pom pom.xml
-
Configure the webapp using the appropriate azure plugin for maven:
mvn com.microsoft.azure:azure-webapp-maven-plugin:2.5.0:config
-
Update the app name and .jar file location in the newly modified pom.xml, for example:
<appName>Scala-App-Name</appName>
<deployment> <resources> <resource> <directory>${project.basedir}/target/scala-2.13</directory> <includes> <include><app-name>-assembly-1.0.jar</include> </includes> </resource> </resources> </deployment>
-
Deploy to App Service with the following command:
mvn azure-webapp:deploy
-
Update the application by running
sbt assembly
followed bymvn azure-webapp:deploy
after making & testing changes locally.