Cloud Zone is brought to you in partnership with:

Marina Sprava, Public Relations manager at Jelastic since October 2011, has technical engineering background in systems of control and automation. Scientific interests: cloud computing, cloud hosting, agile methodologies and medical imaging. Marina is a DZone MVB and is not an employee of DZone and has posted 15 posts at DZone. You can read more from them at their website. View Full User Profile

Getting started with Scala on Jelastic

07.10.2013
| 3304 views |
  • submit to reddit

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.

scala environment

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, toproject/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" % "6.1.22" % "container"
)

Note:  Blank lines are required in order to separate each item.

3. Create a file build.properties in the basedirectory 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 thetarget/scala-x.x.x directory.

5. Go back to the Jelastic dashboard and upload your WAR package to the Deployment Manager.

scala upload

6. Deploy your Scala application to the environment you’ve just created.

scala deploy

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.

import JelasticKeys._
 
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"

Note:  Blank lines are required in order to separate each item.

3. Create a file build.properties in helloworld directory and specify sbt version in there in order to avoid any potential confusion:

1sbt.version=0.12.4

4. Use the upload task to build a WAR file and upload it to the Jelastic Deployment Manager for further its deployment. Use deploy task to build a WAR file, upload and deploy it to the specified environment and context.


>sbt upload
>sbt deploy

Voila! See your Scala app in the the Cloud!

scala app in 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.





Published at DZone with permission of Marina Sprava, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)