X

Scala and Maven - Getting Started Guide using maven-scala-plugin (TOTD #170)

Here is a quick Tip Of The Day (TOTD) that shows
a "pom.xml" to get you started with building and running your Scala applications
using Maven.



  1. Generate the Maven project
    D:\code\samples>mvn archetype:generate \
    -DarchetypeGroupId=org.scala-tools.archetypes \
    -DarchetypeArtifactId=scala-archetype-simple \
    -DremoteRepositories=http://scala-tools.org/repo-releases \
    -DgroupId=org.glassfish.samples \
    -DartifactId=scala-helloworld \
    -Dversion=1.0-SNAPSHOT
    [INFO] Scanning for projects...
    [INFO]
    [INFO] ------------------------------------------------------------------------
    [INFO] Building Maven Stub Project (No POM) 1
    [INFO] ------------------------------------------------------------------------
    [INFO]
    [INFO] >>> maven-archetype-plugin:2.0:generate (default-cli) @ standalone-pom >>>
    [INFO]
    [INFO] <<< maven-archetype-plugin:2.0:generate (default-cli) @ standalone-pom <<<
    [INFO]
    [INFO] --- maven-archetype-plugin:2.0:generate (default-cli) @ standalone-pom ---
    [INFO] Generating project in Interactive mode
    [INFO] Archetype [org.scala-tools.archetypes:scala-archetype-simple:1.3] found in catalog remote
    [INFO] Using property: groupId = org.glassfish.samples
    [INFO] Using property: artifactId = scala-helloworld
    [INFO] Using property: version = 1.0-SNAPSHOT
    [INFO] Using property: package = org.glassfish.samples
    Confirm properties configuration:
    groupId: org.glassfish.samples
    artifactId: scala-helloworld
    version: 1.0-SNAPSHOT
    package: org.glassfish.samples
    Y: : Y
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 6.452s
    [INFO] Finished at: Fri Sep 09 09:59:53 PDT 2011
    [INFO] Final Memory: 12M/153M
    [INFO] ------------------------------------------------------------------------

    This command gets into an interactive mode confirming the entered details. After confirming, the generated directory structure looks like:


    .
    ./.gitignore
    ./pom.xml
    ./src
    ./src/main
    ./src/main/scala
    ./src/main/scala/org
    ./src/main/scala/org/glassfish
    ./src/main/scala/org/glassfish/samples
    ./src/main/scala/org/glassfish/samples/App.scala
    ./src/test
    ./src/test/scala
    ./src/test/scala/samples
    ./src/test/scala/samples/junit.scala
    ./src/test/scala/samples/scalatest.scala
    ./src/test/scala/samples/specs.scala

    And the generated "pom.xml" looks like:
    <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>org.glassfish.samples</groupId>
    <artifactId>scala-helloworld</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>${project.artifactId}</name>
    <description>My wonderfull scala app</description>
    <inceptionYear>2010</inceptionYear>
    <licenses>
    <license>
    <name>My License</name>
    <url>http://....</url>
    <distribution>repo</distribution>
    </license>
    </licenses>
    <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>
    <!--
    <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>
    -->
    <dependencies>
    <dependency>
    <groupId>org.scala-lang</groupId>
    <artifactId>scala-library</artifactId>
    <version>${scala.version}</version>
    </dependency>
    <!-- Test -->
    <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.8.1</version>
    <scope>test</scope>
    </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>
    </dependencies>
    <build>
    <sourceDirectory>src/main/scala</sourceDirectory>
    <testSourceDirectory>src/test/scala</testSourceDirectory>
    <plugins>
    <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>
    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.6</version>
    <configuration>
    <useFile>false</useFile>
    <disableXmlReport>true</disableXmlReport>
    <!-- If you have classpath issue like NoDefClassError,... -->
    <!-- useManifestOnlyJar>false</useManifestOnlyJar -->
    <includes>
    <include>**/*Test.*</include>
    <include>**/*Suite.*</include>
    </includes>
    </configuration>
    </plugin>
    </plugins>
    </build>
    </project>



  2. Edit the "pom.xml" to specify a script to be launched. Add the following code fragment after </executions> for "maven-scala-plugin":
    <configuration>
    <launchers>
    <launcher>
    <id>sample</id>
    <mainClass>org.glassfish.samples.App</mainClass>
    <args>
    <arg>${basedir}</arg>
    </args>
    </launcher>
    </launchers>
    </configuration>



  3. Build and run the project as:

    D:\code\samples\scala-helloworld>mvn scala:run
    [INFO] Scanning for projects...
    [INFO]
    [INFO] ------------------------------------------------------------------------
    [INFO] Building scala-helloworld 1.0-SNAPSHOT
    [INFO] ------------------------------------------------------------------------
    [INFO]
    [INFO] >>> maven-scala-plugin:2.15.0:run (default-cli) @ scala-helloworld >>>
    [INFO]
    [INFO] --- maven-resources-plugin:2.4.3:resources (default-resources) @ scala-helloworld ---
    [INFO] Using 'UTF-8' encoding to copy filtered resources.
    [INFO] skip non existing resourceDirectory D:\code\samples\scala-helloworld\src\main\resources
    [INFO]
    [INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ scala-helloworld ---
    [INFO] Nothing to compile - all classes are up to date
    [INFO]
    [INFO] --- maven-scala-plugin:2.15.0:compile (default) @ scala-helloworld ---
    [INFO] Checking for multiple versions of scala
    [INFO] includes = [**/*.scala,**/*.java,]
    [INFO] excludes = []
    [INFO] D:\code\samples\scala-helloworld\src\main\scala:-1: info: compiling
    [INFO] Compiling 1 source files to D:\code\samples\scala-helloworld\target\classes at 1315588676077
    [INFO] No known dependencies. Compiling everything
    [INFO] prepare-compile in 0 s
    [INFO] compile in 5 s
    [INFO]
    [INFO] --- maven-resources-plugin:2.4.3:testResources (default-testResources) @ scala-helloworld ---
    [INFO] Using 'UTF-8' encoding to copy filtered resources.
    [INFO] skip non existing resourceDirectory D:\code\samples\scala-helloworld\src\test\resources
    [INFO]
    [INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ scala-helloworld ---
    [INFO] Nothing to compile - all classes are up to date
    [INFO]
    [INFO] --- maven-scala-plugin:2.15.0:testCompile (default) @ scala-helloworld ---
    [INFO] Checking for multiple versions of scala
    [INFO] includes = [**/*.scala,**/*.java,]
    [INFO] excludes = []
    [INFO] D:\code\samples\scala-helloworld\src\test\scala:-1: info: compiling
    [INFO] Compiling 3 source files to D:\code\samples\scala-helloworld\target\test-classes at 131558868
    1986
    [INFO] No known dependencies. Compiling everything
    [INFO] prepare-compile in 0 s
    [INFO] compile in 10 s
    [INFO]
    [INFO] <<< maven-scala-plugin:2.15.0:run (default-cli) @ scala-helloworld <<<
    [INFO]
    [INFO] --- maven-scala-plugin:2.15.0:run (default-cli) @ scala-helloworld ---
    [INFO] Checking for multiple versions of scala
    [INFO] launcher 'sample' selected => org.glassfish.samples.App
    Hello World!
    concat arguments = D:\code\samples\scala-helloworld
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 18.970s
    [INFO] Finished at: Fri Sep 09 10:18:13 PDT 2011
    [INFO] Final Memory: 10M/153M
    [INFO] ------------------------------------------------------------------------


    Look for the output "Hello World!" towards the bottom.



Fire away to add more classes and build your application. A subsequent blog will show how to perform database connectivity from a Scala application.

The generated project can be downloaded from

totd170-scala-helloworld.zip
.


More details at:

Join the discussion

Comments ( 1 )
  • guest Wednesday, January 9, 2013

    Thanks a lot.


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.Captcha
Oracle

Integrated Cloud Applications & Platform Services