Tell SBT to collect all my dependencies together - scala

When building a web application SBT is able to collect all my jar dependencies into the WAR file.
Is this possible to have SBT put all the jars I depend on in my non-web application into a directory so I can easily put them onto my class path when running the app?

Yes, you can put something like this in your project definition class:
val libraryJarPath = outputPath / "lib"
def collectJarsTask = {
val jars = mainDependencies.libraries +++ mainDependencies.scalaJars
FileUtilities.copyFlat(jars.get, libraryJarPath, log)
}
lazy val collectJars = task { collectJarsTask; None } dependsOn(compile)
and run the task via collect-jars in your SBT console. This will copy the scala-library.jar and the jars used for compilation in a directory called lib in the same directory as your classes directory.

In my honest opinion don't bother with sbt-assembly. I'm new with scala but I'm quite technology agnostic, I handle a lot of technologies and sbt-assembly it's not clean. Just an opinion.
I would recommend you sbt-pack. Awesome piece of work. It will give you the runnable scripts as well, for both.. WINDOWS AND LINUX.
https://github.com/xerial/sbt-pack

You can use sbt-assembly to make a fat jar with all dependencies: https://github.com/sbt/sbt-assembly

Related

Import code packaged as jar into another sbt project

I want to import below code packaged as jar to another sbt intellij project how could i do this?
package yyy
object Hello extends Greeting with App {
println(greeting)
}
trait Greeting {
lazy val greeting: String = "hello"
}
If you have this code packaged as a jar, you can simply place it in the lib/ folder of the other sbt project. It should be on the classpath, so you'll be able to do import yyy._ in the code.
You can read more about unmanaged dependencies in the sbt documentation. Here's an excerpt from it:
Unmanaged dependencies work like this: add jars to lib and they will be placed on the project classpath. Not much else to it!
Dependencies in lib go on all the classpaths (for compile, test, run, and console).
There’s nothing to add to build.sbt to use unmanaged dependencies, though you could change the unmanagedBase key if you’d like to use a different directory rather than lib.

How can I send SMS from PC to mobile in scala?

I am working on a live project involving technologies like Scala, Akka and Slick. I have to implement the SMS functionality. After googling I didn't get a single example using Scala, but I got a few using Java and some comments saying that Simplewire is best to implement this functionality. But I am not able to find the library dependencies for any of them. Help me to resolve and implement this simplewire example using Scala.
I tried to start like this,
import com.simplewire.sms._;
object SMSHelper {
def sendSMS = {
val sms = new SMS() // SMS() is not resolving
}
// ...
}
From the sbt documentation:
Unmanaged dependencies work like this: add jars to lib and they will
be placed on the project classpath. Not much else to it!
Download the JAR file from here and simply copy it into the lib folder located at the root of your SBT project then sbt compile and it should work.
If you want to place your libraries in another folder (for instance custom_lib), add this line to your build.sbt file :
unmanagedBase := baseDirectory.value / "custom_lib"

build and executable jar using SBT

I have a simple Scala command line App that I want to package using SBT.
object Transform extends App {
val source = scala.io.Source.fromFile(args(0))
...
}
I can't seem to find anything in the SBT docs or an online example of a SBT configuration/command that would allows me to create a standalone executable jar (java -jar ...) with the appropriate manifest and dependencies included.
I did find SBT Assembly, but it looks to be a plugin for SBT < 0.13.5.
sbt-onejar was created for exactly this use case.

How do I use shared configurations across SBT (Play) multi-projects?

I have several SBT 0.13 / Play 2.2 projects (websites). They are all multi-module as they share some common functionality. This makes their project configuration files both complex and almost identical, but not quite.
I would like to be able to share as much as possible of these configuration files across the projects (frequent play updates makes keeping 5+ websites up to date a royal pain, not to mention all the almost-identical-but-evolving dependency lists across the projects).
build.properties and plugins.sbt are identical across projects and can be overwritten by a simple script. Great.
Build.scala is trickier - I would like to introduce a shared base class like so:
abstract class MyBuildBase extends Build { ... }
that in Build.scala do:
object ApplicationBuild extends MyBuildBuild { ... }
In order for this to make any sense at all, MyBuildBase.scala needs to be shared across projects. This can be done with svn:external, which operates on directories. Which means I need to somehow make this shared directory accessible when Build.scala is compiled (otherwise sbt complains loudly).
Reading http://www.scala-sbt.org/0.13.0/docs/Detailed-Topics/Classpaths.html and http://www.scala-sbt.org/0.13.0/docs/Getting-Started/Full-Def.html it seems like this should be possible.
However, it is exceptionally unclear to me what to actually put in the project/project/Build.scala file to actually achieve this - I can't find an example of "an sbt build file that's intended to build an sbt build file and include some extra source files in the build".
Any suggestions?
What you probably want to do is create a plugin, or shared library.
You can make an sbt project with a build like follows:
build.sbt
sbtPlugin := true
organization := "you"
name := "common-build"
version := "1.0"
Then create in src/main/scala your abstract class "MyBuildBase". Release this project as an sbt plugin.
Then in your other projects, you can use this as a library/plugin. In project/plugins.sbt add:
addSbtPlugin("you" % "common-build" % "1.0")
And this will resolve your common build library when building your build.
If you need more information, look up more about sbt plugins and ignore the part about making something that extends a Plugin. Plugins are just libraries versioned with sbt's version number and your own. You should be able to put whatever code you want in there to share between builds.
Note: in 2016, Build.scala is deprecated for Build.sbt.
Here is the new (Dec. 2016) multi-module with App Scala sbt template by Michael Lewis.
Usage
sbt new lewismj/sbt-template.g8
You can then run:
sbt compile
sbt publish-local
sbt assembly
It is based on Scala SBT template (Library)
This giter8 template will write SBT build files for a Scala library.

Compile scala files from a sbt plugin

I am developing a sbt plugin. In this plugin I generate some new scala sources packaged in a sbt project. Then I need to compile these new files programaticaly so that I could add the generated class in my classLoader.
I do not find any way to compile programaticaly sources from a given sbt project path (and eventually from a classLoader) in the sbt API, something as simple as the sbt command (sbt compile) line would be very convenient, something like:
XXX.compile(path/to/sbt/project)
Thanks
I suggest you have a look at sbt-boilerplate which is an sbt plugin that generates code, works well and is really simple.
Here's a link to the file that you probably want to take a look at