Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize a new Scala project in sbt, Eclipse and github

Tags:

How to initialize a new Scala project in sbt, Eclipse and github, so that it all plays together...

like image 471
matanster Avatar asked Mar 13 '13 09:03

matanster


People also ask

How do I create a Scala project in sbt?

Delegate a Scala project build to sbtPress Ctrl+Alt+S to open the IDE settings and select Build, Execution, Deployment | Build Tools | sbt. In the sbt projects section, select a project for which you want to configure build actions. In the sbt shell section, select the builds option. Click OK to save the changes.

How do I import a Scala project into Eclipse?

To import the Scala IDE in your workspace simply click on File > Import. The Eclipse Import dialog will open. There, select General > Existing Projects into Workspace and click Next. A new dialog will open.


1 Answers

A new Scala project typically requires being set up for sbt, eclipse (if you so choose) and github such that it all works together. After investing some time on this setup, it may help to have this list for aligning these 3 tools/services, for as long as simpler ways are not available. The series of steps that works for me follows. It assumes you have the Scala IDE plugin installed in eclipse.

  1. Create a new repo in Github.
  2. Decide a directory location for the new project
  3. In eclipse, use the Git Repositories View to import the Github repo into that location. Alternatively you can use command line git for that.
  4. Locate to that same location you've chosen for the project and run sbt eclipse. This makes sure eclipse will be able to handle the sbt project structure, so that your project can be built by sbt while also being intelligible for eclipse. If sbt eclipse doesn't work, the sbt eclipse plugin is probably not installed in sbt - install it.
  5. In eclipse, use File --> Import --> General --> Existing Projects into Workspace, selecting that same location, so that eclipse builds its project structure for the file structure having just been prepared by sbt.
  6. Make git ignore all but the core of your new project by updating the .gitignore file to ignore eclipse and sbt files. The following seems to be currently fine.

    *.class *.log  # sbt specific dist/* target/ lib_managed/ src_managed/ project/boot/ project/plugins/project/  # Scala-IDE specific .scala_dependencies  # Eclipse specific .project .classpath .cache 

You should now be able to run the project in eclipse, and in sbt, and commit and push code changes through git. To see the empty project run, which may very well make sense at this stage, you can add a scala class in eclipse to it, containing merely the following code. Note that scala sources should typically sit under src/main/scala. If this path doesn't exist yet, create it through e.g. mkdir -p src/main/scala on Unix.

object hello {   def main(args: Array[String]) {     println("Main starting")     } } 

Or alternatively only this code:

object app extends App {   println("Application starting")   } 

It should work now. Need to disclaim that future versions of eclipse, sbt, etc may render this outdated. If this is dead wrong in your environment, you can add a better answer.

like image 107
matanster Avatar answered Oct 20 '22 20:10

matanster