Getting Started with Scala and Scalatra
Join the DZone community and get the full member experience.
Join For Freescalatra 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:
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 ).
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.
that's it for this tutorial!
Published at DZone with permission of Jos Dirksen, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
Azure Virtual Machines
-
How AI Will Change Agile Project Management
-
How To Use an Automatic Sequence Diagram Generator
-
Performance Comparison — Thread Pool vs. Virtual Threads (Project Loom) In Spring Boot Applications
Comments