Gavin Niu
Gavin Niu

Reputation: 1335

How to compile scala into runnable jar file

I didnt find a good way to compile scala in Linux so I tried following commands,

mkdir runnablescala
cd runnablescala
mkdir -p src/main/scala
mkdir -p src/main/resources
mkdir -p src/test/scala
mkdir -p src/test/resources
cd src/main/scala
mkdir -p com/johnathanmarksmith/gradle
vi com/johnathanmarksmith/gradle/HelloWorld.scala

package com.johnathanmarksmith.gradle;
 object HelloWorld {
    def main(args: Array[String]) {
      println("Hello, world!")
    }
  }

cd ../../..
vi build.gradle

apply plugin: 'scala' 

 jar { 
        baseName = 'smith' 
        version = '1.0' 
        manifest { 
                     attributes 'Main-Class': 'com.johnathanmarksmith.gradle.HelloWorld' } 
     }
gradle build

The build failed with this result

:compileJava UP-TO-DATE
:compileScala FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':compileScala'.
> 'compileScala.scalaClasspath' must not be empty

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Can anyone tell me how to compile scala? thanks! Do I need anyother plug in or something?

Upvotes: 2

Views: 5221

Answers (1)

Alexey Romanov
Alexey Romanov

Reputation: 170733

https://docs.gradle.org/current/userguide/scala_plugin.html:

Unless a task's scalaClasspath is configured explicitly, the Scala (base) plugin will try to infer it from the task's classpath. This is done as follows:

If a scala-library Jar is found on classpath, and the project has at least one repository declared, a corresponding scala-compiler repository dependency will be added to scalaClasspath.

Otherwise, execution of the task will fail with a message saying that scalaClasspath could not be inferred.

i.e. you need to add

dependencies {
  compile 'org.scala-lang:scala-library:2.11.7'
}

to build.gradle. If you want to build a stand-alone jar, see Building a uberjar with Gradle.

As Rex Kerr mentions, if you don't have a specific reason to use Gradle, I'd go with SBT for a Scala project.

Upvotes: 4

Related Questions