poniedziałek, 20 maja 2013

SetUp Scala development with Maven

To generate simple scala application by maven execute:

 mvn archetype:generate \
-DarchetypeGroupId=org.scala-tools.archetypes \
-DarchetypeArtifactId=scala-archetype-simple \ -DremoteRepositories=http://scala-tools.org/repo-releases \ -DgroupId=org.scala.test.app \
-DartifactId=TestScalaApp \
-Dversion=1.0-SNAPSHOT

Scala stands for "Scalable Language". It is defined as a multi-paradigm programming language and has integrated support for object-oriented programming and functional programming. It runs on the JVM and on Android. It can read existing Java libraries which give it a huge advantage in cases where there is a lot of existing code infrastructure in Java.

Now we can look to generated code:
Simple Hello world App.scala:

package org.scala.test.app

object App {
 
  def foo(x : Array[String]) = x.foldLeft("")((a,b) => a + b)
 
  def main(args : Array[String]) {
    println( "Hello World!" )
    println("concat arguments = " + foo(args))
  }

}


The pom.xml file is configured in a way that inntegrated the build lifecycle with Maven Scala plugin:

      <plugin>
        <groupId>org.scala-tools</groupId>
        <artifactId>maven-scala-plugin</artifactId>
        <version>2.15.0</version>
        <executions>
          <execution>
            <goals>
              <goal>compile</goal>
              <goal>testCompile</goal>
            </goals>
            <configuration>
              <args>
                <arg>-make:transitive</arg>
                <arg>-dependencyfile</arg>
                <arg>${project.build.directory}/.scala_dependencies</arg>
              </args>
            </configuration>
          </execution>
        </executions>
      </plugin>


Compile  project: mvn compile
Run tests: mvn test

The generated pom.xml file contains Scala dependencies:

 <properties>
    <maven.compiler.source>1.5</maven.compiler.source>
    <maven.compiler.target>1.5</maven.compiler.target>
    <encoding>UTF-8</encoding>
    <scala.version>2.8.0</scala.version>
  </properties>

    <dependency>
      <groupId>org.scala-lang</groupId>
      <artifactId>scala-library</artifactId>
      <version>${scala.version}</version>
    </dependency>
    <dependency>
      <groupId>org.scala-tools.testing</groupId>
      <artifactId>specs_${scala.version}</artifactId>
      <version>1.6.5</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.scalatest</groupId>
      <artifactId>scalatest</artifactId>
      <version>1.2</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.8.1</version>
      <scope>test</scope>
    </dependency>

The first dependency is the Scala programming language, that is org.scala-lang.scala-library. The next two are for running Scala tests. The last one is for junit tests.


The last think what we will look on is the maven scala plugin. This are available goals and their explanations:
  • mvn scala:compile -Compile the Scala source directory
  • mvn scala:console - Launches the Scala REPL with all project dependencies
  • mvn scala:doc - Generates the documentation for all Scala sources
  • mvn scala:help - Displays the Scala compiler help
  • mvn scala:run - Executes a Scala class
  • mvn scala:script - Executes a Scala Scripts
  • mvn scala:testCompile - Compiles the Scala test sources



Brak komentarzy:

Prześlij komentarz