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.
Related
I have created basic Scala Play application with https://www.playframework.com/getting-started play-scala-seed. This project compiles and runs with sbt run. But I have another Scala project that compiles and runs and which I have submitted to my local Ivy repository with command sbt publishLocal. This other project was saved at C:\Users\tomr\.ivy2\local\com.agiintelligence\scala-isabelle_2.13\master-SNAPSHOT as a result of this command.
Then I imported (exactly so - imported, no just opened) my Play project in IntelliJ and I used Project - Open Module Settings - Project Settings - Libraries to add com.agiintelligence jar from my ivy2 location. After such operations IntelliJ editor recognizes com.agiintelligence classes. That is fine.
But when I am trying to run my Play application with sbt run, I experience the error message not found: object com that is exactly when compiling import com.agiintelligence line in my Scala controller file of Play application.
Of course - such error has been reported and resolved with, e.g. object play not found in scala application
But that solution suggests to append build.sbt file. My build.sbt file is pretty bare:
name := """agiintelligence"""
organization := "com.agiintelligence"
version := "1.0-SNAPSHOT"
lazy val root = (project in file(".")).enablePlugins(PlayScala)
scalaVersion := "2.13.5"
libraryDependencies += guice
libraryDependencies += "org.scalatestplus.play" %% "scalatestplus-play" % "5.0.0" % Test
// Adds additional packages into Twirl
//TwirlKeys.templateImports += "com.skaraintelligence.controllers._"
// Adds additional packages into conf/routes
// play.sbt.routes.RoutesKeys.routesImport += "com.skaraintelligence.binders._"
My Play application contains (as can bee seen from the IntelliJ project pane) some tens of 'external libraries' (it shows my com.agiintelligence jar as well), but why should I add my own ivy2 library in build.sbt file if no other libraries are listed here? What is different with my library? It is on my computer, in the repository as expected already?
Of course, I can try to add it build.sbt and issue sbt update and see what happens, but I can not understand this logic? Can someone explain it and provide some clue to intelligible solution of my error message?
My Play application contains (as can bee seen from the IntelliJ project pane) some tens of 'external libraries'
Those are probably just transitive dependencies of your Play dependency, that is why sbt downloaded all of them and put them in your classpath so you could use them without you needing to tell it about them; because the pom of Play already did.
It is not that the build tool or the IDE magically added all those dependencies for you because they read your mind and magically understood you wanted them. And that for some reason the magic stopped working for your own library.
Why it is not sufficient to list it Project-Setting--External Libraries in IntelliJ only?
That is sufficient for the IDE to work, but not for the build tool. The build tool is independent of the IDE; it doesn't know about it. sbt just knows about the dependencies you configured in your definition file.
Even more, you should always configure your dependencies on your build tool and then import that in the IDE; rather than the opposite. IDEs are graphical tools, so their state can not be committed, can not be shared, can not keep track of changes, can not be used in CI / CD environments; additionally, different teammates may want to use different IDEs.
I resolved the error message by adding line in build.sbt file
libraryDependencies += "de.unruh" %% "scala-isabelle" % "master-SNAPSHOT"
and by subsequent run of sbt update.
Error is solved, but the main question still stand - why I had to do this? Why there are tens of dependencies that are not listed in build.sbt and why should I list my dependency in build.sbt and why it is not sufficient to list it Project-Setting--External Libraries in IntelliJ only?
OK, comment by #Luis_Miguel_Mejía_Suárez gave the explanation, that comment is the actual and expected answer to my question.
I'm following this tutorial to create a scala jar to upload to streamsets to use in a spark evaluator.
I'm using Intellij 2017.3.4.
If I use Intellij to build the artifact into the out folder, it's over 100mb in size.
If i use the 'sbt clean package' option from the command line, the jar is 3mb.
They both work fine.
Could anyone tell me why there's such a difference, and how I'd setup my Intellij so I create the smaller version?
Thanks
Matt
Without further knowledge of your build.sbt and your IntelliJ settings I can only guess that your build.sbt contains a line like this:
libraryDependencies += "org.apache.spark" %% "spark-core" % "2.3.0" % Provided
Notice the Provided. It means that spark is not bundled in the jar that sbt prepares, but is expected to already be present at the server in which you run your program (on your class path).
IntelliJ uses all the jars (including those marked provided) so that you can debug/run your program via IntelliJ. Since you did not add your build settings in IntelliJ this is the best I can do for now.
If you use sbt assembly to package your artifact, and you want to do this with a run configuration in IntelliJ you can go to Run/Debug Configurations, click the + button, select sbt task and fill in assembly as the task.
Read more about packaging your spark app here.
I hope that helps :)
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
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.
is any IDE supporting SBT in a proper way (like Maven for example)? Because I've found a lot of tools that generate IDE-related configuration files but I haven't found any plugins that give any support of SBT interaction form within IDE.
I want to make an IDE-agnostic project based on SBT, but also I want to be able to use full spectrum of features that IDE provide and not just use it as an editor and do all the other stuff from console.
Does Intellij fit the bill ? It has an SBT plugin (and a Scala plugin, obviously!)
I know this isn't exactly what you're looking for, but putting it here as a work-around for working with SBT in eclipse for whoever is interested.
SBT generates eclipse config files, but after you import it, it works fine from within eclipse. You just need to set up the project for the first time outside of Eclipse, run SBT to resolve dependencies, generate eclipse structure using the eclipse sbt plugin and import into Eclipse. After that, you can run the code directly from Eclipse and it works fine.
Here're the steps in detail:
Create the folder structure as follows:
Create a file called plugins.sbt in the project folder and add the following line to it:
addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "2.1.0")
Create build.sbt in the root directory i.e. ScalaSBTProject with content similar to the following. I'm using akka here, but add and remove libraries as you require:
name := "ScalaSBTProject"
version := "1.0"
scalaVersion := "2.10.0-RC2"
resolvers += "Typesafe Repository" at "http://repo.typesafe.com/typesafe/releases/"
libraryDependencies += "com.typesafe.akka" % "akka-cluster-experimental_2.10.0-RC2" % "2.1.0-RC2"
Open command prompt and run sbt in the directory ScalaSBTProject. SBT will download and resolve whatever dependencies are required
Run the command eclipse at the SBT command line. This will generate all the eclipse related project files
Import ScalaSBTProject into Eclipse using File->Import->Existing Project to workspace, and make sure you check Import into workspace
EDIT: Just as a Post-Script, you can quite easily create a batch file to take the name of the project and generate the eclipse compatible project, just a way to speed up the process.