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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15381294/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 08:18:50  来源:igfitidea点击:

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

eclipsegitscalasbt

提问by matanster

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

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

回答by matanster

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.

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

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

    *.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/scalaon Unix.

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

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

回答by Bene Volent

Previous answer/s is/are of high importance as well as the question since these tools are not self explanatory. Based on personal experience with this challenge which seemed impossible but when one acknowledges few key insurance policy reference points things will become reality:

以前的答案与问题一样重要,因为这些工具不是不言自明的。根据个人对这一挑战的经验,这似乎是不可能的,但当人们承认几个关键的保险政策参考点时,事情就会变成现实:

  1. Implementation of development environment may require radical re-installation as new tool is adopted (if you adopt GIT after you already had SBT in place you may need to empty SBT's working directory as GIT require the cloning process to have empty house on local working directory)
  2. Keep really well updated backups especially when considering re-setting any of this "domino" environment layers as some of the GUI or command initiated cleansing operations truly take care of that sometimes with surprising effectiveness and without any regard to other tools / layers requirements
  3. Keep really good documentation (including previous pragmatic answer) on key stages to make things a reality (again)
  1. 由于采用了新工具,开发环境的实现可能需要彻底重新安装(如果在已经安装了 SBT 之后采用 GIT,则可能需要清空 SBT 的工作目录,因为 GIT 要求克隆过程在本地工作目录上有空房子)
  2. 保持真正更新的备份,尤其是在考虑重新设置任何这种“多米诺”环境层时,因为某些 GUI 或命令启动的清理操作确实会处理这些问题,有时会产生惊人的效果,并且不考虑其他工具/层的要求
  3. 在关键阶段保留非常好的文档(包括以前的务实回答)以使事情成为现实(再次)