scala 创建简单项目 SBT 0.10.X

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7392474/
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-10-22 03:27:25  来源:igfitidea点击:

Create Simple Project SBT 0.10.X

scalasbt

提问by dsg

(This is a follow up to sbt not creating projects correctly. The question wasn't answered.)

(这是sbt 没有正确创建项目的后续行动。这个问题没有得到回答。)



Basically, that questionsays "I don't know how to create a project under the new sbt. With the old one, I just ran sbtin a new folder and there was a guided wizard that led me through the setup."

基本上,这个问题说“我不知道如何在新的 sbt 下创建一个项目。使用旧的,我只是sbt在一个新文件夹中运行,有一个向导引导我完成设置。”

The accepted answerdoes not explain how to create a new project, it just points to the documentation, which also doesn't explicitly say how to create a new project -- only how to write a build.sbtfile.

接受的答案没有解释如何创建一个新的项目,它只是指向的文件,也没有明确说,如何创建一个新的项目-只是如何写一个build.sbt文件。

So I tried first writing a build.sbtand then running sbtin the directory with the build.sbtfile, but I still don't see a srcdirectory to work with.

所以我尝试先写一个build.sbt,然后sbt在包含build.sbt文件的目录中运行,但我仍然没有看到可以使用的src目录。

Could someone post a simple step-by-step (I'm assuming there are like 3 steps at most) guiding how to create a new project under sbt 0.10.X?

有人可以发布一个简单的分步指南(我假设最多有 3 个步骤)指导如何在 sbt 0.10.X 下创建一个新项目?

回答by dsg

I found the answer I was looking for at this webpage: Scala 2.9.1, sbt 0.10 and ScalaTest step-by-step.

我在这个网页上找到了我正在寻找的答案:Scala 2.9.1, sbt 0.10 和 ScalaTest step-by-step

The high-level steps are:

高级步骤是:

  1. mkdir my_projectmake a folder for your project
  2. Create a simple my_project/build.sbtfile, e.g.:

    name := "A Project"
    
    version := "0.1"
    
    scalaVersion := "2.9.1"
    
    libraryDependencies ++= Seq(
      "org.scalatest" %% "scalatest" % "1.6.1" % "test"
    )
    
  3. Create a file my_project/src/main/scala/HelloWorld.scala, where you create all the directories you need as you go (e.g. create the directory structure src/main/scala/)

    object Main extends App { Console.println("Hello World!") }

  4. Execute your sbt commands: e.g. sbt run

  1. mkdir my_project为您的项目创建一个文件夹
  2. 创建一个简单的my_project/build.sbt文件,例如:

    name := "A Project"
    
    version := "0.1"
    
    scalaVersion := "2.9.1"
    
    libraryDependencies ++= Seq(
      "org.scalatest" %% "scalatest" % "1.6.1" % "test"
    )
    
  3. 创建一个文件my_project/src/main/scala/HelloWorld.scala,您可以在其中随时创建所需的所有目录(例如,创建目录结构src/main/scala/

    object Main extends App { Console.println("Hello World!") }

  4. 执行你的 sbt 命令:例如 sbt run

回答by Régis Jean-Gilles

I am surprised that noone gave another solution which is the closest to the old way (as mentioned by @dsg) to create a simple project in sbt: Just run sbt in your project directory, then issue the following commands in the sbt REPL:

我很惊讶没有人提供另一种最接近旧方法的解决方案(如@dsg 所述)在 sbt 中创建一个简单的项目:只需在您的项目目录中运行 sbt,然后在 sbt REPL 中发出以下命令:

> set name := "MyProject"
> set version := "1.0"
> set scalaVersion := "2.9.0"
> session save
> exit

Granted, it is only mildly useful as it will just create the build.sbt file (enough to make it a proper sbt project) with the corresponding properties set, and you might as well create the file by hand (I usually prefer to do so myself). It won't create the srcdirectory either.

当然,它只是稍微有用,因为它只会创建带有相应属性集的 build.sbt 文件(足以使其成为一个合适的 sbt 项目),您也可以手动创建该文件(我通常更喜欢这样做我)。它也不会创建src目录。

回答by om-nom-nom

Just a few days ago np(new project) plugin to sbt was released. It intended to dealt exactly with that problem:

就在几天前,sbt 的np新项目)插件发布了。它打算准确地处理这个问题:

Initial release. Provides a minimal interface for generating new sbt projects via,... sbt.

Basic use is to install the plugin globally and start up a new project with

初始发行。提供一个最小的界面,用于通过... sbt 生成新的 sbt 项目。

基本用途是全局安装插件并启动一个新项目

$ sbt 
$ np name:my-project org:com.mypackage version:0.1.0-SNAPSHOT  

This will generate a simple build.sbtproject for you along with the standard project directory structure for main and test sources.

For more advanced usage, see the project's readmefor more info

这将为您生成一个简单的build.sbt项目以及主要和测试源的标准项目目录结构。

有关更高级的用法,请参阅项目的自述文件以获取更多信息

回答by fehu

You can use https://github.com/n8han/giter8to generate project layout using various templates

您可以使用https://github.com/n8han/giter8使用各种模板生成项目布局

回答by txominpelu

In newer versions of sbt, you can just install sbteclipse:

在较新版本的 sbt 中,您只需安装 sbteclipse:

// ~/.sbt/plugins/build.sbt
addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "2.1.0")

then from sbt's console you can run:

然后从 sbt 的控制台你可以运行:

eclipse with-source=true

In version 0.10.x I think this post can help you:

在 0.10.x 版本中,我认为这篇文章可以帮助您:

http://dcsobral.blogspot.fr/2011/06/very-quick-guide-to-project-creation-on.html

http://dcsobral.blogspot.fr/2011/06/very-quick-guide-to-project-creation-on.html

回答by VonC

Don't forget the recent sbt 0.13.3 newcommand:

不要忘记最近的 sbt 0.13.3new命令

Example:

例子:

First, you need sbt's launcher version 0.13.13 or above.
Normally the exact version for the sbt launcher does not matter because it will use the version specified by sbt.versionin project/build.properties; however for new sbt's launcher 0.13.13 or above is required as the command functions without a project/build.propertiespresent.

Next, run:

首先,您需要 sbt 的启动器版本 0.13.13 或更高版本。
通常 sbt 启动器的确切版本并不重要,因为它将使用sbt.versionin指定的版本project/build.properties;但是对于新的 sbt 的启动器,需要 0.13.13 或更高版本,因为命令功能没有project/build.properties礼物。

接下来,运行:

$ sbt new eed3si9n/hello.g8
....
name [hello]:
scala_version [2.11.8]:

Template applied in ./hello

This ran the template eed3si9n/hello.g8using Giter8, prompted for values for “name” and “scala_version” (which have defaults “hello” and “2.11.8”, which we accepted hitting [Enter]), and created a build under ./hello.

这种电脑采用了模板eed3si9n/hello.g8使用Giter8,提示为“名”和“scala_version”值(有缺省值“ hello”和“ 2.11.8”,这是我们接受击中[Enter]键),并下创建的构建./hello

回答by user272735

I've been using https://github.com/ymasory/sbt-prototypeskeleton. See also my other answer.

我一直在使用https://github.com/ymasory/sbt-prototype框架。另请参阅我的其他答案

It was the first one that just worked and I've been a quite happy with it since then.

这是第一个刚刚奏效的作品,从那时起我就对它非常满意。

回答by cevaris

Check out the GitHub repo Scala SBT Template. In particular the buildt.sbtfile.

查看 GitHub 存储库Scala SBT 模板。特别是buildt.sbt文件。

Just clone the repo, go to that directory, then call the sbtcommand.

只需克隆 repo,转到该目录,然后调用sbt命令。

 $ git clone git://github.com/dph01/scala-sbt-template.git
 $ cd scala-sbt-template
 $ ./sbt

From inside of sbt, you can type runto execute provided code. Have fun!

在 内部sbt,您可以键入run以执行提供的代码。玩得开心!

回答by Sal81

An alternative way to generate the project structure using Intellij:

使用 Intellij 生成项目结构的另一种方法:

  • Create the directory for the project and include there a basic sbt file. You just need to specify the project name.

    name := "porjectName"

  • With Intellij import the project. During the process check the options "Use auto-import" and "Create directories for empty content roots automatically"

  • 为项目创建目录并在其中包含一个基本的 sbt 文件。您只需要指定项目名称。

    名称:=“项目名称”

  • 使用 Intellij 导入项目。在此过程中检查选项“使用自动导入”和“自动为空内容根创建目录”

That will create for you the basic skeleton for the sbt project.

这将为您创建 sbt 项目的基本框架。