Scala User Group Hamburg

Die Scala User Group Hamburg ist da. Nachdem wir bereits im letzten Jahr innerhalb der Firma unsere Bemühungen verstärkt hatten das Thema Scala voranzutreiben, wagen wir nun den Schritt an die Öffentlichkeit. Ich bin gespannt wo uns die Reise hinführt, aktuell gibt es noch viele denkbare Richtungen und vielfältige Möglichkeiten sich einzubringen. Zunächst freue ich mich aber erstmal auf spannende Diskussionen bei den ersten Treffen und auf der Mailingliste. Also: Anmelden und Mitdiskutieren!

@all non-german-speakers: Sorry that this post is not in german, but as the User Group is German as well, understanding this post would be of little help for you anyways 😉

Double Challenge #4 – Scala User Group and Some Sports

Finally! It took some time to finish my last challenge, but in the end I managed to deploy a simple Play application to Heroku and I have also written a tutorial about it. So the curious folks among you might have a look at it, and hopefully get the application running faster than me. Well, probably the writing part took me twice as long as creating and deploying the application. Overall I have to say Play 2.0, Heroku and Scala left a very good impression, individually and in combination.

So what are the next items in my pipeline:

  • Take our work-internal Scala group to the next level and start the first Scala User Group in Hamburg
  • Get fit and healthy for the summer (Exercise/work out three times a week)

Getting started with Play 2.0, Heroku and Scala

This is another tutorial of the category ‘quick start for absolute beginners’. This time the topic is on Play 2.0, Heroku and Scala. The reason for this tutorial is that recent announcements indicate that the three topics will have a bright future together. A really nice overview of all three topics can be found in this presentation on slideshare (simply skip the Akka part. Or even better, read it as well!).

Play 2.0

As the time of writing Play 2.0 is still in beta status and according to the official homepage, APIs are likely to change a bit in the future. Nevertheless, I guess it is time to have a quick look into the new features of the framework and create a simple application.

Installing Play

As we would like to access all latest features, we are going to build Play from the source code ourselves. Therefore, you need to retrieve the sources from GitHub. If you haven’t installed git yet, do so now. Next, clone the play repository by calling git clone git://github.com/playframework/Play20.git

Once you have the source code on your local machine, switch to the ../play20/framework folder and type build. This will start up an embedded sbt shell, in which you have to execute the build-repository command. (Make sure that you have a solid internet connection, as this step will require to download some megabytes of data. I tried it in the train, which was very frustrating…)

To complete the installation, you need to add the ../play20/ folder to your systems PATH environment variable.

More detailed instructions on how to install Play 2.0 (either from source, or from pre-compiled binary packages) can be found in the Play 2.0 wiki.

Creating a Simple Application

Once Play 2.0 is setup on your machine, you can create a simple applicationby typing play new myFirstApp. Provide the name of your application and as we would like to use Scala as our main development language, select the according entry. And that is it! You have just created your first Play application.

Play 2.0 comes with a nifty little console, which supports many development task out of the box. The most common command for you to execute is likely to be run. Others might include, console, clean, compile and test. The commands are more or less self explanatory. Nevertheless, there is also a help goal (you may guess what this one does), and a little bit of documentation.

If you don’t want to open the Play Console just to execute a single command, you can also type play [your_command]. For now, this is what you should do, type play run to start the server in development mode.

After the server has been started, open a browser and go to http://localhost:9000 You should see something similar to the screenshot below.

Creating and running a Play application

Play 2.0 Application

Where to go from here

In the previous section we have successfully created a simple Play 2.0 application. The second part of this post will show you how to get this application running on Heroku. But before we go there, I would like to point you to some further resources to get familiar with Play 2.0.

Most defiantly you should have a closer look on the anatomy of a Play 2.0 application. Furthermore, there are three nice sample applications, which are provided within the Play 2.0 sources and documented adequately in the wiki. Last but not least you might also want to browse through Play’s scala api manual.

Heroku

I promised you, that this article will show you how to get your Play application running on Heroku. So let’s see how this is done.

Quickstarting Heroku

First of all, go to heroku signup page and enter your E-Mail address. After you have received a confirmation mail and provided a password, you are successfully signed up.

Next go to the quickstart guide and download the heroku toolbelt corresponding to your system and install it. After the installation is completed you can start to access heroku from the command line. Type heroku login to connect to the heroku platform servers. The first time you connect to heroku you need to provide your personal SSH key (or simply hit enter to automatically create a new one).

Now that you are logged in to Heroku, you are ready to deploy your Play application. Unfortunately, your app isn’t ready for that process yet, so let’s apply some minor changes.

Making your app ready for Heroku

The following section is based on the Deploying Play to Heroku tutorial, you might either read the original source or the summary given here.

Heroku needs to know how to start your add, therfore you should add a start script provided by the great folks at typesafe. Simply add the following line to your project/plugins.sbt file (Ensure that you also place a blank line before and after this line): addSbtPlugin(“com.typesafe.startscript” % “xsbt-start-script-plugin” % “0.5.0”)

Next, you need to create a plain text file in your application’s root directory called Procfile containing the following line: web: target/start

The next thing to do is create a local git repository for your application. In your application root folder, just type git init to create the repo, then type git add . to add all files and folder and finally make a commit by typing git commit -m “init”.

Last thing to do is creating your app at heroku, do this by typing heroku create –stack cedar. (Make sure you are still logged in to heroku!) You shoud note down the url where your application is going to be available. In my case that is http://blooming-dawn-3920.heokuapp.com/

Making your app ready for heroku

Deploying your app

By now, everything you need to deploy your Play 2.0 application to Heroku should be set up. The last thing you need to do is simply push your contents from your local git repository to the heroku server. To do so, type git push heroku master, after your sources are transferred to the remote repository an automatic execution of sbt clean compile stage is triggered. If everything went well, you should see something similar to the screen shot below. You should also be able to go to your url and see your application running there.

Successful Heroku Deployment

You are now ready to develop some nice features for your application. Whenever you want to deploy a new version of your app heroku, simply push your changes by executing git push heroku master again. Three more hints before I leave you alone with your app: You can tpye heroku ps to check the state of your application, call heroku logs to access your applications log files, and even start a remote REPL session by executing heroku run sbt play.

And now, happy coding!

Get rid of “unmappable character for encoding Cp1252” once and for all

Nowadays, many open source as well as commercially developed projects contain code that is UTF-8 encoded. More general it is very unlikely that you checkout some code from GIT, SVN or [insert arbitrary abbreviation here] that is actually encoded with Windows’ crappy default encoding. (In case of my western European system, that encoding would be Cp1252).

So, how many times have you seen the above error when compiling some Java code on a windows machine? And how many times have you wondered if there is a solution to set the file encoding in a global, system wide manner? Well, fortunately, there is a way to do that!

A quite simple procedure to globally set the file encoding for all JVMs is given in the answer by erickson in this stackoverflow question. All you need to do, is to specify a envirnoment variable called JAVA_TOOL_OPTIONS. If you set this variable to -Dfile.encoding=UTF8, everytime a JVM is started, it will pick up this information.

Obviously, you are able set other JVM parameters via JAVA_TOOL_OPTIONS as well. See the JVM documentation for more details. If you are interested in further details about character encodings in Java, check out this excellent article.

Double Challenge #3 – Play!

It’s December already, which means it is time for the last double challenge of the year and a quick retrospective.

Retrospective:

November’s double challenge was pretty much successful. I’ve taken care of almost all Christmas presents (only minor things are missing) and have read the aimed 250 pages in my Scala book. With that preparation I think I am ready for the next steps in Scala… so here is the goal for this month.

Upcoming double challenge:

  • Deploy a simple Play! application, written in Scala, to Heroku. Why? Recent announcements indicate that the three topics will have a bright future together.
  • Do not drink any sugared/sweetened beverages until Christmas. Let’s find out how many drinks are actually full of sugar…

Double Challenge #2 – Christmas and Scala

It has been a bit more than a month since I ‘ve been writing about the first double challenge. Thus, I think it is time for a short retrospective and a call-out of the next challenges.

Retrospective:

The first double challenge was only half a success. Cutting down meat consumption to three days a week has dramatically reduced my overall meat consumption. It wasn’t easy, but I can absolutely recommend this model (maybe start with two or three meat free days a week). I will defiantly stick with the four meat-less-days-a-week model for a few more months.
The technical challenge on the other hand was not that successful. Unfortunately, I couldn’t really find the time to get an application running on cloud foundry. Well, I got quite far with that (it is quite easy), but finding the time to write a tutorial about it was pretty much impossible. Fortunately, some good tutorials about this topic are already existent.

Upcoming double challenge:

  • Buy all Christmas presents until the end of November. Why? To escape the pre-christmas stress! Man, I hate shopping in December.
  • Work myself through another 250 pages of my beloved Scala book. (And write heaps of Scala code on the way.) Why? Scala! 😉

Scala & Maven – POM Example

Today, there is no waffle about what Scala or Maven is. This is just the plain configuration to build Scala projects with Maven. If you want more detailed information check Maven for Beginners at scala-lang.org or browse through the documentation of the Scala-Maven-Plugin.

By the way,the configuration works as well if you want to mix Java and Scala within a single project. Just place your scala files in src/main/scala and your Java files in src/main/java (or the src/test equivalents). Maven will do the rest for you.

Enjoy!

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>my.company</groupId>
  <artifactId>my-artifact</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>

  <repositories>
    <repository>
      <id>scala-tools.org</id>
      <name>Scala-tools Repository</name>
      <url>http://scala-tools.org/repo-releases</url>
    </repository>
  </repositories>
  <pluginRepositories>
    <pluginRepository>
      <id>scala-tools.org</id>
      <name>Scala-tools Repository</name>
      <url>http://scala-tools.org/repo-releases</url>
    </pluginRepository>
  </pluginRepositories>

  <dependencies>
    <dependency>
      <groupId>org.scala-lang</groupId>
      <artifactId>scala-library</artifactId>
      <version>2.9.1</version>
    </dependency>
    <dependency>
      <groupId>org.scalatest</groupId>
      <artifactId>scalatest_2.9.1</artifactId>
      <version>1.6.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.10</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.scala-tools</groupId>
        <artifactId>maven-scala-plugin</artifactId>
        <executions>
          <execution>
            <goals>
              <goal>compile</goal>
              <goal>testCompile</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <jvmArgs>
            <jvmArg>-Xms64m</jvmArg>
            <jvmArg>-Xmx1024m</jvmArg>
          </jvmArgs>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

Getting started with Scala

Update: Scala Documentation (an excellent site for getting started with Scala) has just been launched. Make sure to check it out as well.

If you want to get started with Scala, here is a short list with (in my opinion) some of the most important resources to get you on the fast track.

  1. Get a book! I highly recommend this one, but there are plenty out there already.
  2. Install SBT and learn how to use it. (You might as well use Maven with Scala if you really want to.)
  3. Install Scala IDE for Eclipse and use sbteclipse to import your SBT projects.
  4. Get some friends out there, and learn.
  5. And not to forget my personal favourite for quick hacks: Simply Scala
  6. Enjoy!

First Dobule Challenge – Less meat, more clouds

I am quite a fan of Lars Vogels’s Challenge of the Month. That is why I decided to introduce a similar category on my blog. But, as I don’t want to be a typical European copy-cat, I will do it slightly different. Every month I will set up a double challenge for myself. Including one IT concerned challenge and one non-IT concerned challenge. By doing this, I hope it will a) improve my writing, b) improve my IT-skills and c) improve some other areas in my live.

So my first double challenge will be: