Saturday, September 10, 2011

Using SBT 0.10.1, Eclipsify for a new Scala project

Run sbt(0.10.1) in a new folder:

D:\samplescala>sbt

D:\samplescala>set SCRIPT_DIR=C:\util\sbt\

D:\samplescala>java -Dfile.encoding=UTF8 -Xmx1536M -Xss1M -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=256m -jar "C:\util\sbt\sbt
-launch.jar"
Getting net.java.dev.jna jna 3.2.3 ...
:: retrieving :: org.scala-tools.sbt#boot-app
        confs: [default]
        1 artifacts copied, 0 already retrieved (838kB/46ms)
Getting Scala 2.8.1 (for sbt)...
:: retrieving :: org.scala-tools.sbt#boot-scala
        confs: [default]
        3 artifacts copied, 0 already retrieved (15178kB/235ms)
Getting org.scala-tools.sbt sbt_2.8.1 0.10.1 ...
:: retrieving :: org.scala-tools.sbt#boot-app
        confs: [default]
        36 artifacts copied, 0 already retrieved (6414kB/965ms)
[info] Set current project to default-097978 (in build file:/D:/samplescala/)

The following folders will show up under the root folder:

Create the default source/test/resource structure:
mkdir src\main\resources
mkdir src\main\scala
mkdir src\main\java
mkdir src\test\resources
mkdir src\test\scala
mkdir src\test\java

Create a build configuration file build.sbt, and place it in the root of the project with the following content:

name:="samplescala"

version:="1.0"

scalaVersion := "2.9.0-1"

libraryDependencies ++= Seq(
    "junit" % "junit" % "4.8" % "test",
    "org.scalatest" % "scalatest_2.9.0" % "1.6.1"
)

defaultExcludes ~= (filter => filter || "*~")

Add, one sample test in the file src\test\scala\gcdtests.scala, just to test out the sbt configuration:
package com.sample

object Gcd{
    def gcd(a:Int, b:Int):Int = if (b==0) a else gcd(b, a%b)
}


import org.scalatest.FlatSpec
import org.scalatest.matchers.ShouldMatchers


class GcdTests extends FlatSpec with ShouldMatchers{
    "GCD of 1440, 408" should  "be 24" in {
        Gcd.gcd(1440, 408) should equal (24)
    }
}


Invoke sbt, and run test, if everything is configured correctly the following output will be displayed:
D:\samplescala>sbt

D:\samplescala>set SCRIPT_DIR=C:\util\sbt\

D:\samplescala>java -Dfile.encoding=UTF8 -Xmx1536M -Xss1M -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=256m -jar "C:\util\sbt\sbt
-launch.jar"
[info] Set current project to default-097978 (in build file:/D:/samplescala/)
> test
[info] Updating {file:/D:/samplescala/}default-097978...
[info] Done updating.
[info] GcdTests:
[info] GCD of 1440, 408
[info] - should be 24
[info] Passed: : Total 1, Failed 0, Errors 0, Passed 1, Skipped 0
[success] Total time: 1 s, completed Sep 10, 2011 7:00:28 PM

Now, to import this project into eclipse. Create a build.sbt file with the following contents and place in the project/plugins folder:
libraryDependencies <+= (libraryDependencies, sbtVersion) { (deps, version) => 
    "de.element34" %% "sbt-eclipsify" % "0.10.0-SNAPSHOT"
}

Restart sbt, if the plugin is configured correctly, eclipse will be a valid task in sbt, running this will create the .project and .classpath files for Eclipse:
> eclipse
[info] Starting eclipse
[info] written .project for samplescala
[info] written .classpath for samplescala
[info] * Don't forget to install the Scala IDE Plugin from http://www.scalaide.org/
[info] You may now import your projects in Eclipse

Assuming that Scala IDE for Eclipse is installed, import this new project into Eclipse:

No comments:

Post a Comment