如何在 sbt、Eclipse 和 github 中初始化一个新的 Scala 项目

2022-01-24 00:00:00 git scala eclipse sbt

如何在 sbt、Eclipse 和 github 中初始化一个新的 Scala 项目,让它们一起玩……

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

推荐答案

一个新的 Scala 项目通常需要为 sbt、eclipse(如果你选择)和 github 设置,这样它们就可以协同工作.在此设置上投入一些时间后,只要没有更简单的方法可用,拥有此列表以对齐这 3 个工具/服务可能会有所帮助.对我有用的一系列步骤如下.它假定您在 eclipse 中安装了 Scala IDE 插件.

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. 在 Github 中创建一个新的 repo .
  2. 确定新项目的目录位置
  3. 在 Eclipse 中,使用 Git 存储库视图将 Github 存储库导入该位置.或者,您可以为此使用命令行 git.
  4. 找到您为项目选择的同一位置并运行 sbt eclipse.这确保 eclipse 能够处理 sbt 项目结构,以便您的项目可以由 sbt 构建,同时也可以被 eclipse 理解.如果 sbt eclipse 不起作用,可能是 sbt 中没有安装 sbt eclipse 插件 - install它.
  5. 在eclipse中,使用File -->导入 -->一般 -->Existing Projects into Workspace,选择相同的位置,以便 eclipse 为 sbt 刚刚准备好的文件结构构建其项目结构.
  6. 通过更新 .gitignore 文件以忽略 eclipse 和 sbt 文件,使 git 忽略新项目的核心以外的所有内容.以下似乎目前很好.

  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

您现在应该能够在 eclipse 和 sbt 中运行项目,并通过 git 提交和推送代码更改.要查看空项目运行,这在这个阶段可能很有意义,您可以在 eclipse 中添加一个 scala 类,仅包含以下代码.请注意,scala 源代码通常应位于 src/main/scala 下.如果此路径尚不存在,请通过例如创建它mkdir -p src/main/scala 在 Unix 上.

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")  
  }
}

或者只有这个代码:

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

它现在应该可以工作了.需要声明的是,未来版本的 eclipse、sbt 等可能会使它过时.如果这在您的环境中完全错误,您可以添加更好的答案.

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.

相关文章