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

后端 未结 2 642
太阳男子
太阳男子 2021-01-31 04:42

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

2条回答
  •  盖世英雄少女心
    2021-01-31 05:36

    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.

提交回复
热议问题