scala - 如何将scala编译成可运行的jar文件

标签 scala gradle

我没有找到在 Linux 中编译 scala 的好方法,所以我尝试了以下命令,

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

构建失败并出现此结果

: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

谁能告诉我如何编译scala?谢谢!我需要其他插件吗?

最佳答案

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.

即您需要添加

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

build.gradle。如果你想构建一个独立的 jar,请参阅 Building a uberjar with Gradle

正如 Rex Kerr 提到的,如果您没有使用 Gradle 的具体原因,我会在 Scala 项目中使用 SBT。

关于scala - 如何将scala编译成可运行的jar文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33291506/

相关文章:

scala - Clojure 的 'let' 在 Scala 中等效

json - 在 Play 2 中如何检查 JsValue 变量是否为 NULL?

gradle - 使用特定配置运行插件任务

java - SpringBoot项目gradle build 'bootRepackage'任务失败,因为无法重命名jar

scala - Play Framework 2.0。升级骨骼cp

Scala - 可以扩展的抽象基类中的参数类型

scala - 我如何根据 Scala 中的模式匹配在 map 中找到一个键

java - Gradle:下载gradle需要 "very long time",activity_main.xml一直显示 "Loading"

java - Gradle Run throw “Error: Could not find or load main class”

android - gradle 获取系统环境变量