Scala Working With Gradle
Examples
Basic Setup
1.Create a file named SCALA_PROJECT/build.gradle with these contents:
group 'scala_gradle'
version '1.0-SNAPSHOT'
apply plugin: 'scala'
repositories {
jcenter()
mavenCentral()
maven {
url "https://repo.typesafe.com/typesafe/maven-releases"
}
}
dependencies {
compile group: 'org.scala-lang', name: 'scala-library', version: '2.10.6'
}
task "create-dirs" << {
sourceSets*.scala.srcDirs*.each { it.mkdirs() }
sourceSets*.resources.srcDirs*.each { it.mkdirs() }
}
2. Run gradle tasks to see available tasks.
3. Run gradle create-dirs to create a src/scala, src/resources directory.
4. Run gradle build to build the project and download dependencies.
Create your own Gradle Scala plugin
After going through the Basic Setup example, you may find yourself repeating most part of it in every single Scala Gradle project. Smells like boilerplate code...
What if, instead of applying the Scala plugin offered by Gradle, you could apply your own Scala plugin, which would be responsible for handling all your common build logic, extending, at the same time, the already existing plugin.
This example is going to transform the previous build logic into a reusable Gradle plugin.
Luckyly, in Gradle, you can easily write custom plugins with the help of the Gradle API, as outlined in the documentation. As language of implementation, you can use Scala itself or even Java.