Build a jar file with Scala-Metals on VSCode

2.2k views Asked by At

I'm attempting to develop for Scala and Spark using VSCode and the metals extension. My setup is using a dev container following the instructions (here). I now have my code running and I need to build a jar file to deploy to Spark. Is there a way to do this within the metals extension?

I have read about using sbt-assembly to build a fat jar but I'm unsure of how to integrate this with my setup. Any help would be greatly appreciated.

Edit

To be more clear, the metals VSCode extension uses bloop or sbt-bsp as the build server and I have configured it to use the sbt server. My question is if and how I can get the sbt-assembly extension integrated with it.

3

There are 3 answers

0
Tanner On

sbt package after sbt compile should generate the result you want.

You can find more about generating the JAR here and then running the JAR here.

0
eNaught On

I found the answers to my question, thank you to everyone for your help.

  • Metals extension for VSCode does not provide any way to run sbt tasks that are not already exposed in the build commands window.
  • I was able to run sbt in the terminal window of VSCode whith Metals running, as long as Metals is configured to use bloop instead of sbt. If Metals is configured to use sbt then the sbt server is already running and I was unable to create a new instance or get a reference to the existing instance.
0
Cole Smith On

For anyone stumbling across this question still, I just wasted a few hours looking through everything. There are two types of Jar files:

  1. Dependency jar files: ones you add to your project to use their code)
  2. Standalone (Fat) jar files: ones you execute directly, either by double clicking or by running via command line.

In the first case, you can simply use sbt package and that will generate a package for you. Any dependencies in the project will simply become dependencies for that jar, and you'll be able to import it into your project.

In the second case, you'll need to install a tool like sbt-assembly which will allow you to download your dependencies and include them in the packaged jar you generate. This will let your code run from anywhere. To do this, you'll need to add addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "1.0.0") (or whatever the newest version is) to your plugins.sbt file, where you can then re-import your code into VSCode and then run sbt assembly from the terminal. There is no way I have found to make this process integrate with a button on VSCode, but it's simple enough to run a single command.

Hope this helped yall