How to import directory of .scala files in sbt (unmanaged)? - scala

I have a directory that I want to use as a dependancy for an sbt project however its comprised of .scala files which means I can't place it inside a jar (from what I understand) so then how can I use it as a dependancy for my project?
(sbt doesn't have it as a managed dependancy)
thanks in advance

as written in the manual, you can customize the sources (or source directories) pretty freely. by default, sbt will expect to have scala and java sources under a source directory.
you can customize that too. depending on your exact use case, maybe you want these sources under a different configuration? if it's just extra sources to compile and package, you can simply use:
sourceDirectories in Compile += file("/path/to/your/sources")
or:
unmanagedSourceDirectories in Compile += file("/path/to/your/sources")
use the first when the sources are managed, e.g: if these sources are generated by some other program, or retrieved as a dependency, etc'...
use the second when these are plain sources not managed by anything.

Related

Adding library dependency in play 2.3.8

I'm trying to add the apache commons email library to my Play project and I'm having trouble.
Firstly I have both build.sbt and plugins.sbt in my project and I'm not sure which one I should be putting the import into, does anyone know?
Also, I'm not sure why there even is the separate project module in my project, intelliJ created it as part of the project. Could anyone explain the purpose of the two separate modules and why they are there?
Thanks!
So, in sbt, you have your project. This is specified in build.sbt (or more correctly, any *.sbt file in your projects base directory). Any libraries that your applications code needs, for example, if your application needs to send emails using the commons email library, go in to the librarDependencies seeing in here.
But build.sbt itself is Scala code that needs to be compiled, but it's not part of your applications runtime. So in sbt, your projects build is a project itself, one that has to be compiled. It has its own classpath, which consists of the sbt plugins you're using, so for example, if you need a less compiler to compile your less files, that's not something that gets done at runtime, so you don't want your application code depending on that, it goes into your project builds libraryDependencies, which gets specified in project/plugins.sbt (or in fact any *.sbt in the project directory). So, once you add it there, you can use the Scala code it provides from build.sbt. IntelliJ imports this project for you so that you can have syntax highlighting and other IDE features in build.sbt.
But it doesn't stop there. How does project/plugins.sbt get compiled, where is its classpath? Well, your projects builds projects builds project is also an sbt project itself too... It keeps going down. IntelliJ stops at that point though, it doesn't keep importing these meta sbt projects because it's actually very rare to need additional sbt plugins for your projects builds projects builds project, so it just uses the same classpath as your projects build project for syntax highlighting in project/plugins.sbt.

Generate a JAR from one Scala source file

I have no Scala experience, but I need to create a JAR to include on a project's classpath from a single Scala source file.
I'm thinking there is a relatively straightforward way to do this, but I can't seem to figure it out.
The Scala file is here: http://pastebin.com/MYqjNkac
The JAR doesn't need to be executable, it just needs to be able to be referenced from another program.
The most convenient way is to use some build tool like Sbt or Maven. For maven there is the maven-scala-plugin plugin, and for Sbt here is a tutorial.
If you don't want to use any build tool, you may want to compile the code with scalac and then create the jar file manually by using zip on the resulting class files and renaming it to jar. But you have to preserve the directory structure. In your pastebin you use the package org.apache.spark.examples.pythonconverters, so make sure the directories match.
Btw, if you want to just integrate this piece of code with your java project, and using maven, you can have the scala code in your 1 project as well (in src/main/scala). Just use the maven-scala-plugin plugin and hook it to the compile phase, or some sooner phase if your Java code depends on it. However, I don't recommend mixing multiple languages in one project, I would split it into two separate ones.

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

How do I get sbt to not try to compile a directory whose name ends in .scala as a Scala source file?

I am trying to convert an existing Maven multi-module project over to use sbt instead. One of the module subdirectories has .scala at the end of its name (because it's a pure Scala implementation of a library that was originally written in Java). Maven had no problem with this. However, sbt sees the .scala and thinks that the directory is a Scala source file and tries to compile it, which fails, of course.
How can I configure sbt so that it doesn't try to compile a directory as a source file? Note that the top-level (root) project doesn't contain any source code itself, so disabling compilation altogether at that level would be an acceptable solution (as long as it doesn't prevent the submodules from being compiled).
Yes, I have considered simply renaming the submodule directory so that it doesn't contain .scala, but I would prefer to avoid restructuring my build tree if possible.
Let's say the name of your directory is ./directory.scala (located in the root of the project). Then this should do the trick:
excludeFilter in Compile := "directory.scala"

Sbt building distribution for a project

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.