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

2.8k views Asked by At

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.

2

There are 2 answers

1
David Soergel On BEST ANSWER

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.

  1. Install the SBT runner script per step 1 above. (It can handle SBT 0.7 projects too).
  2. Manually git clone git://github.com/twitter/finagle.git.
  3. 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).
  4. Create an IDEA project from scratch, and manually add dependencies to the finagle jars and their dependencies (under Project Structure->Dependencies).
1
David Soergel On

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.