2

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.

eNaught
  • 81
  • 6
  • 3
    **metals** is just a Language Server Protocol, it is only useful for your IDE. But your project still should _(must?)_ have a proper build tool to define your project structure, dependencies and manage task like generating jars. You may want to take a look to common options like **sbt**, **maven** or **mill**. – Luis Miguel Mejía Suárez Jun 02 '21 at 20:07
  • 1
    @LuisMiguelMejíaSuárez thank you for your fast response. You are correct about metals and I realize that my question may not have been very clearly worded. The metals VSCode plugin uses bloop or sbt-bsp as the build server internally. My question is about if and how I can add the sbt-assembly extension to it and trigger the assembly task. I'll update the question to be more clear. – eNaught Jun 03 '21 at 00:40
  • 1
    I still don't think the question is clear. You want Metals to automatically generate a JAR every time it compiles your code? – sinanspd Jun 03 '21 at 00:51
  • If you are using **sbt** then just add the plugin to your build as explained in the docs adding a line like `addSbtPlugin("foo" % "bar" % "version")` to the `project/plugins.sbt` file. After that you can generate an assembly file running the `sbt assembly` command. If you question is how to run that command from **vscode** I believe it is not possible. - Anyways, since you mentioned **Spark** remember that creating fat jars for **Spark** is tricky, you need to ensure same versions _(even patches)_ for **Scala** and **Spark** and exclude those from the final jar. – Luis Miguel Mejía Suárez Jun 03 '21 at 13:01

3 Answers3

1

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.
eNaught
  • 81
  • 6
0

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.

Tanner
  • 548
  • 8
  • 20
0

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

Cole Smith
  • 150
  • 1
  • 7