如何将 jar 打包到 sbt 中的给定目录? [英] How to package jar to a given directory in sbt?
问题描述
在sbt中,如果我们执行package
,会在target/scala-2.12/XXX.jar
处生成一个jar文件.但我想将 jar 文件放在 myDirectory/XXX.jar
.我怎样才能做到这一点?
In sbt, if we execute package
, a jar file will generated at target/scala-2.12/XXX.jar
. But I want to put the jar file at myDirectory/XXX.jar
. How can I make this?
推荐答案
默认路径的 target/
部分由 target
设置键和 定义>scala-2.12/
部分由 crossTarget
设置定义(因为您可能会针对不同版本的 Scala 进行交叉编译).因此,一个简单的解决方案是将其添加到您的 build.sbt
中:
The target/
part of the default path is defined by the target
setting key and the scala-2.12/
part is defined by the crossTarget
setting (because you may cross-compile for different version of Scala). So a simple solution is to add this to your build.sbt
:
crossTarget := baseDirectory.value / "myDirectory"
现在,如果您从 sbt 调用 package
任务,您将获得 myDirectory/
目录中的所有文件,包括通常进入 的 jar、类和其他内容target/scala-2.12/
.
Now if you call package
task from sbt, you will get all files the myDirectory/
directory including the jar, classes and other stuff that normally goes to target/scala-2.12/
.
如果您只想更改 jar 的位置,您可以更改 artifactPath
设置.但是您应该将其设置在正确的范围内并使用 jar 文件名:
If you want to change only the jar's location, you can change the artifactPath
setting. But you should set it in the right scope and with the jar filename:
artifactPath in packageBin in Compile := baseDirectory.value / "myDirectory" / "XXX.jar"
// if you're using sbt 1.1+, you can also write like this:
Compile/packageBin/artifactPath := ...
如果您有一个重要的设置,这很容易变得更加复杂和混乱.所以检查 sbt 来源以了解如何 artifactPath
设置实际上是在 sbt 文档.
This can easily get more complicated and messy if you have a non-trivial setup. So check the sbt sources to learn how artifactPath
setting is actually defined and read about artifacts in the sbt documentation.
这篇关于如何将 jar 打包到 sbt 中的给定目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!