Gradle init project for Scala application

I want to create a skeleton console app for Scala i.e. a single entry class with a main function that prints "Hello world".

I was able to create a Scala library init project by executing:

gradle init --type scala-library

however there seems to be no scala-application, running:

gradle init --type scala-application

The requested build setup type 'scala-application' is not supported. Supported types: 'basic', 'groovy-application', 'groovy-library', 'java-application', 'java-library', 'pom', 'scala-library'.

Is there no Scala console app template for Gradle?

Upvotes: 13

Views: 2561

Answers (3)

Philippe
Philippe

Reputation: 2007

As of Gradle 6.7 the scala-application build type exists now.

Upvotes: 1

bobah
bobah

Reputation: 18864

On top of what you have for Java you pretty much only need to add apply plugin: 'scala' and change Main to be:

object Main extends App {
    println('Hello, World!')
}

I ended up doing just that.

Additionally, if you want joint compilation (and have compiler understand Java and Scala in the same place) you can add the below snippet (one block per source set: main, test, jmh, etc.):

sourceSets.main {
    java.srcDirs = []
    scala.srcDirs = ['src/main/scala', 'src/main/java']
    scala.include '**/*.*'
}

sourceSets.test {
    java.srcDirs = []
    scala.srcDirs = ['src/test/scala', 'src/test/java']
    scala.include '**/*.*'
}

Upvotes: 2

voxoid
voxoid

Reputation: 1289

No, there is no scala application template, however it is easy to make your generated "scala-library" project into a scala application in two steps:

  1. Create your main class and method under a package under src/main/scala
  2. Add two lines to your gradle.build, to add the 'application' plugin and specify your main class.

For example, you add src/main/scala/com/example/myapp/Main.scala:

package com.example.myapp

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

Then to your gradle.build you add:

apply plugin: 'application'

mainClassName='com.example.myapp.Main'

Upvotes: 5

Related Questions