Scala programming language, which also operates on the JVM, can play nicely with many of the operational and monitoring tools as well as with Java and other languages. Scala shares many simple and readable syntax features of other programming languages. It has various productivity advantages and allows developers to work more effectively. Developers often characterize Scala as a more functional language than Java. Actually it's a mix of object-oriented and functional programming. Such languages like Scala often attract flexible, highly skilled and innovative developers, which form great technical teams. So, let's see how this highly flexible language works in the cloud!
Create environment
1. Log in into Jelastic and click on the Create environment button.
2. Create your environment in Jelastic e.g. with Tomcat 7 and Java 7.
Create application
1. First of all download and install sbt for your OS. It is s a build tool for Scala and Java projects.
2. Now let's create a simple Scala servlet application for testing. Helloworld.scala (src/main/scala/helloworld.scala) file will look like:
import javax.servlet.http._
class HelloWorldServlet extends HttpServlet {
override def doGet(req: HttpServletRequest, resp: HttpServletResponse) = {
resp.getWriter().print("Hello World!")
}
}
3. In the src/main/webapp directory create web.xml file for your project:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
<web-app>
<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>HelloWorldServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Build and deploy project
Xsbt-web-plugin
1. Add special plugin for WAR file building, xsbt-web-plugin, to project/plugins.sbt:
addSbtPlugin("com.earldouglas" % "xsbt-web-plugin" % "0.3.0")
2. Create build.sbt file in the base directory of your project. Specify the name of your app, its version, version of Scala and inject the plugin settings:
name := "helloworld"
version := "1.0"
scalaVersion := "2.9.2"
seq(webSettings :_*)
libraryDependencies ++= Seq(
"javax.servlet" % "servlet-api" % "2.5",
"org.mortbay.jetty" % "jetty" % "8.1.11" % "container"
)
3. Create a file build.properties in the base directory and specify sbt version in there in order to avoid any potential confusion:
sbt.version=0.12.4
4. Run the next command from inside of your base directory in order to build a WAR file:
>compile:package-war
The WAR archive you have just generated will be stored in the target/scala-x.x.x directory.
5. Go back to the Jelastic dashboard and upload your WAR package to the Deployment Manager.
6. Deploy your Scala application to the environment you've just created.
Sbt-jelastic-deploy plugin
1. Add special plugin for Jelastic to your project for further its deployment. Sbt-jelastic-deploy plugin depends on xsbt-web-plugin and deploys WAR archive, generated by xsbt-web-plugin to the Jelastic environment. To enable sbt-jelastic-deploy specify the appropriate setting in your plugins.sbt (project/plugins.sbt) file:
addSbtPlugin("com.github.casualjim" % "sbt-jelastic-deploy" % "0.1.2")
2. Navigate to your project base directory and create build.sbt file. Specify the name of your app, its version, version of Scala and specify the keys for Jelastic plugin.
name := "helloworld"
version := "1.0"
scalaVersion := "2.9.2"
seq(webSettings :_*)
libraryDependencies ++= Seq(
"javax.servlet" % "servlet-api" % "2.5",
"org.mortbay.jetty" % "jetty" % "6.1.22" % "container"
)
seq(jelasticSettings:_*)
email in deploy := "your_email_address"
password in deploy := "your_jelastic_password"
apiHoster := "your_hoster's_api"
environment in deploy := "your_jelastic_environment_name"
context in deploy := "context_for_your_app"
3. Create a file build.properties in helloworld directory and specify sbt version in there in order to avoid any potential confusion:
sbt.version=0.12.4
>sbt upload
>sbt deploy
Voila! See your Scala app in the the Cloud!
This was just a simple example to show you how to get started. You can use any of the mentioned above plugins for creating your Scala WAR package and deploying it to your environment. If you have any questions regarding this topic please let us know in the comment section below.