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:- Download and install STS if you haven't already.
- Launch STS.
- Install these extensions: Scala, Gradle, Groovy. Maven support is not needed.
- Create a new Scala Project.
- Create source folders: src/main/scala, src/main/resources, src/test/scala, src/test/resources.
- Right click on the project, Configure -> Convert to Gradle project.
- Create a file called build.gradle in the root project directory.
- Paste sample build.gradle content (see below).
- 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.
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!