Scala sbt file dependency from github repository - scala

Is it possible to include a dependency from github?
The repository does not have a jar file, just a build.sbt file and a source folder.

You can create a new project which points to the source in your build.sbt and then use dependsOn:
lazy val projectIDependOn = RootProject(uri("git://github.com/user/Project.git"))
lazy val myProject = project in file("my-project").dependsOn(projectIDependOn)
An alternative approach would be to clone to github repository and then use sbt-assembly to create an uber JAR you can use, but that requires a bit more work.

Related

Publish single 3rd party jar to local repository in sbt

I have a project which depends on a 3rd party jar SomeJar.jar.
How can I make a specific sub project cause this jar to be published to local repository before the sub project runs its own compile?
In the example below, somejar-common needs to first be published to local repo.
lazy val subproj1 = (project in file("subproj1"))
.settings(libraryDependencies += "org.someorg" % "somejar-common" % "1.0.0") // This one needs to be deployed first to local repo
If your (sub)project depends on a fixed jar, you don't need to publish it locally to work with it. You can add it as an unmanaged dependency: just put it in your (sub)project's lib/ subfolder.
See sbt documentation on unmanaged dependencies.

How to use a file based repository in sbt?

In sbt, I want to publish a project's artifacts to a local folder and then use those artifacts in another project.
In project A, I can publish the artifacts using this:
publishTo := Some(Resolver.file("file", new File( Path.userHome.absolutePath + "/myMavenRepo/releases" )) )
followed by sbt publish. I am able to see the .jar file and other artifacts in the myMavenRepo/releases/ folder.
However, when I add this to project B:
Resolver.file("myRepo", file(Path.userHome.absolutePath + "/myMavenRepo/releases")) transactional()
... then the project A dependency is not resolved.
Am I using these APIs incorrectly?
By the way, I don't want to use publishLocal because these are experimental projects and don't want them to pollute the repository under ~/.sbt/ folder.

How to add one SBT project as a dependency of another?

I have the following project setup:
Base/
build.sbt
src/
Main/
build.sbt
src/
Where Base and Main are two projects. I would like Main to have the classes of Base on the classpath of Main. If possible, I would like to keep the builds separate. How can I do this?
Thanks!
You can try Multi-Project build, maybe it will be best in you case: http://www.scala-sbt.org/0.13.5/docs/Getting-Started/Multi-Project.html
However if this two projects are completely separate sbt supports source dependencies, it definitely works with github and I think should work with file dependencies as well
lazy val Main = Project("Main", file("."), settings = ...) dependsOn(baseDep)
lazy val baseDep = uri("file:///path/to/base/project")

How to create standalone jar file for elastic4s from sources?

I'm trying to create a standalone jar file from the elastic4s sources on github. I've used sbt compile as detailed on the github page, but I can't find the jar file.
How do I use sbt to create the jar file so I can import it into my projects as a dependency?
The compile task will only compile the project.
> help compile
Compiles sources.
If you want to create a jar file and use it as a dependency in your project, you have two ways of doing that.
Unmanaged dependency (not recommended)
Unmanaged dependency run +package, which will create a jar file for each supported scala version, which you can use in your projects as an unmanaged dependency. Copy the package-generated jar to lib folder in your project.
The jar files will be located in target/scala-2.11 and target/scala-2.10, depending on the Scala version you want to use it with.
Publish to Local Repository (recommended yet imperfect)
If you want to include your custom-built elastic4s, as a managed dependency, you have to run +publishLocal. This will do the same as above, but additionally it will publish the artifact to your local repository. Assuming you've built it with version := "1.2.1.1-SNAPSHOT", you can include it in your project by just adding:
libraryDependencies += "com.sksamuel.elastic4s" %% "elastic4s" % "1.2.1.1-SNAPSHOT"
What makes the approach imperfect is that once you shared the project on GitHub (or any other project sharing platform), people will have to do publishLocal themselves to be able to build your project. The dependency should therefore go to one of the official binary repositories so when a dependency is needed, it's downloaded from Internet. Consult Publishing.
What is the + character in front of the commands
The + in the commands is for cross-building, if you don't use it the command will be executed only using scalaVersion declared in the build.sbt of the project.

Can multi-projects from GIT be used as SBT dependencies?

I would like to use banana-rdf in my project, ideally by defining it as a dependency in a build.scala using dependsOn:
lazy val root = Project("root", file(".")) dependsOn RootProject(uri("git://github.com/w3c/banana-rdf"))
However, banana-rdf is a multi-project so needs to be composed differently. From what I can see, these multi-project definitions only allow you to specify project locations as file paths, and won't allow URIs.
Question: Am I right in saying that I have to clone these multi-project GIT dependencies into my project and reference them as folders?
I rather like the idea of leaving all the GIT cloning up to SBT, and having these cloned in some tmp SBT folder rather than cluttering up my project...
I depend on Banana RDF subprojects all the time with ProjectRef, like this:
lazy val core: Project = Project(
...
).dependsOn(
ProjectRef(uri("git://github.com/w3c/banana-rdf.git"), "banana-jena")
)
One especially nice part is that you can just tack a commit or branch name as a fragment identifier on the URI and everything works exactly as you'd expect.