adding external url jar as an sbt unmanaged dependency - scala

I am working on an sbt project and I need to pull a jar, available via url, into my project as an unmanaged dependency.
As an example:
I want to retrieve a jar file available at
http://www.sourceGorge.net/fooProject/v4.2/lib/bar.jar. If the jar file is absent from myProject/lib then simply download the source jar, do nothing otherwise.
One possible solution I found on the web suggests the following:
libraryDependencies += "net.sf.proguard" % "proguard-base" % "5.0"
from "file:///tmp/proguard5.0beta2/lib/proguard.jar"
But my question would then be: what am I supposed to substitute for "net.sf.proguard", "program-base", and "5.0" if all I have is a url of the form specified in the above example?
Thank you in advance, and happy holidays if this is your holiday season.

If you're pulling the jar from a url, it doesn't matter what you call it--preferably something informative. sbt uses the organization/artifact/version to resolve dependencies from a repository. If you're providing an exact url, then it doesn't really matter.
For example, I can resolve joda-time like this, and it works just fine:
libraryDependencies += "doesn't" % "matter" % "2.1" from "http://central.maven.org/maven2/joda-time/joda-time/2.1/joda-time-2.1.jar"
It also uses the organization/artifact/version combination to store the jar in a specific directory in your local ivy repository. The above line downloaded the jar to:
~/.ivy2/cache/doesn't/matter/jars/matter-2.1.jar

Related

Scala SBT Inline Dependency Declaration

Scala SBT manual apparently says that there is some "lightweight alternative to Ivy" called "inline declarations"
http://www.scala-sbt.org/0.13/docs/Library-Management.html
I have a single jar file ( printer_2.11-1.0.jar ) with no dependencies or imports what so ever that I created with "sbt package". I want to get this jar to a place on the internet where I can include it into my future projects as soon as possible. I don't know how to use Ivy or Maven. Can someone give me a solution that can get my jar listed as fast as possible?
Solved:
libraryDependencies += "printer" % "printer" % "2.11" from "https://dl.dropboxusercontent.com/u/214507961/printer_2.11-1.0.jar"
^ The jar file is posted to a public dropbox folder.
You are misreading the manual. Going to section Inline Declarations. The text reads:
Inline declarations are a basic way of specifying the dependencies to be automatically retrieved. They are intended as a lightweight alternative to a full configuration using Ivy.
This is an example inline definition of a dependency:
libraryDependencies += groupID % artifactID % revision
which will be normally resolved through Ivy. The sentence does not say 'lightweight alternative to Ivy', it means:
They are intended as an Ivy based automatically resolved lightweight alternative to specifying a full configuration manually.
The latter being:
Manually managing dependencies involves copying any jars that you want to use to the lib directory.
This is the solution...
libraryDependencies += "printer" % "printer" % "2.11" from "https://dl.dropboxusercontent.com/u/214507961/printer_2.11-1.0.jar"
^ The jar file is posted to a public dropbox folder. This jar file makes print statements easier to locate.

SBT Plugin in an unmanaged jar file

The requirement: an SBT plugin code is in an unmanaged jar, for example in lib/unmanaged-sbt-plugin-0.0.1.jar.
I was expecting the jar file would be picked up by SBT automatically and the plugin would work out-of-the-box, i.e.: the tasks would be accessible from SBT command line, but that is not the case.
The jar file has the sbt/sbt.autoplugins file in it and the plugin works if pulled from a remote repo into the local one and imported via addSbtPlugin(...). Please note I cannot do that - it's a requirement to get it to load from the lib/unmanaged-sbt-plugin-0.0.1.jar and not from the local/remote repo.
Putting the following line in the build.sbt doesn't make the plugin work (there's not error either):
unmanagedJars in Compile += file("lib/unmanaged-sbt-plugin-0.0.1.jar")
The implementation of addSbtPlugin(...) is simply (according to http://www.scala-sbt.org/0.12.2/docs/Getting-Started/Using-Plugins.html):
def addSbtPlugin(dependency: ModuleID): Setting[Seq[ModuleID]] =
libraryDependencies <+= (sbtBinaryVersion in update, scalaBinaryVersion in update)
{ (sbtV, scalaV) => sbtPluginExtra(dependency, sbtV, scalaV) }
I'm wondering if the above info can be used to resolve my issue?
Thank you in advance!
So you can specify an explicit URL for library dependencies (ModuleID):
addSbtPlugin("org.my-org" % "unmanaged-sbt-plugin" % "0.0.1"
from "file:///./lib/unmanaged-sbt-plugin-0.0.1.jar")
Have been struggling to get this to work.
Could not get it to work with proposed solution using from "file://.." (using sbt 1.0.4).
Got it to work by putting the plugin in project/lib folder and adding all the plugin dependencies to plugins.sbt using libraryDependencies ++= Seq(..) like in build.sbt. You can find the plugin dependencies by looking at the plugin pom file, usually in .ivy2/local/<org>/<pluginname>/poms folder.

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.

sbt constructs wrong path for specific / timestamped snapshot versions

Background
I am developing a Play 2.2.3 application; the Play build system uses sbt.
I am using Sonatype Nexus OSS to manage various libraries built and deployed by Maven. At the moment, I am deploying snapshot versions of those libraries. For instance:
<groupId>myGroup</groupId>
<artifactId>myArtifact</artifactId>
<version>0.1-SNAPSHOT</version>
The Play application depends on snapshot versions of those libraries.
Intention
In the Play project, I want to use a specific (thus, "timestamped") snapshot version of a dependency -- say 0.1-20140509.053703-1 -- instead of the latest one as specified by 0.1-SNAPSHOT.
Issue
While the latter works perfectly well, sbt seems to be unable to resolve the specific version. At least sbt constructs the URL pointing to that specific version faultily: The name of the directory that contains the artifact ends with the actual timestamp instead of string SNAPSHOT.
Example
I created a new Play application and added the following dependency in file build.sbt: libraryDependencies += "myGroup" % "myArtifact" % "0.1-20140509.053703-1"
When resolving that dependency, sbt constructs URLs ending with myGroup/myArtifact/0.1-20140509.053703-1/myArtifact-0.1-20140509.053703-1.pom.
Note that the 3rd path component is 0.1-20140509.053703-1. Thus, the directory containing artifact myArtifact incorrectly contains the timestamp as well.
The correct directory name would be 0.1-SNAPSHOT; thus, using SNAPSHOT instead of the actual timestamp. The complete correct URL would thus end with myGroup/myArtifact/0.1-SNAPSHOT/myArtifact-0.1-20140509.053703-1.pom.
Referring to the latest snapshot version instead by using dependency entry libraryDependencies += "myGroup" % "myArtifact" % "0.1-SNAPSHOT" works. The URLs end with 0.1-SNAPSHOT/myArtifact-0.1-SNAPSHOT.pom.
Question
How could I persuade sbt to download a specific snapshot version?

How do I add a dependency to an SBT build file

I have an SBT build file like with lots of lines like this:
"org.apache.mahout" % "mahout-math" % "0.5"
Specifying dependencies. I have a new Jar that I want to add to my dependencies. How do I figure out this "blah" % "blah" % "blah.0" form I should write it in?
I know this is probably a very basic question. Thank you in advance for your help.
If the dependency you want is not available into a repository you can just put the jar a "lib" folder in the root of your project, otherwise like any dependency you should get the information (groupId, name, version and repository) of the artifact (try searching for 'maven' + library name in google).
Everything is explained in great detail in the SBT documentation.
for basic java libraries, a search in MVN Repository should quickly yield the pom if are looking for. Rules about SBT resolution, particularly in Scala Crosscompiled projects, please refer to SBT