DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Frameworks
  4. Getting Started with Scala and Scalatra

Getting Started with Scala and Scalatra

Jos Dirksen user avatar by
Jos Dirksen
·
Sep. 08, 12 · Interview
Like (0)
Save
Tweet
Share
8.43K Views

Join the DZone community and get the full member experience.

Join For Free

scalatra is a lightweight scala based micro-web framework, that can be used to create high performance websites and apis. in this tutorial we'll get started with the installation of scalatra and import our test project into eclipse.

sbt and giter8

before you can get started you need to install a couple of tools (i'm assuming you've got a jdk 1.6+ installed). i'll give you the condensed installation instructions, and extensive version of this can be found at the following scalatra page ( http://www.scalatra.org/getting-started/first-steps.html ). this approach should work for most environments, for my own however, it didn't work... it downloaded old versions of giter8 and an old version of sbt. for sbt i used macports to get the latest version:

port install sbt

and i also downloaded giter8 manually.

curl https://raw.github.com/n8han/conscript/master/setup.sh | sh
cs n8han/giter8

this last command will install g8 in your /bin directory. i've tested this with this g8 version:

~/bin/g8
 
giter8 0.5.0
usage: g8 [template] [option]...
apply specified template.
 
options
    -b, --branch
        resolves a template within a given branch
    --paramname=paramvalue
        set given parameter value and bypass interaction.
 
 
apply template and interactively fulfill parameters.
    g8 n8han/giter8
 
or
    g8 git://github.com/n8han/giter8.git
 
apply template from a remote branch
    g8 n8han/giter8 -b some-branch
 
apply template from a local repo
    g8 file://path/to/the/repo
 
apply given name parameter and use defaults for all others.
    g8 n8han/giter8 --name=template-test

create initial project

go to the root directory where you keep you projects and run the following:

jos@joss-macbook-pro.local:~/dev/scalatra/firststeps$ g8 scalatra/scalatra-sbt
organization [com.example]: org.smartjava
package [com.example.app]: org.smartjava.scalatra     
name [scalatra-sbt-prototype]: hello-scalatra
servlet_name [myscalatraservlet]: helloscalatraservlet
scala_version [2.9.1]: 
version [0.1.0-snapshot]:

this will create a hello-scalatra folder in the directory your in that contains the project.

./build.sbt
./project
./project/build.properties
./project/plugins.sbt
./readme.md
./src
./src/main
./src/main/resources
./src/main/resources/logback.xml
./src/main/scala
./src/main/scala/org
./src/main/scala/org/smartjava
./src/main/scala/org/smartjava/scalatra
./src/main/scala/org/smartjava/scalatra/helloscalatraservlet.scala
./src/main/scala/scalatra.scala
./src/main/webapp
./src/main/webapp/web-inf
./src/main/webapp/web-inf/layouts
./src/main/webapp/web-inf/layouts/default.scaml
./src/main/webapp/web-inf/views
./src/main/webapp/web-inf/views/hello-scalate.scaml
./src/main/webapp/web-inf/web.xml
./src/test
./src/test/scala
./src/test/scala/org
./src/test/scala/org/smartjava
./src/test/scala/org/smartjava/scalatra
./src/test/scala/org/smartjava/scalatra/helloscalatraservletspec.scala

to test if everything is working, go into this directory and use sbt to start the application from sbt with 'container:start'. this will download a lot of stuff, and finally start the application:

jos@joss-macbook-pro.local:~/dev/scalatra/firststeps/hello-scalatra$ sbt
[info] loading project definition from /users/jos/dev/scalatra/firststeps/hello-scalatra/project
[info] set current project to hello-scalatra (in build file:/users/jos/dev/scalatra/firststeps/hello-scalatra/)
> container:start
[info] jetty-8.1.5.v20120716
[info] no jsp support for /, did not find org.apache.jasper.servlet.jspservlet
[info] started o.e.j.w.webappcontext{/,[file:/users/jos/dev/scalatra/firststeps/hello-scalatra/src/main/webapp/]}
[info] started o.e.j.w.webappcontext{/,[file:/users/jos/dev/scalatra/firststeps/hello-scalatra/src/main/webapp/]}
15:12:44.604 [pool-6-thread-4] info  o.scalatra.servlet.scalatralistener - initializing life cycle class: scalatra
[info] started o.e.j.w.webappcontext{/,[file:/users/jos/dev/scalatra/firststeps/hello-scalatra/src/main/webapp/]}
15:12:44.727 [pool-6-thread-4] info  o.f.s.servlet.servlettemplateengine - scalate template engine using working directory: /var/folders/mc/vvzshptn22lg5zpp7fdccdzr0000gn/t/scalate-5609005579304140836-workdir
[info] started selectchannelconnector@0.0.0.0:8080
[success] total time: 1 s, completed sep 7, 2012 3:12:44 pm
>
[success] total time: 1 s, completed sep 7, 2012 3:10:05 pm

to test if everything is correct point the browser to localhost:8080 and you'll see the following screen:

localhost_8080.jpg

import in eclipse

i usually develop with eclipse, so in this case, lets make sure we can edit the source code in eclipse. for this we can use sbt eclipse plugin. adding this plugin is very easy. go to the 'project' folder and add the following (with an empty line before it) to the plugins.sbt file.

addsbtplugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "2.1.0")

when you next run sbt, you'll see a lot of stuff being downloaded. from sbt run 'eclipse' and eclipse configuration files (.classpath and .project) will be created. now start up the eclipse ide and you can import the project you just created (make sure you also install the scala-ide for eclipse ).

scala - hello-scalatra_src_main_scala_org_smartjava_scalatra_helloscalatraservlet.scala - eclipse - _users_jos_dev_workspaces_workspace-scala.jpg

now we can start the container, and let sbt listen for changes in our resources.

$ sbt
> container:start
> ~ ;copy-resources;aux-compile

any time we save a file in eclipse, sbt will copy and recompile the resources. so we can directly see our changed files in the browser. open the file helloscalatraservlet and change the welcome text. if you save this, sbt will reload the application and you'll directly see the changed files.

localhost_8080-1.jpg

that's it for this tutorial!

Scalatra Scala (programming language) Eclipse

Published at DZone with permission of Jos Dirksen, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How to Create a Real-Time Scalable Streaming App Using Apache NiFi, Apache Pulsar, and Apache Flink SQL
  • Kubernetes vs Docker: Differences Explained
  • Differences Between Site Reliability Engineer vs. Software Engineer vs. Cloud Engineer vs. DevOps Engineer
  • Better Performance and Security by Monitoring Logs, Metrics, and More

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: