How to use existing Intellij projects as a I move forward with SBT? - scala

Novice SBT question - Now that I've started with some basic SBT tutorials, I'd like to start using SBT build files (within Intellij) a lot more often. However, there's a couple of problems with this :
1) Existing projects that I currently publish to a jar, and later import into other projects... how do I publish this jar file to my local repository? SBT publish-local doesn't seem to fit my situation, because the project was made in Intellij and is not (yet) an SBT project.
2) Suppose I do convert the project to an SBT build setup (and then import it into Intellij).. how do I configure Intellij to to publish-local (update) each time I build the project? I do not see many configurable settings around SBT within the new Intellij SBT support.
Using Intellij 13 and SBT 0.13.1
Thanks!

to get you started up quickly on using SBT to drive Idea, have a look at my template project called skeleton
It supports most of the basic tasks you'd want to do.
To publish to your repository, use the publish task.
hope that helps!

For publishing, you simply use the publish action:
To specify the repository, assign a repository to publishTo and optionally set the publishing style. For example, to upload to Nexus:
publishTo := Some("Sonatype Snapshots Nexus" at "https://oss.sonatype.org/content/repositories/snapshots")
As for your second question, despite being a JetBrains fanboy, I have found SBT integration quite disappointing. For one thing, as the JetBrains documentation states itself, you need two plugins: their plugin and sbt-idea. You use sbt-idea to synchronize the IDEA module structure with the SBT build, and you use JetBrains' idea-sbt-plugin to execute SBT tasks in the "Before Launch" action in Run Configurations.
It sounds like you want to do an "install" on every build, so "Before Launch" action support isn't useful. I would suggest writing your own custom SBT task to install on build and using the Command Line Tools Console to execute that task with SBT as if from the command line. I know; that indirection is annoying.
Bear in mind one more thing. I have found numerous bugs with idea-sbt-plugin. At least on Mac. JetBrains told me the next version will be much better, and you can see for yourself with the next EAP version.
I certainly welcome others who have managed to have more success than I have to chime in.

Related

Why it took so long to download sbt dependencies when I create a scala project in intellij idea?

I am new to scala, when I create a scala project in intellij it took really so long to download all the jar files. I have installed the scala plugin for idea IDE. Could anyone please tell me what should I do to get on the right track? Thanks very much.
Since the scala language is really a library on top of the JVM, creating a scala project for the first time requires the download of the specified scala version from a maven repository (usually this is at least the compiler and library, which for 2.10 comes to ~20MB). This will happen the first time, even if you've installed that same version of scala on your machine outside of sbt.
Once you go through this the first time, though, the next project you create--whether via sbt on the command line or via an IntelliJ sbt project--will pull these dependencies from your local ivy cache instead.
However, should you change the language version in your project, it will once again have to download the full language dependencies for that version.
In the screenshot it try to download the source files of one of of the dependencies. That mean it will try to download not only the compiled jars, but also it's source and docs.
only after downloading all of them, the project will be ready.
Disable to downloading of the sources and docs, and it will be much faster. How to disable it depends on how you create the project. (eg if you create sbt project, make sure to uncheck the "download sources and docs" in the creation/import wizard)

How does sbt integrate with IntelliJ?

Is there a definite doc somewhere that explains all the magic that happens behind the "Typesafe Activator" generation of "IntelliJ supported" project?
The sbt build files look absolutely monstrous, and I have no idea what and where IntelliJ looks for.
This is frustrating as working from two different PCs the scala seed project refers to different hard-coded paths.
Is there a good place to start?
Last time I checked, the typesafe activator was using SBT as the underlying build tool. When creating an intellij project it would thus use the sbt-idea plugin.
I guess a possible place to start would be that plugin's documentation.
However I think there is something else going on here. I think you have the activator installed on two different PCs and are trying to share the project between both PCs whether using version control or copying the folders.
The sbt-idea plugin will indeed write some absolute path in ideas project files (most likely the absolute paths to the sbt managed libraries in the ivy cache of your home folder) since this is required for the intellij project to work.
There should be no reason to "share" the idea project files, these should be considered computer specific and should not be checked into source control, or expected to work when copied from a random computer to another. You are expected to regenerate them for each computer the project is worked on.
If that sounds like a burden, you may want to install the Intellij scala plugin. Once installed, the sbt integration will allow you to import any sbt project even if you haven't generated the intellij support in the activator. Have a look at the features page, there is a video showing how to use the plugin.

Export eclipse project to build.sbt

I have a setup with 13 different eclipse projects (mostly scala and java). All projects have dependencies on each other in different ways. Now the project is starting to get big so we want to transition to a build tool and I wanted to try SBT.
First question: Is there any way to export the build files from eclipse? I mean, I have everything working in eclipse so It feels like an "export build.sbt" would be possible.
Second question: I have not found any easy way to add the project dependencies in a sbt file. Some sites say that I should publish all projects to a local maven repo and then using dependencies to be able to build it, but that requirement seems a little extreme.
I found my answers by a friendly person on the #sbt irc-channel.
For the first question: No, there seems to be none at the moment.
For the second qestion: I should create a multi-project build and define dependencies between projects that way (following the guide at: http://www.scala-sbt.org/release/docs/Getting-Started/Multi-Project.html)

Setting up a Scala project in Eclipse, together with JUnit & Scalatest

I have recently completed the Scala course on Coursera, and since then I have been looking forward to getting my hands dirty with Scala again. I have written code for some years but I neither educated to be nor work as a programmer, so it took me a while to get a good opportunity but now that I have some time to invest and a good project to work on it's time...
Except I can't seem to get things set up properly, which I find really frustrating. I have OpenJDK 1.7.0_25 running on my Linux machine. I have downloaded and installed the Bundle Scala IDE build for Eclipse (just like we used in the course). And I got ScalaTest both as a jar file and the Eclipse plug-in.
I have a simple project (so far) and no matter what I do I can't seem to get my builds and tests in order. First off how exactly am I supposed to set up my project so that my classes and tests are actually run properly? All the assignments we got were projects that had the same structure, so do I have to have:
project
|--src
|--main
|--scala
|--test
|--scala
structure? If so why is it not the default way the project is setup when I create a new project? Do I create these folders manually, as packages or as source folders? The whole thing gets pretty murky..
I should mention that I tried to "Mavenize" the project using the contextual menu in Eclipse, added my ScalaTest dependency. The first thing that happens is that I get compile errors, at every point of dependency in my code. So clearly the library is not visible, in other words Maven does not seem to be doing much of management. I thought the whole point of Maven was to get and maintain dependencies as the project evolves. I concluded that I do not fully understand the way Maven works and thus I eventually gave up on Maven, once again, and went back to doing things manually.
Secondly, I can't seem to run my tests; the Run As... menu item does not include ScalaTest as it's mentioned in the documentation of ScalaTest Eclipse Plug-in. I have double checked that the plugin is installed. If I instead try to run using JUnitRunner then my tests are not recognized as valid tests. I have JUnit and ScalaTest on my build path, so it's got to be something else.
I suppose my overarching question is as follows:
given the Scala IDE build of Eclipse and ScalaTest, just exactly how am I supposed to set up my project (in Eclipse) so that I can just focus on writing my code and testing it, and hopefully not have any other headaches?
I work alone, and this project is not a product I need to deliver to some client. In other words I do not need to adhere to strict professionalism here. Honestly I just want to be able to code, get better acquainted with Scala and hopefully build a small data analysis tool that I will be using from time to time.
Thanks in advance!
Try using the sbt eclipse plugin:
https://github.com/typesafehub/sbteclipse
This is of course assumes that you use sbt as you build tool. If you don't at the moment you can find instructions on installation and usage here: http://www.scala-sbt.org/
Personally I've been using typesafe giter8 template (https://github.com/typesafehub/scala-sbt.g8) to setup my Scala projects, and then I use the sbt plugin mentioned above to generate eclipse project files.
Scala is somewhat Maven-based (sometimes implicitly), that's why you use that structure.
The easiest way I think is to create a simple Sbt/Maven POM and create the Eclipse project configurations (like with sbt eclipse). There you can set the dependencies (like the actual version of JUnit, Scalatest to use), so you can use the ScalaTest plugin easily.
In case of other issues, feel free to ask at the ScalaTest mailing list, Chee Seng and Bill Venners can help you a lot there.
The Scala IDE website has a full documentation on how to run unit testing frameworks with the IDE, have a look ! If you find missing elements, the bug tracker of the scala-IDE project is here.

How to set up a scala project in IntelliJ IDEA that uses git libraries

I would like to give IntelliJ IDEA a try, but I have no idea how to get going.
I am simply trying to create a new project that uses Finagle Echo Server, hosted on github, as starting point.
Assuming I'm starting with a clean install on Mac. I installed IDEA and added the Scala and SBT plugins. What steps should I take to create a project, that uses Finagle and run the code in the http server example?
PLEASE help. I realize my question sounds like a stupid question, but there are so many different approaches to working with Scala projects from SBT command line, Scala-IDE, Idea, etc, that I simply don't know how to get a comfortable development environment going.
A manual solution that doesn't require you to use SBT for your project might be more straightforward, given the SBT versioning issues. You'll still use SBT to build finagle, though.
Install the SBT runner script per step 1 above. (It can handle SBT 0.7 projects too).
Manually git clone git://github.com/twitter/finagle.git.
cd to the finagle directory and type "sbt package". Its dependencies should end up under lib_managed, and it should produce the finagle jars themselves under target/ or some such (just note the locations in the command output).
Create an IDEA project from scratch, and manually add dependencies to the finagle jars and their dependencies (under Project Structure->Dependencies).
This answer assumes that you want to use SBT. Also, I should qualify that this is my usual procedure, but I haven't confirmed that it works with finagle in particular.
0. Install IDEA, with Scala and SBT plugins. (Done by the OP; here for others)
1. Install SBT (automatic method). Copy this handy sbt runner script to a convenient location (or, if you want to keep it up to date, git clone https://github.com/paulp/sbt-extras.git and symlink the script into ~/bin), and make sure it's executable. It will automatically download whatever it needs based on the sbt.version specified in your build.properties.
2. Install sbt-idea. sbt-idea is an SBT plugin (not an IDEA plugin) that generates IDEA module files from an SBT project. It's convenient to install this globally since it's not project-specific. You don't have to download anything manually; just add this to ~/.sbt/plugins/build.sbt:
resolvers += "sbt-idea-repo" at "http://mpeltonen.github.com/maven/"
addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "0.11.0")
3. Create SBT project. Create a directory for your project, and a "project" directory within it. Create project/Build.scala as follows:
import sbt._
object MyBuild extends Build {
lazy val root = Project("root", file(".")) dependsOn finagle
lazy val finagle = RootProject(uri("git://github.com/twitter/finagle.git"))
}
See the SBT documentation for lots more options re configuring your project. Note we have to use the Full Configuration here (not just build.sbt) in order to express the github dependency.
It's also a good idea to create project/build.properties:
sbt.version=0.11.2
project.version=0.1
build.scala.versions=2.9.1
4. Generate IDEA project. cd to the directory containing the sbt-based project. type "sbt gen-idea". If all goes well, the directory will now have ".idea" and ".idea_modules" subdirectories.
5. Open the project in IDEA. It may be necessary to fix the target JDK version in the project settings. Aside from that, the project should be ready to go, with all source paths, library dependencies, etc. properly configured.