2

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?

Suma
  • 33,181
  • 16
  • 123
  • 191
Gavin Niu
  • 1,315
  • 4
  • 20
  • 27
  • 3
    Do you mean using Gradle specifically? If yes, you should add the gradle tag. If no, SBT is usually easiest to get off the ground. – Rex Kerr Oct 22 '15 at 22:08
  • `scalac`? http://www.scala-lang.org/old/node/166 – Paul Oct 22 '15 at 22:09
  • I took the liberty to change tags, because that's a Gradle error, not a compiler error. – Alexey Romanov Oct 23 '15 at 07:09
  • @RexKerr Is there any tutorial for using sbt to compile scala code? I've never use it before. Thanks! – Gavin Niu Oct 23 '15 at 14:08
  • @GavinNiu - http://www.scala-sbt.org/documentation.html is one source. If you just want to compile one file, http://www.scala-sbt.org/0.13/tutorial/Hello.html is an example of that. If you want to build a jar file with everything needed on the classpath (and then some--it's not for optimizing, just convenience), search for the assembly plugin. – Rex Kerr Oct 23 '15 at 14:13
  • @RexKerr Thanks so much! – Gavin Niu Oct 23 '15 at 14:26
  • @RexKerr I tried tong@tong-VirtualBox:/usr/local/jars/hello/hello$ addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.0") bash: syntax error near unexpected token `"com.eed3si9n"' How can I modify the command – Gavin Niu Oct 23 '15 at 15:10
  • @GavinNiu - It's not a bash shell command! You have to put the command in the file `project/assembly.sbt`! – Rex Kerr Oct 23 '15 at 23:16

1 Answers1

4

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.

Community
  • 1
  • 1
Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487