To be able to run an interactive programming session in the previous chapters, we have downloaded and installed a tool named
Typesafe Activator. Running either as a command-line tool or through a web browser, the activator lets us create and execute a sample project out of a template, in this case, a minimal hello-scala
project. From it, we have accessed the SBT console, which acts as a REPL.
Typesafe Activator can be seen as a lightweight IDE powered by SBT. It provides many project templates that programmers can reuse as a starting point in their new development project.
Open a command terminal window and go to the directory where you extracted the activator, then enter the following command:
> ./activator new Enter an application name >
You need to enter a name for your new project as follows:
> javasample Fetching the latest list of templates... Enter a template name, or hit tab to see a list of possible templates > [Hit TAB] activator-akka-cassandra activator-akka-spray activator-play-autosource-reactivemongo activator-scalding activator-spray-twitter akka-callcenter akka-cluster-sharding-scalaakka-clustering akka-distributed-workers akka-distributed-workers-java akka-java-spring akka-sample-camel-java akka-sample-camel-scalaakka-sample-cluster-java akka-sample-cluster-scalaakka-sample-fsm-java-lambda akka-sample-fsm-scalaakka-sample-main-java akka-sample-main-scalaakka-sample-multi-node-scala akka-sample-persistence-java akka-sample-persistence-scala akka-sample-remote-java akka-sample-remote-scala akka-scala-spring akka-supervision angular-seed-play atomic-scala-examples dart-akka-spray eventual hello-akka hello-play hello-play-backbone hello-play-java hello-play-scala hello-sbt hello-scala hello-scala-eclipse hello-scaloid hello-slick just-play-scalamacwire-activator matthiasn-sse-chat-template modern-web-template play-akka-angular-websocket play-angularjs-webapp-seed play-cake play-example-form play-guice play-hbase play-java-spring play-mongo-knockout play-scalatest-subcut play-slick play-slick-advanced play-spring-data-jpa play-sqlite play-with-angular-requirejs play-yeoman play2-crud-activator reactive-maps reactive-stocks realtime-search scala-phantom-types scaldi-play-example scalikejdbc-activator-template six-minute-apps slick-android-example slick-codegen-customization-example slick-codegen-example slick-plainsql spray-actor-per-request tcp-async template-template test-patterns-scala tweetmap-workshop
Version 1.0.13 that we are using already contains 76 templates combining diverse technologies and frameworks together to make some interesting demo projects, but this list is increasing quickly (from 38 to 76 between Version 1.0.0 and 1.0.13, which are only a few months apart).
For now, let's take a look at the play-java-spring
template, a project sample in Java, so that we can feel comfortable with the code it contains. Therefore, enter its name when prompted for the name of the template to be used:
> play-java-spring OK, application "javasample" is being created using the "play-java-spring" template. To run "javasample" from the command-line, run: /Users/thomas/scala/activator-1.0.13/javasample/activator run To run the test for "javasample" from the command-line, run: /Users/thomas/scala/activator-1.0.13/javasample/activator test To run the Activator UI for "javasample" from the command-line, run: /Users/thomas/scala/activator-1.0.13/javasample/activator ui
The activator creates a SBT project, meaning you can edit build.sbt
or plugins.sbt
to add dependencies, repositories (that is, resolvers) as well as SBT plugins. We can, for example, reuse the addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "2.4.0")
line in plugins.sbt
that we stated earlier to be able to create Eclipse project files and import the project into our Scala IDE.
First, let's execute the program to see what it does:
> cd javasample > ./activator run
As the sample is based on the Play framework (that we will cover in later chapters), the following is displayed to indicate that the web application is deployed on localhost at port 9000:
--- (Running the application from SBT, auto-reloading is enabled) --- [info] play - Listening for HTTP on /0:0:0:0:0:0:0:0:9000 (Server started, use CTRL +D to stop and go back to the console...)
Open a browser at localhost:9000 to visualize the very basic web form of the sample and submit a couple of entries to be stored in the tiny database, as shown in the following screenshot:
This web application takes an input from a simple HTML form and saves Bar
objects into a small database through JPA.
To take a look at the code that is part of this template; we can run it through the activator by first pressing Ctrl + D in the command window to interrupt the current execution, and then, enter the following command:
> ./activator ui
After a few seconds, a browser page should open at http://localhost:8888/app/javasample/
displaying the activator user interface specifically targeted to this application. Click on the Code view & Open in IDE item and navigate to the app/models/Bar.java
file by double-clicking on the items on the left-hand side panel, as shown in the following screenshot:
The browser displays a JPA-annotated entity as we are used to working with typically in the Eclipse IDE with colored and formatted syntax. The panel on the right-hand side leaves room for a tutorial, a precious feature to quickly understand the code and start modifying it. The top menu enables you to compile, run, or test the application from within the browser. You can open some of the other source files to identify the structure of the code, although we will cover play web applications in detail later on.
In summary, Typesafe Activator is a way to get you started in just minutes and is very flexible as you can run activator projects directly as SBT projects, therefore, having the possibility to generate IDE-specific files to continue working in Eclipse, IDEA, or NetBeans if you wish to.