Wednesday, September 1, 2010

Creating a Java/Scala Maven project

In this post, I will explain how I set up my first program, allowing me to create and run an extremely simple Scala program in my favorite IDE using Maven.

Structure

I started out with creating the following directory structure:

scalasim
+-- src
|   +-- main
|   |   +-- java
|   |   +-- resources
|   |   \-- scala
|   |       \-- Simulation.scala
|   \-- test
|       +-- java
|       +-- resources
|       \-- scala
\-- pom.xml

Nothing exciting here – just an ordinary Maven project structure with two extra scala directories.

The POM

Creating the file pom.xml was not very exciting either; I basically copy-pasted it from the Scala website:

<?xml version="1.0" encoding="UTF-8"?>
 <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>nl.scalasim</groupId>
  <artifactId>scalasim</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>
  <name>Scala SIM</name>
  <description>
    Tutorial application for creating a city simulation in the Scala
    programming language.
  </description>
  <url>http://scala-simulation.blogspot.com</url>
  <inceptionYear>2010</inceptionYear>
  <dependencies>
    <dependency>
      <groupId>org.scala-lang</groupId>
      <artifactId>scala-library</artifactId>
      <version>2.8.0</version>
    </dependency>
  </dependencies>
  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.scala-tools</groupId>
          <artifactId>maven-scala-plugin</artifactId>
          <version>2.9.1</version>
        </plugin>
      </plugins>
    </pluginManagement>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
          <encoding>UTF-8</encoding>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.scala-tools</groupId>
        <artifactId>maven-scala-plugin</artifactId>
        <executions>
          <execution>
            <goals>
              <goal>compile</goal>
              <goal>testCompile</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <launchers>
            <launcher>
              <id>main</id>
              <mainClass>nl.scalasim.Simulation</mainClass>
            </launcher>
          </launchers>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <repositories>
    <repository>
      <id>scala-tools.org</id>
      <name>Scala-tools Maven2 Repository</name>
      <url>http://scala-tools.org/repo-releases</url>
    </repository>
  </repositories>
  <pluginRepositories>
    <pluginRepository>
      <id>scala-tools.org</id>
      <name>Scala-tools Maven2 Repository</name>
      <url>http://scala-tools.org/repo-releases</url>
    </pluginRepository>
  </pluginRepositories>
</project>

The only interesting part can be found in lines 62–67 where I added a launcher configuration which enables me to run my scala program from the command line using mvn scala:run.

Using my favorite IDE I installed the Scala plugin, imported the POM, and was ready to go.

My first program

Now, everything was ready to run, and I created the following program in src/main/scala/Simulation.scala:

object Simulation extends Application {
  println("My first Scala simulation")
}

Running the code (using mvn scala:run) resulted in the usual Maven output, interspersed with the text:

[INFO] My first Scala simulation

I did my hurray-it-works-dance, wrote this blog entry, and went on to do the next entry where actually exciting stuff will happen.

Lessons learned

Even in this short exercise, I learned a few things about Scala:

  • Setting up a Scala project is just as easy as setting up an arbitrary Java project;
  • Creating a small Scala program is easier than creating a similar program in Java;
  • Scala's support for object definitions instead of convoluted singletons is nice.

Get the code

The full code for this post can be found at code.google.com.

No comments:

Post a Comment