2012-03-31

Scala + Guice + Gradle Tutorial (Part 1)

Hi, in this tutorial I will show you how to create a Gradle based Scala project that uses Guice for dependency injection.

Create Gradle project in IDE from existing Gradle sources

If you already have a Gradle project, you can easily generate Eclipse or IntelliJ project files using available plugins (Eclipse plugin, IntelliJ plugin). I haven't found anything like that for NetBeans but I'm not a NetBeans user.

Create Gradle project from scratch

If you do not have Gradle sources and are starting a project from scratch, it's a bit more tiresome, because at this moment neither Eclipse nor IntelliJ support creating Gradle projects. NetBeans has a Gradle plugin, but I haven't tried it out.

(Option 1 - any IDE) In that case you can create a Gradle project skeleton by hand and then import it into your IDE using methods above (there seems to be no way to create a skeleton project automatically at this moment).

(Option 2 - just Eclipse) You can create a new Scala project in your IDE and add Gradle nature - but that at the moment only seems to work in Eclipse. Again, I haven't tried that with NetBeans.

Here's how to do it with SpringSource Tool Suite:
  1. Download and install STS if you haven't already.
  2. Launch STS.
  3. Install these extensions: Scala, Gradle, Groovy. Maven support is not needed.
  4. Create a new Scala Project.
  5. Create source folders: src/main/scala, src/main/resources, src/test/scala, src/test/resources.
  6. Right click on the project, Configure -> Convert to Gradle project.
  7. Create a file called build.gradle in the root project directory.
  8. Paste sample build.gradle content (see below).
  9. Right click on the build.gradle file and choose Gradle -> Refresh All. Give it a while...
  10. Right click on the build.gradle file and choose Run As -> Gradle Build. Tick build and hit Run.
  11. This should build the project and obtain all dependencies.
  12. Create a Scala object in a package inside src/main/scala. In my case the package is me.m1key.audioliciousmigration and the class name is Launcher. That corresponds to the build.gradle manifest Main-class declaration.
  13. Paste Launcher.scala content (from below).
  14. You can now right click on Launcher, choose Run As -> Scala Application and it will run.
  15. If you now run gradle install rather that gradle build, it will deploy the artifact to your local Maven (.m2) repository. The artifact name is deduced from the project name.
If you create your project like that, you can successfully import it into another STS instance or IntelliJ, I checked that.

build.gradle

apply plugin: 'scala'
apply plugin: 'maven'

sourceCompatibility = 1.6
version = '0.0.1-SNAPSHOT'
group = 'me.m1key.audiolicious-migration'

jar {
   manifest {
       attributes 'Implementation-Title': 'Audiolicious Migration', 'Implementation-Version': version,
               'Main-class': 'me.m1key.audioliciousmigration.Launcher'
   }
   from {
       configurations.compile.collect {
           it.isDirectory() ? it : zipTree(it)
       }
   }
}

repositories { mavenCentral() }

dependencies {
   compile group: 'com.google.inject', name: 'guice', version: '3.0'
   testCompile group: 'org.scala-tools.testing', name: 'specs_2.9.1', version: '1.6.9'
   testCompile group: 'junit', name: 'junit', version: '4.+'
   
   // Libraries needed to run the scala tools
   scalaTools 'org.scala-lang:scala-compiler:2.9.1'
   scalaTools 'org.scala-lang:scala-library:2.9.1'

//     Libraries needed for scala api
   compile 'org.scala-lang:scala-library:2.9.1'
}

Launcher.scala

package me.m1key.audioliciousmigration

object Launcher {

 def main(args: Array[String]): Unit = {
    println("Hello world!")
 }

}
Once it's built, you can run the jar like that from project root:

java -jar build\libs\audiolicious-migration-0.0.1-SNAPSHOT.jar (Windows)

java -jar build/libs/audiolicious-migration-0.0.1-SNAPSHOT.jar (Unix)

Unit Tests

Here's how to write a unit test. You can run it as a JUnit test in your IDE and it runs as a part of gradle build, which I think is quite cool.
package me.m1key.audioliciousmigration

import org.specs._
import org.specs.runner._
import org.junit.runner.RunWith

@RunWith(classOf[JUnitSuiteRunner])
class LauncherSpec extends Specification with JUnit {
   
   "2 + 2" should {    
       "equal 4" in {
           2 + 2 mustBe 4
       }
   }

}
gradle install does not run tests!

Download source code

Source code for this article can be obtained via GitHub.

Guice

In Part 2 of this tutorial I will describe how to use Guice in this project.