How to download sbt plugin source jars in a common sbt project? - scala

It's nature to download the dependencies source jars in a sbt project, using sbt gen-idea or idea's autoimport feature
but how can I download the sbt plugin source jars which I declared in project/plugins.sbt

IDEA
IDEA should already do this*.
When you import a project and have Download sbt sources checked in Preferences > Build, Execution, Deployment > Build Tools > sbt then it will run the updateSbtClassifiers sbt task.
* Read the Troubleshooting section as there are several known issues with this.
sbt
As mentioned above, you can run the following sbt task:
sbt> updateSbtClassifiers
This command is somewhat special in that it knows to resolve the classifiers for the plugins. It also uses settings that are scoped to that task. See the caveat about sbt/sbt#3432.
Longer way
It is important to understand that sbt is recursive.
Dependencies declared in build.sbt will be for the proper build.
Dependencies declared in project/plugins.sbt will be for the meta-build.
When you just run updateSbtClassifiers it is running this on the proper build, however the dependencies are actually for the meta-build. That is why I said that this task is a little special.
Another way (which can achieve different results) is to run the updateClassifiers task directly on the meta-build.
First switch over to the meta-build:
sbt> reload plugins
Now that you are in the meta-build run:
sbt:project> updateClassifiers
This will retrieve the src and doc for your dependencies (it may not actually do this for some plugins). Run libraryDependencies to see the dependencies of the meta-build.
To get back to the proper
sbt:project> reload return build run:
Troubleshooting
Download failed
You may see in the sbt logs that it failed to download either the src or doc. For example:
[warn] ::::::::::::::::::::::::::::::::::::::::::::::
[warn] :: FAILED DOWNLOADS ::
[warn] :: ^ see resolution messages for details ^ ::
[warn] ::::::::::::::::::::::::::::::::::::::::::::::
[warn] :: rocks.muki#sbt-graphql;0.5.0!sbt-graphql.jar(doc)
[warn] :: rocks.muki#sbt-graphql;0.5.0!sbt-graphql.jar(src)
[warn] ::::::::::::::::::::::::::::::::::::::::::::::
If you look just above you will see all the resolvers that it tried. You should see one for sbt-plugin-releases which is where most sbt plugins are published to. See predefined resolvers for more details.
If you do not see that it tried sbt-plugin-releases then chances are you have encountered sbt/sbt#3432.
updateSbtClassifiers does not use the correct resolvers. Add the following to your build.sbt file:
updateSbtClassifiers / dependencyResolution := IvyDependencyResolution((updateSbtClassifiers / ivyConfiguration).value)
Sources not attaching
Even if the updateSbtClassifiers successfully downloads and resolves the src and doc IDEA may not attach them.
This seems to be a bug in the sbt-structure plugin. See SCL-13619 for details.
No attempt at downloading
If for some reason this doesn't download the sources (there is no failed download message) then have a look in your .ivy2/exclude_classifiers file. I'm not entirely sure what this file is for but I do know that sbt will exclude anything that is in here.
In my case it had a whole lot of things that I didn't want excluded so I deleted it and then it worked. Delete at your own risk.

You can use reload plugins to go into the project that contains the plugins. If you type libraryDependencies you can (for example) see the list of plugin dependencies. Use reload return to return to your normal sbt console.
More information here: Commands for managing the build definition

Related

How to create executable jar in scala which can run on jvm?

I am using scala version 2.11.4, I have tried various options like sbt-assembly, build artifact (Intellij Idea feature), sbt package. Unfortunately, none of them worked form me.
I attempted following things :
With sbt-assembly :
Created assembly.sbt file and added following line :
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.12.0")
build.sbt :
scalaVersion in ThisBuild := "2.11.4"
resolvers += Resolver.url("bintray-sbt-plugins", url("http://dl.bintray.com/sbt/sbt-plugin-releases"))(Resolver.ivyStylePatterns)
ivyScala := ivyScala.value map { _.copy(overrideScalaVersion = true) }
I got the following error
[warn] Note: Some unresolved dependencies have extra attributes. Check that these dependencies exist with the requested attributes.
[warn] com.eed3si9n:sbt-assembly:0.12.0 (sbtVersion=0.13, scalaVersion=2.11)
With 'build artifact' feature using Intellij Idea 15
Able to create a jar. However, not able to execute it. Was getting following error:
Invalid or corrupt jarfile
For this I tried the command : java -jar JAR_FILE
With sbt package :
Able to create JAR. However, not able to execute it. Was getting following error :
java.lang.NoClassDefFoundError scala/Function0
I was trying with the command :
java -cp scala-library.jar -jar JAR_FILE
Resolved
I am able to create jar by switching to scala version 2.10.5 and then used sbt-assembly plugin. Slightly disappointed with the fact that there is no available solution to create executable jar with latest version of scala.
If you are using sbt-assembly plugin, the correct task to perform is called assembly. When using IntelliJ IDEA, sbt assembly needs to be performed from a command line, the IDE is still unable to perform such SBT tasks.
The resulting jar, which includes all dependencies, is usually called a fat jar - if you need some more searching, this is what to search for.
Since I can't comment yet, I'll use answer, but more of a question - I have been using sbt package extensively for exactly this. I cd into the directory that holds src/main/scala|java and running sbt package, and then pushing jar/war file to the desired destination, in my case jetty.
Can you explain exactly what you're doing, the output and details on the issue?

Public SBT Plugin Repo seems to be broken

Currently all our builds are failing because that are not able to resolve plugins from the public SBT Plugin Repo. We get the following error:
[warn] Note: Some unresolved dependencies have extra attributes. Check that these dependencies exist with the requested attributes.
[warn] com.typesafe.play:sbt-plugin:2.4.0 (scalaVersion=2.10, sbtVersion=0.13)
[warn] com.github.gseitz:sbt-release:1.0.0 (scalaVersion=2.10, sbtVersion=0.13)
The SBT docs list the predefined repos. The link for the sbt plugins results in a 404. I think some kind of redirect mechanism is broken.
I know that the actual artifacts are hosted on bintray. Therefore i tried to point my build directly to this repo, but this does seem to help. I added following to my project/plugins.sbt:
resolvers += Resolver.url(
"fix-sbt-plugin-releases",
url("http://dl.bintray.com/sbt/sbt-plugin-releases"))(
Resolver.ivyStylePatterns)
Does anyone know a fix how to circumvent this?
Will not help you right now, but you might want to consider it for the future:
You could use something like Artifactory to mirror all of your dependencies in your local network.
You can than configure Artifactory as a proxy and would only be disappointed when you try to download a new dependency during down time (which should be pretty unlikely).
Makes dependency resolving faster, too.

Why IntelliJ can't find SBT snapshot dependencies?

I have a Play 2.3 Scala project which runs perfectly when started with the activator run command. However, if I try to start it in IntelliJ 13 from the Run/Debug Configurations option there's a problem with resolving snapshot dependencies.
According to logs following repositories has been checked in order to find dependencies:
[warn] module not found: org.package#my-dep;1.0-SNAPSHOT
[warn] ==== local: tried
[warn] C:\Users\MyUser\.ivy2\local\org.package\my-dep\1.0-SNAPSHOT\ivys\ivy.xml
[warn] ==== public: tried
[warn] http://repo1.maven.org/maven2/org/package/my-dep/1.0-SNAPSHOT/my-dep-1.0-SNAPSHOT.pom
[warn] ==== Typesafe Releases Repository: tried
[warn] http://repo.typesafe.com/typesafe/releases/org/package/my-dep/1.0-SNAPSHOT/my-dep-1.0-SNAPSHOT.pom
Dependencies are present at my ~/.ivy2/cache directory. I also have there other dependencies which aren't marked as snapshots and it looks like IntelliJ doesn't have a problem with finding them. It only applies to snapshots.
Update
It looks like IDEA ignores my ~/.sbt/repositories file where snapshot repository is defined and uses default SBT plugin configuration.
Is it actually possible to configure IDEA so it would use my custom repository definition?
I tried without success to set custom sbt-launcher in Settings -> SBT -> Launcher -> Custom.
After I run my application with Run configuration I could see in the logs that for some reason launcher that I specified was overridden by another which was part of the Scala plugin.
-Dsbt.global.base=C:\Users\User\AppData\Local\Temp\sbt-global-plugin3401565420094215394stub -classpath C:\Users\User\.IntelliJIdea13\config\plugins\Scala\launcher\sbt-launch.jar
Finally I managed to force IDEA to use my ~/.sbt/repositories by adding -Dsbt.repository.config=C:\Users\User\.sbt\repositories to the Run Configuration JVM Options.
I have also set -Dsbt.override.build.repos=true to make sure that only repositories listed in my custom configuration are used.

Error: Could not retrieve sbt 0.11.3 when running existing project in Play 2.1.0?

I'm new to Play Framework. I've just read about how to create and run a play project. I've been given a project and am facing Error: Could not retrieve sbt 0.11.3:
Getting org.scala-sbt sbt 0.11.3 ...
:: problems summary ::
:::: WARNINGS
module not found: org.scala-sbt#sbt;0.11.3
==== local: tried
/home/ritesh/Play/play-2.1.0/repository/local/org.scala-sbt/sbt/0.11.3/ivys/ivy.xml
==== Maven2 Local: tried
file:///home/ritesh/.m2/repository/org/scala-sbt/sbt/0.11.3/sbt-0.11.3.pom
==== typesafe-ivy-releases: tried
http://repo.typesafe.com/typesafe/ivy-releases/org.scala-sbt/sbt/0.11.3/ivys/ivy.xml
==== Maven Central: tried
http://repo1.maven.org/maven2/org/scala-sbt/sbt/0.11.3/sbt-0.11.3.pom
::::::::::::::::::::::::::::::::::::::::::::::
:: UNRESOLVED DEPENDENCIES ::
::::::::::::::::::::::::::::::::::::::::::::::
:: org.scala-sbt#sbt;0.11.3: not found
::::::::::::::::::::::::::::::::::::::::::::::
:: USE VERBOSE OR DEBUG MESSAGE LEVEL FOR MORE DETAILS
unresolved dependency: org.scala-sbt#sbt;0.11.3: not found
Error during sbt execution: Error retrieving required libraries
(see /home/ritesh/Play/play-2.1.0/framework/sbt/boot/update.log for complete log)
Error: Could not retrieve sbt 0.11.3
I only installed Play Framework 2.1.0 distribution and nothing else.
How can I get passed the error and execute the play command successfully?
I think your existing project has dependencies to sbt 0.11.3 and you are running it with Play 2.1.0 which uses sbt 0.12.2.
Check the file build.properties located under the project folder. If it says 0.11.3 your project probably uses another Play version and need to be upgraded to 2.1.0.
The sbt-plugin should have Play 2.1.0 assigned - se plugins.sbt under project folder.
Either ugrade your project: http://www.playframework.com/documentation/2.1.0/Migration
Or - switch to an older Play version - the correct for your project. => http://www.playframework.com/download.
It seems you're using play 2.1.0 which is distributed with sbt 0.12.2.
The solution is not to upgrade your play distribution as you only need to edit your project/build.properties file and change
sbt.version=0.11.3
to
sbt.version=0.12.2
With the change, the project should be fine.
Play 2.1 does not come with sbt 0.11.3. Since you are not on a window box I recommend you use sbt-extras. That utility will automatically download the correct version of sbt.
curl https://raw.github.com/paulp/sbt-extras/master/sbt > ~/bin/sbt
chmod +x ~/bin/sbt
And then from within your project directory:
~/bin/sbt
If files in your bin folder are automatically on the path you can skip the ~/bin/ part.
To work without sbt extras you would need to download the appropriate play version which can be found in the project/plugins.sbt within the directory of your project.

scala-redis requires sbt 0.10.1 (other versions don't work) - is that normal?

While trying out the Scala bindings for Redis, I followed the instructions in the README file (clone the repo, cd into it, type sbt) and I got this:
$ sbt
Getting org.scala-tools.sbt sbt_2.9.1 0.10.1 ...
:: problems summary ::
:::: WARNINGS
module not found: org.scala-tools.sbt#sbt_2.9.1;0.10.1
==== local: tried
/Users/noah/.ivy2/local/org.scala-tools.sbt/sbt_2.9.1/0.10.1/ivys/ivy.xml
-- artifact org.scala-tools.sbt#sbt_2.9.1;0.10.1!sbt_2.9.1.jar:
/Users/noah/.ivy2/local/org.scala-tools.sbt/sbt_2.9.1/0.10.1/jars/sbt_2.9.1.jar
==== Maven2 Local: tried
file:///Users/noah/.m2/repository/org/scala-tools/sbt/sbt_2.9.1/0.10.1/sbt_2.9.1-0.10.1.pom
-- artifact org.scala-tools.sbt#sbt_2.9.1;0.10.1!sbt_2.9.1.jar:
file:///Users/noah/.m2/repository/org/scala-tools/sbt/sbt_2.9.1/0.10.1/sbt_2.9.1-0.10.1.jar
==== typesafe-ivy-releases: tried
http://repo.typesafe.com/typesafe/ivy-releases/org.scala-tools.sbt/sbt_2.9.1/0.10.1/ivys/ivy.xml
-- artifact org.scala-tools.sbt#sbt_2.9.1;0.10.1!sbt_2.9.1.jar:
http://repo.typesafe.com/typesafe/ivy-releases/org.scala-tools.sbt/sbt_2.9.1/0.10.1/jars/sbt_2.9.1.jar
==== Maven Central: tried
http://repo1.maven.org/maven2/org/scala-tools/sbt/sbt_2.9.1/0.10.1/sbt_2.9.1-0.10.1.pom
-- artifact org.scala-tools.sbt#sbt_2.9.1;0.10.1!sbt_2.9.1.jar:
http://repo1.maven.org/maven2/org/scala-tools/sbt/sbt_2.9.1/0.10.1/sbt_2.9.1-0.10.1.jar
==== Scala-Tools Maven2 Repository: tried
http://scala-tools.org/repo-releases/org/scala-tools/sbt/sbt_2.9.1/0.10.1/sbt_2.9.1-0.10.1.pom
-- artifact org.scala-tools.sbt#sbt_2.9.1;0.10.1!sbt_2.9.1.jar:
http://scala-tools.org/repo-releases/org/scala-tools/sbt/sbt_2.9.1/0.10.1/sbt_2.9.1-0.10.1.jar
==== Scala-Tools Maven2 Snapshots Repository: tried
http://scala-tools.org/repo-snapshots/org/scala-tools/sbt/sbt_2.9.1/0.10.1/sbt_2.9.1-0.10.1.pom
-- artifact org.scala-tools.sbt#sbt_2.9.1;0.10.1!sbt_2.9.1.jar:
http://scala-tools.org/repo-snapshots/org/scala-tools/sbt/sbt_2.9.1/0.10.1/sbt_2.9.1-0.10.1.jar
::::::::::::::::::::::::::::::::::::::::::::::
:: UNRESOLVED DEPENDENCIES ::
::::::::::::::::::::::::::::::::::::::::::::::
:: org.scala-tools.sbt#sbt_2.9.1;0.10.1: not found
::::::::::::::::::::::::::::::::::::::::::::::
:: USE VERBOSE OR DEBUG MESSAGE LEVEL FOR MORE DETAILS
unresolved dependency: org.scala-tools.sbt#sbt_2.9.1;0.10.1: not found
Error during sbt execution: Error retrieving required libraries
(see /Users/noah/.sbt/boot/update.log for complete log)
Error: Could not retrieve sbt 0.10.1
This was using sbt 0.7.4. I tried downloading the latest version of sbt (0.11.0) and I got the same problem. Then I manually downloaded sbt-launcher.jar from the 0.10.1 release of sbt, and that worked.
My question is: is sbt supposed to automatically download the required version of itself, or am I going to have to manually download & run different versions of sbt for packages which require different versions?
The error message seems unrelated to Redis.
Yes, SBT is supposed to download the appropriate version of itself. You can find out what versions of SBT are available by pointing your browser to: http://repo.typesafe.com/typesafe/ivy-releases/org.scala-tools.sbt/ You will see that Scala 2.8.1 is tied to SBT versions <= 0.10.1, whereas Scala 2.9.1 is (currently) compatible with SBT 0.11.0 only.
You're getting this error message because somehow you're trying to get SBT 0.10.1 for Scala 2.9.1, which is an invalid combination. Not sure how you did this.. maybe you edited some internal SBT config files in the directory ~/.sbt/? If you want to get a "virgin" SBT, you can delete ~/.sbt/ and download the latest sbt-launch.jar. If you want to use a previous version of SBT, you can edit the project/build.properties file, as described on the Wiki.