Getting into Scala and Gradle

I’ve decided to deliberately put some time aside to get back to coding and explore some of the technologies that the day job doesn’t allow time for. It is an opportunity to get more deep in to tools that I have had an interest in for a while, and so I thought I would put a hobby project together using the following:

Scala

Given the uncertainty around the Java language, even before Oracle took it over, I keep coming back to the question of what is likely to replace Java. Scala, to me at least, looks to be a genuine contender.

Gradle

For all that Maven is a pain in the proverbial, I still prefer it to writing reams and reams of Ant configuration that gets copied and pasted across multiple projects. Perhaps Gradle is the answer? Let’s find out.

Getting Started

So, the first thing to do is get gradle down. The installation instructions on Gradle’s site are pretty clear so there is no need to repeat them here. You will need the Scala plugin though. The bad news is that this plugin does not support ScalaTest out of the box, so let’s have a look at what’s needed.

First off, here’s the build.gradle setup as suggested by Scala’s Gradle plugin page:

repositories {
    mavenCentral()
}

dependencies {
    // Libraries needed to run the scala tools
    scalaTools 'org.scala-lang:scala-compiler:2.7.7'
    scalaTools 'org.scala-lang:scala-library:2.7.7'

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

So far, so good. What we do need, though, is to add ScalaTest to our dependencies:

repositories {
    mavenCentral()
}

dependencies {
    // Libraries needed to run the scala tools
    scalaTools 'org.scala-lang:scala-compiler:2.7.7'
    scalaTools 'org.scala-lang:scala-library:2.7.7'

    // Libraries needed for scala api
    compile 'org.scala-lang:scala-library:2.7.7'
    testCompile 'org.scalatest:scalatest:1.1'
}

After some mucking about it turns out that this isn’t enough, either, as Gradle’s Scala plugin doesn’t support ScalaTest. We need to get in amongst the test phase and make use of ScalaTest’s Ant support in order to get Gradle’s test phase to acknowledge the existence of ScalaTest classes. So, we end up with this:

repositories {
    mavenRepo urls: 'http://scala-tools.org/repo-releases'
    mavenCentral()
}

dependencies {
    // Libraries needed to run the scala tools
    scalaTools 'org.scala-lang:scala-compiler:2.7.7'
    scalaTools 'org.scala-lang:scala-library:2.7.7'

    // Libraries needed for scala api
    compile 'org.scala-lang:scala-library:2.7.7'
    testCompile 'org.scalatest:scalatest:1.1'
}

task test(overwrite: true, dependsOn: testClasses) << {
    ant.taskdef(name: 'scalatest',
        classname: 'org.scalatest.tools.ScalaTestAntTask',
        classpath: sourceSets.test.runtimeClasspath.asPath
    )
    ant.scalatest(runpath: sourceSets.test.classesDir,
        haltonfailure: 'true',
        fork: 'false') {reporter(type: 'stdout')}
}

Thanks to Gradle’s user mailing list for leading me here. There is also an enhancement logged against Gradle to include this support out of the box.

comments powered by Disqus