2012-03-31

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.

Right click on the build.gradle file and choose Gradle -> Refresh All. Give it a while...

Right click on the build.gradle file and choose Run As -> Gradle Build. Tick build and hit Run.

This should build the project and obtain all dependencies.

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.

Paste Launcher.scala content (from below).

You can now right click on Launcher, choose Run As -> Scala Application and it will run.

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.