Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to package jar to a given directory in sbt?

Tags:

scala

sbt

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?

like image 793
蘇哲聖 Avatar asked Mar 19 '18 07:03

蘇哲聖


1 Answers

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"

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/.

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 := ...

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.

like image 58
laughedelic Avatar answered Nov 01 '22 01:11

laughedelic