Errors when creating a Scala project with sbt - scala

When I create a Scala project with sbt I get the following error:
ERROR org.eclipse.jgit.util.FS - caught exception in FS.readPipe()
java.io.IOException: Cannot run program "bash" (in directory "C:\Users\xyz")....
Though it will create the project and the correct structure. But when I go into the new folder of the project and run the sbt command, this error occurs:
Unresolved dependency: com.typesafe.sbteclipse#sbteclipse-plugin;5.1.0: not found
I put the dependency addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "5.1.0") in the file project/plugins.sbt.
EDIT When I put the plugins.sbt file into the ~/.sbt/0.13/plugins/ folder, then I can start sbt from the project folder but it will throw an error when in sbt I type in the command eclipse, saying it wouldn't be a valid command.
I use JDK 1.8, sbt 0.13.16 and Scala IDE 4.7.0
Why is this happening? How can I solve it?

I found the problem. There is a file called build.properties (i.e. hello-world/project/build.properties) and in this file I had a this line: sbt.version=1.00.00. I changed this to sbt.version=0.13.16 then everything worked. I hope this helps others with a similar problem.
Though it is still not clear why sbt new scala/hello-world creates the project with sbt.version=1.00.00 by default. Especially when typing sbt about will output that the used version is 0.13.16.
Edit: It is choosing version 1.00.00 (even when you download the 0.13.16 installer) because this is the newest version, if you want to run an older (still supported) version you will have to change it in the above mentioned sbt file.

Related

Tasks stuck with sbt 1.0

Following the "https://www.coursera.org/learn/scala-spark-big-data/supplement/R7FJ9/eclipse-tutorial" course, I'm trying to create a eclipse project from sbt, by adding sbteclipse plugin.
sbt new scala/hello-world.g8
cd hello-world
echo 'addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "5.1.0")' > project/plugins.sbt
sbt> eclipse
It should last a minute and return several successful, but instead it stay still, it gets stuck. The message I've received is
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=256m; support was removed in 8.0
I've installed sbt 1.0.2, it fails, then I'd install sbt 0.13.16 and still fails. Checking the sbt version it says it's 1.0.2 (although I'd uninstalled sbt 1.0.2 to install the older version).
checking https://github.com/typesafehub/sbteclipse/issues/346 it seen like this is a brand new issue, it's supose the gets fix by typing sbt.version=0.13.16 in project/build.properties, but even after that the problem persist. I don't know if problem persist because is a fix for Mac and I'm working on windows 10, or if the solution saids is for sbt.version=0.13.15 and I'm using sbt.version=0.13.16.
Checking the \scala hello world\hello-world\eclipse file it says:
[error] [C:\Users\israel.rodriguez\Documents\eclipse\scala hello world\hello-world\project\plugins.sbt]:1: unclosed character literal
Project loading failed: (r)etry, (q)uit, (l)ast, or (i)gnore?
Terminate batch job (Y/N)?
checking the \scala hello world\hello-world\project\plugins.sbt I found :
'addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "5.1.0")'
when removing the ' ' at the beginning and the end. the \scala hello world\hello-world\eclipse file saids :
[info] Loading project definition from
C:\Users\israel.rodriguez\Documents\eclipse\scala hello
world\hello-world\project
[info] Set current project to hello-world (in build file:/C:/Users/israel.rodriguez/Documents/eclipse/scala%20hello%20world/hello-world/)
and it remain there for eternity. with the same message on windows shell :
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option
MaxPermSize=256m; support was removed in 8.0
My question is, what's wrong over here? ,and how can I solve or work around this. Sorry for the rookie question, is my firt time with sbt, scala or spark.
I was in exact same scenario with the same problem as mentioned above. Here is how I overcame it.
Firstly there is no problem in steps that you follow everything is correct except:
addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "5.1.0")
Use latest version for the sbt plugin form, which is:
addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "5.2.4")
The latest version handles the hanging console problem as described here, also there is no need to use '' at the beginning and the end.
Step: 1. Download and install sbt from here (windows sbt-1.0.4.msi)
Step 2. Go to cmd prompt and type sbt about, it will display something like this
C:\Users\rajnish.kumar>sbt about
"C:\Users\rajnish.kumar\.sbt\preloaded\org.scala-sbt\sbt\"1.0.4"\jars\sbt.jar"
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=256m; support was removed in 8.0
[warn] Executing in batch mode.
[warn] For better performance, hit [ENTER] to switch to interactive mode, or
[warn] consider launching sbt without any commands, or explicitly passing 'shell'
[info] Loading project definition from C:\Users\rajnish.kumar\project
[info] Set current project to rajnish-kumar (in build file:/C:/Users/rajnish.kumar/)
[info] This is sbt 0.13.15
[info] The current project is {file:/C:/Users/rajnish.kumar/}rajnish-kumar 0.1-SNAPSHOT
[info] The current project is built against Scala 2.10.6
[info] Available Plugins: sbt.plugins.IvyPlugin, sbt.plugins.JvmPlugin, sbt.plugins.CorePlugin, sbt.plugins.JUnitXmlReportPlugin, sbt.plugins.Giter8TemplatePlugin
[info] sbt, sbt plugins, and build definitions are using Scala 2.10.6
I'm not sure why it says "This is sbt 0.13.15"; if anybody knows please answer it here.
Step 3. Go to your workspace form cmd prompt and type and hit sbt new sbt/scala-seed.g8, type project name as (e.g. hello) when asked.
Step 4. Now move to your project directory i.e cd hello
Step 5. Now type echo addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "5.2.4") > project/plugins.sbt
Step 6. Make sure you are inside the hello project and then instead of calling sbt > eclipse (if you will do like this way then it will get stuck in the same infinite loop as you're getting) only call sbt and it will start the server on default port.
Step 7. Now type eclipse, now it will start downloading all dependency, eventually it will show Successfully created Eclipse project files for project(s):
Step 8. Import project (hello) to Eclipse as normal and it will work. Please not do not call sbt > eclipse in single go call them one by one first call sbt and when server is started then only call Eclipse.
I had the same issue when I start reading about scala.
Concerning the problem of [\plugins.sbt]:1: unclosed character literal] use :
echo addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "5.1.0") > project/plugins.sbt
Remove the simple quote from the begining and the end of the echo first parameter
I had the very same issue as you describe. I use Windows 10 and wonder if it just occurs on that system.
It works if I use sbteclipse 5.2.4 and some other small modifications:
cd hello-world
echo 'addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "5.2.4")' > project/plugins.sbt
Next, I need to go to project/plugins.sbt and remove the ' at the beginning and end of the line.
Now, do NOT enter sbt > eclipse! This will just pipe the sbt console into a the newly created file and you won't execute eclipse. Instead, typ the following into the command line:
sbt eclipse

sbteclipse not adding generated source folders to java build path?

I ran sbt eclipse on a Scala Project and when I imported it into Scala IDE(4.0.0 RC2), it gave me a type not found error as the types referred to were actually auto-generated code which were at target/scala-2_10/src_managed/main/compiled_avro/org/... I was able to do a sbt compile at the console though.
I got it to compile by adding the above folder to the Java Build Path.
My question is that since sbt eclipse can already detect Java Projects which the current project depends on and since sbt compile works at the console, should sbt eclipse be able to figure out dependencies to source folders of generated code as well? or maybe such a feature exists and I just don't know about it?
This may not be the correct way of doing things but to fix the issue i did the following.
sbt avro:compile
sbt compile
sbt eclipse
In eclipse i right clicked on target/scala-*/src_managed/main/compiled_avro > build path > use as source folder
The sbteclipse way:
Edit your project or global build.sbt file. My global ~/.sbt/0.13/build.sbt contains:
import com.typesafe.sbteclipse.plugin.EclipsePlugin._
EclipseKeys.createSrc := EclipseCreateSrc.Default + EclipseCreateSrc.Managed
I'm using an older version of _sbteclipse, version 2.5.0 (various non-relevant reasons), which seems to require both the import and a single blank link between each line of real content (this drives me a bit crazy, yes). I don't believe the import is required for newer versions of sbteclipse.
sbt clean avro:compile compile
sbt eclipse

How to get Eclipse to resolve BuildInfo in an sbt / Scala/ BuildInfo project

Some Scala / SBT projects such as Tugboat or giter8 use BuildInfo - for example see here and here. When I import these projects into Eclipse after creating project files using sbteclipse the project will not compile as it cannot resolve BuildInfo.
There are some instructions about how to resolve this in the BuildInfo README. However If I add the suggested line to the Tugboat SBT file:
EclipseKeys.createSrc := EclipseCreateSrc.Default + EclipseCreateSrc.Managed
it does not resolve the problem. In the Tugboat SBT file it specifies some other settings - do I need to do anything else to get access to BuildInfo in Eclipse?
The EclipseCreateSrc.Managed setting tells Eclipse to use managed sources i.e. automatically created code. In the case of tugboat, the BuildInfo managed source should be found in tugboat\target\scala-2.11\src_managed\main\sbt-buildinfo (the exact directory depends on your Scala version).
I resolved my problem by calling compile from the sbt command line prior to building in Eclipse. This should generate the BuildInfo.scala file. Once this is file is present the project should generate successfully. Eclipse does not seem to be able to generate this file itself.

Intellij unable to resolve references to a specific jar file

In a given project that is driven by sbt there is some kind of corruption in the project libraries specifically for a MavenLocal repository used for kafka-spark-9.7.2.jar in which:
references to the classes provided by that jar are marked as "symbol not found" by the editor parser
however the editor (strangely) does offer to import the classes
but after accepting the import, the symbols are still marked in red as unresolved.
The following attempts to "clean things up" have already been performed:
Build | Make Project
Build | Rebuild Project
In addition I have verified that the project does build from
sbt package
on the command line
UPDATE After re-running sbt gen-idea the librraries are still not found by the Parser. yet the libraries exist -even IJ knows about them as shown in the following screenshot. Why is it that IJ can find the library
C:\Users\S80035683\.ivy2\cache\org.apache.kafka\kafka\jars\kafka-0.7.2-spark.jar!\kafka\api\FetchRequest.class
However IJ is unable to resolve any classes from that library in the Parser?
You have to build the project for Intellij, try to do this in your project root:
sbt idea with-sources=yes
This should build the project structure from scratch and add the right dependencies, usually I refresh the project after adding a dependency or a jar.
Edit:
To use the command you need this plugin, otherwise you can use gen-idea but I used it only a few times and I'm not sure how it will work out.
Edit2:
There was some confusion, first, for the IDEA SBT console you don't need to prepend the sbt command since you already are inside sbt:
If you have the sbt plugin for idea you can use gen-idea with-source=yes (without prepending sbt)
From the terminal, either you go to your project root and type sbt to enter the sbt console and use gen-idea or idea with-sources=yes (without prepending sbt)
or directly sbt gen-idea or if you have the plugin sbt idea with-sources=yes (prepending sbt)
To reach the sbt console inside idea you need to install the sbt plugin on preferences -> plugin and search for sbt and then View -> Tool Windows -> SBT Console:
To start the console click on the play button, to kill the console on the skull.
I had the same problem. I fixed it by directly writing the CLASSES and SOURCES of the problematic library. This can be found in .idea/libraries/SBT__<problematic library>_jar.xml

How to build a jar file out of github project in sbt to be used in a scala program

I am trying to use scala to access Amazon's DynamoDB and found this great package on github https://github.com/piotrga/async-dynamo
so I downloaded the code as a zip file , unzipped it and then did "sbt clean test" and getting the following error
error sbt.ResolveException: unresolved dependency: asyncdynamo#async-dynamo;1.6.0: not found
Questions : is this the correct way to generate a jar file that I can include in my Scala program or is there a better way?
thanks in advance.
EDIT:
just for the benefit of others, the SCALA SBT documentation provides lots of information regarding the build process.
Instead of generating a jar file, you can just run 'sbt publish-local' and then include the lines for the managed dependency in the other project.
Sbt/ivy will see you have the artifact that way you don't need to add the jar to the other project which is much cleaner.
Then for example if you need to update the other project you don't need to replace the jar again - just publish-local again and clean and run your other project!
You are not the only one to have problems with this it seems, see github issues page:
https://github.com/piotrga/async-dynamo/issues
The command 'sbt clean test' will run the tests sbt detects. If you want a .jar file you could use 'sbt clean package', which produces a .jar in the target/ folder.
I cloned the repo and was able to run sbt package after changing release.sbt a bit. I had to change the 'publishTo'-variable as it seemed to depend on the repository creators local environment variable, so I just commented it away.
I did not get the dependency problem, so I suppose it is correctly declared. The tests it tries to run do fail though, but sbt package compiles produces the .jar just fine.
EDIT: As Matthias Schlaipfer pointed out in the comments, the more elegant way(and much easier) would just be to add this as an depency in your build.sbt. From the readme, this is what you need to add:
resolvers += "piotrga" at
"https://github.com/piotrga/piotrga.github.com/tree/master/maven-repo"
libraryDependencies += "asyncdynamo" % "async-dynamo" % "1.6"