Sbt building distribution for a project - scala

Is there an easy way to create a distribution for a SBT project, which collects all dependency jars into a single directory like lib and the main project jar file refers to all its dependencies using manifest entries? It would be nice if one could define a main class.

This answere says how to do part of what you want. Namely get the managed dependencies into lib_managed directory. Basically just add:
retrieveManaged := true
to your build.sbt file.
Alternatively you could look at a One-jar plugin https://github.com/sbt/sbt-onejar It might do a bit more than you want though.

Related

Why is there a build.gradle and a build.sbt in play framework?

I'm starting to develop a web app with scala. What I don't understand is why we have a build.gradle file and a build.sbt file. There are dependencies defined in both files.
Which one should I use in what case?
The files are equivalent, and that is why they both declare (almost) the same dependencies.
So you can use Gradle or sbt, but you won't need to use both. If you want to use Gradle, just remove build.sbt. If you want to use sbt, just remove build.gradle. They are both present in samples and seeds so that users can choose and see how to configure Gradle/sbt for that kind of project.

Specify plugins in Build.scala

If I want to use a build.scala file in order to build my project (instead of build.sbt)
how do I specify the plugins?
I want to use the sbt-assmbly plugin. and I am aware that I can create a plugins.sb file and do a addSbtPlugin there.
But i want to put all my build logic in build.scala instead of the sbt files.
No, you can't. You can put it in project/project/Build.scala, which would let you avoid .sbt files, but probably isn't what you want.
The thing is, project directory is itself an SBT project, and SBT handles project/*.sbt files and project/project/*.scala files in it just the same as it does on the top level; but by the time it's working with project/Build.scala, it's too late to add plugins.

Is there any way to use SBT's resolver only for subset of artifacts?

Is there any way to define custom resolver that would be used only for subset of artifacts, more specifically to fetch artifacts only with predefined groupId?
For example, project defines a custom FooResolver that should be used only for artifacts with groupId org.foo but all other artifacts should be resolved using the default resolver.
To add unmanaged dependencies to an SBT project, the simplest solution is to just put the jars in the lib folder in your project. All libraries in the lib folder will be in the classpath by default.
If you want to use another folder instead of lib, you can redefine it:
unmanagedBase := // provide a java.io.File here.
If you want to do something more complex: SBT retrieves unmanaged libraries with the unmanagedJars task, so you can always redefine that task (but that would probably be a sign that you're trying to do something too complicated to reasonably use unmanaged dependencies...).

multi-project sbt build - package all dependent JARs in one directory

I have a multi-project SBT build: some projects are dependent on each other, some are dependent on third-party JARs, and there's a "main" project which depends on everything .
When I sbt package it, I get one JAR in each target/ directory.
What I want to achieve is getting all relevant JARs (mine and external) is one directory. Very similar to the way you package a WAR with Maven.
(And to clarify - I'm not interested in an assembled "FAT JAR" that contains all the dependencies in a single file. Just one directory with all JARs in it)
Im not 100% sure about the suprobject dependencies but I think SBT native packager should help you do something like that, and will also provide a start-script for windows and unixes:
http://www.scala-sbt.org/sbt-native-packager/GettingStartedApplications/MyFirstProject.html
I would recommend sbt-pack for creating self-contained JARs:
https://github.com/xerial/sbt-pack
I use it and haven't seen a glitch so far.
It also generates both OS X/Linux as well as Windows .bat entry scripts for the main classes/objects you choose.

SBT: Simplest way to separate plugins.sbt

This is a very simple question, but I surprisingly havn't gotten an answer for it yet.
Simply put, in most non trivial SBT projects you will have a plugins.sbt file that contains plugins that are required to run your project (like a web container plugin if your SBT project is a website). However in the same file (plugins.sbt), plugins which have nothing to do with actually running your project (such as ensime/intellij/eclipse project generators) are also typically placed in plugins.sbt
I have seen this behavior for many SBT projects which are placed into github
This ideally speaking is not the correct way to do things, ideally plugins which have nothing to do with actually running/compiling your project should be in a separate file which is put into a .gitignore
What is the idiomatic SBT way of dealing with this (I assume it should be something that consists of having 2 separate plugins.sbt files, one with actual project plugins and the other with IDE generators and whatnot)
You can install plugins globally by placing them in ~/.sbt/0.13/plugins/. .sbt or .scala files located here are loaded for every project that you have.
You can also use addSbtPlugin() in a .sbt file to add other plugins.
Check out http://www.scala-sbt.org/release/docs/Getting-Started/Using-Plugins.html