scala 如何从scala制作jar文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30576222/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make a jar file from scala
提问by eddard.stark
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.
我在网上读到可以从可以从 cli 运行的 Scala 代码中创建一个 jar 文件。都写了下面的代码。我如何从中制作一个 jar 文件?我正在使用 sbt 0.13.7。
object Main extends App {
println("Hello World from Scala!")
}
采纳答案by shutty
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.sbtfile in root folder:
为了能够使用 Scala 执行复杂的构建任务,您必须使用 SBT 作为构建工具:它是创建应用程序包的默认 Scala 方式。要将 SBT 支持添加到您的项目,只需build.sbt在根文件夹中创建一个文件:
name := "hello-world"
version := "1.0"
scalaVersion := "2.11.6"
mainClass := Some("com.example.Hello")
To build a jar file with your application in case if you have no external dependencies, you can run sbt packageand it will build a hello-world_2.11_1.0.jarfile 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.
如果您没有外部依赖项,要使用您的应用程序构建 jar 文件,您可以运行sbt package它,它将hello-world_2.11_1.0.jar使用您的代码构建一个文件,以便您可以使用java -jar hello-world.jar. 但是您肯定必须在代码中包含一些依赖项,至少因为 Scala 运行时。
Use sbt-assemblyplugin to build a fat jar with all your dependencies. To install it, add a line
使用sbt-assembly插件构建一个包含所有依赖项的胖 jar。要安装它,请添加一行
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.12.0")
to your project/plugins.sbtfile (and create it if there's no such file) and run sbt assemblytask from console.
到您的project/plugins.sbt文件(如果没有这样的文件,则创建它)并sbt assembly从控制台运行任务。
回答by elm
In addition to sbt, consider also this plain command line,
此外sbt,还要考虑这个简单的命令行,
scalac hello.scala -d hello.jar
which creates the jar file. Run it with
这将创建 jar 文件。运行它
scala hello.jar
Also possible is to script the source code by adding this header
也可以通过添加此标头来编写源代码脚本
#!/bin/sh
exec scala -savecompiled "##代码##" "$@"
!#
and calling the main method with Main.main(args)(note chmod +x hello.shto make the file executable). Here savecompiledwill create a jar file on the first invocation.
并使用Main.main(args)(注意chmod +x hello.sh使文件可执行)调用 main 方法。这里savecompiled将在第一次调用时创建一个 jar 文件。
回答by codejitsu
You can try this SBT plugin: https://github.com/sbt/sbt-native-packager
你可以试试这个 SBT 插件:https: //github.com/sbt/sbt-native-packager
I created Linux Debian packages with this plugin (Windows MSI should be possible as well).
我用这个插件创建了 Linux Debian 包(Windows MSI 也应该是可能的)。
回答by Mikhail Kobenko
sbt: publishLocalthen go to the target folder, probably called scala-2.**
sbt: publishLocal然后转到目标文件夹,可能称为 scala-2.**

