如何从scala制作一个jar文件 [英] How to make a jar file from scala
问题描述
我在线阅读过可以从sci代码创建一个可以从cli运行的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")
到你的项目/ 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屋!