如何从scala制作jar文件 [英] How to make a jar file from scala

查看:53
本文介绍了如何从scala制作jar文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网上读到可以从 cli 运行的 Scala 代码创建一个 jar 文件.都写了下面的代码.我如何从中制作一个 jar 文件?我正在使用 sbt 0.13.7.

I have read online that it is possible to create a jar file from scala code that could be run from the cli. All have written is the following code. How do I make a jar file from it? I am using sbt 0.13.7.

object Main extends App {
    println("Hello World from Scala!")
}

推荐答案

为了能够使用 Scala 执行复杂的构建任务,您必须使用 SBT 作为构建工具:它是创建应用程序包的默认 Scala 方式.要将 SBT 支持添加到您的项目,只需在根文件夹中创建一个 build.sbt 文件:

To be able to perform complex build tasks with Scala, you have to use SBT as a build tool: it's a default scala-way of creating application packages. To add SBT support to your project, just create a build.sbt file in root folder:

name := "hello-world"

version := "1.0"

scalaVersion := "2.11.6"

mainClass := Some("com.example.Hello")

如果您没有外部依赖项,要使用您的应用程序构建 jar 文件,您可以运行 sbt 包,它将构建一个 hello-world_2.11_1.0.jar 文件包含您的代码,以便您可以使用 java -jar hello-world.jar 运行它.但是你肯定必须在你的代码中包含一些依赖项,至少因为 Scala 运行时.

To build a jar file with your application in case if you have no external dependencies, you can run sbt package and it will build a hello-world_2.11_1.0.jar file with your code so you can run it with java -jar hello-world.jar. But you definitely will have to include some dependencies with your code, at least because of a Scala runtime.

使用 sbt-assembly 插件构建一个包含所有依赖项的胖 jar.要安装它,添加一行

Use sbt-assembly plugin to build a fat jar with all your dependencies. To install it, add a line

addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.12.0")

到您的 project/plugins.sbt 文件(如果没有这样的文件,则创建它)并从控制台运行 sbt assembly 任务.

to your project/plugins.sbt file (and create it if there's no such file) and run sbt assembly task from console.

这篇关于如何从scala制作jar文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆